Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/215.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用导航抽屉onClickListener()打开片段_Java_Android_Android Fragments_Navigation Drawer - Fatal编程技术网

Java 如何使用导航抽屉onClickListener()打开片段

Java 如何使用导航抽屉onClickListener()打开片段,java,android,android-fragments,navigation-drawer,Java,Android,Android Fragments,Navigation Drawer,在我的导航抽屉中,我使用这种方法打开活动: 编辑 mDrawerList.setOnItemClickListener(新的AdapterView.OnItemClickListener(){ 片段=空; @SuppressLint(“ResourceAsColor”)@覆盖 public void onItemClick(AdapterView父级、视图、, 内部位置,长id){ 片段=空; FragmentManager FragmentManager=getFragmentManager(

在我的导航抽屉中,我使用这种方法打开活动: 编辑

mDrawerList.setOnItemClickListener(新的AdapterView.OnItemClickListener(){
片段=空;
@SuppressLint(“ResourceAsColor”)@覆盖
public void onItemClick(AdapterView父级、视图、,
内部位置,长id){
片段=空;
FragmentManager FragmentManager=getFragmentManager();
Bundle=新Bundle();
开关(位置){
案例0:
startActivity(新的意图(Intent.ACTION_视图,Uri.parse(str));
打破
案例1:
片段=新片段();
fragment.setArguments(bundle);
fragmentManager.beginTransaction()
.replace(R.id.content_frame,fragment).commit();
打破
startActivity(新的意图(Intent.ACTION_视图,Uri.parse(strg));
打破
案例3:
startActivity(新意图(Intent.ACTION_视图,Uri.parse(strtwit));
打破
}
}
});
}

但我现在想要的是打开一个片段。有没有可能从这个结构中做到这一点?谢谢

在适当的情况下添加/替换要在容器中显示的片段

在开关情况下,使用此代码。这就是如果您想用在导航抽屉列表上单击的片段替换现有片段的方式

//通过替换片段更新主要内容

Fragment fragment = null;
            FragmentManager fragmentManager = getFragmentManager();
            Bundle bundle = new Bundle();
            switch (position) {
            case 0:

                fragment = new Dashboard_Fragment();
                break;
            case 1:

                fragment = new SearchCustomer_Fragment();
                //Request Origin
                bundle.putString("origin", "searchCustomer");
                fragment.setArguments(bundle);
fragmentManager.beginTransaction()
            .replace(R.id.frame_container, fragment).commit();

                break;
            case 2:

                fragment = new STB_Check_Fragment();
fragmentManager.beginTransaction()
            .replace(R.id.frame_container, fragment).commit();
                break;


    }

您可以显示片段而不是活动:

请参阅此片段

mDrawerList.setOnItemClickListener(new DrawerItemClickListener());  //onclick listener

/* The click listner for ListView in the navigation drawer */
private class DrawerItemClickListener implements ListView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        setCharacter(position);
    }
}

//here a new fragment is created and replaces the old one.
public void setCharacter(int position){
    Fragment fragment = new CharacterFragment();
    Bundle args = new Bundle();
    args.putInt(CharacterFragment.ARG_CHAR_NUMBER, position);
    fragment.setArguments(args);

    FragmentManager fragmentManager = getFragmentManager();
    fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();

    // update selected item and title, then close the drawer
    mDrawerList.setItemChecked(position, true);
    setTitle(mCharacterTitles[position]);
    mDrawerLayout.closeDrawer(mLinearLayout);
}

@Override
public void setTitle(CharSequence title) {
    // TODO Auto-generated method stub
    mTitle = title;
    getActionBar().setTitle(mTitle);
}

/**
 * Fragment that appears in the "content_frame", shows a planet
 */
public static class CharacterFragment extends Fragment {
    public static final String ARG_CHAR_NUMBER = "character_number";

    public CharacterFragment() {
        // Empty constructor required for fragment subclasses
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_character, container, false);
        int i = getArguments().getInt(ARG_CHAR_NUMBER);
        String charName = getResources().getStringArray(R.array.characters_array)[i];

        TextView charTextView = (TextView) rootView.findViewById(R.id.text);
        charTextView.setText(charName);
        getActivity().setTitle(charName);
        return rootView;
    }
}
mDrawerList.setOnItemClickListener(新的DrawerItemClickListener())//onclick侦听器
/*在导航抽屉中单击ListView的listner*/
私有类DrawerItemClickListener实现ListView.OnItemClickListener{
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
设置字符(位置);
}
}
//这里创建了一个新片段并替换了旧片段。
公共无效设置字符(int位置){
Fragment Fragment=新的CharacterFragment();
Bundle args=新Bundle();
args.putInt(CharacterFragment.ARG\u CHAR\u编号,位置);
fragment.setArguments(args);
FragmentManager FragmentManager=getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame,fragment.commit();
//更新所选项目和标题,然后关闭抽屉
mDrawerList.setItemChecked(位置,true);
setTitle(McCharacterTitles[位置]);
mDrawerLayout.closeDrawer(mLinearLayout);
}
@凌驾
公共无效设置标题(字符序列标题){
//TODO自动生成的方法存根
mTitle=标题;
getActionBar().setTitle(mTitle);
}
/**
*“内容框架”中出现的片段显示了一颗行星
*/
公共静态类CharacterFragment扩展了Fragment{
公共静态最终字符串ARG\u CHAR\u NUMBER=“character\u NUMBER”;
公共字符片段(){
//片段子类需要空构造函数
}
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
视图根视图=充气器。充气(R.layout.fragment\u字符,容器,false);
int i=getArguments().getInt(ARG\u CHAR\u NUMBER);
String charName=getResources().getStringArray(R.array.characters_array)[i];
TextView charTextView=(TextView)rootView.findViewById(R.id.text);
charTextView.setText(charName);
getActivity().setTitle(charName);
返回rootView;
}
}

希望这能有所帮助。

嗯……什么事也没有发生。在某个地方有提交吗?我展示的代码可以用于片段。如果您的项目中有片段,您可以遵循此操作。您不能在开关盒内启动活动。移除它们。改用片段。我有一个片段。。事实上,我没有任何错误。但是当我点击这个项目时,什么也没有发生。不要开始支离破碎,让我们。
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());  //onclick listener

/* The click listner for ListView in the navigation drawer */
private class DrawerItemClickListener implements ListView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        setCharacter(position);
    }
}

//here a new fragment is created and replaces the old one.
public void setCharacter(int position){
    Fragment fragment = new CharacterFragment();
    Bundle args = new Bundle();
    args.putInt(CharacterFragment.ARG_CHAR_NUMBER, position);
    fragment.setArguments(args);

    FragmentManager fragmentManager = getFragmentManager();
    fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();

    // update selected item and title, then close the drawer
    mDrawerList.setItemChecked(position, true);
    setTitle(mCharacterTitles[position]);
    mDrawerLayout.closeDrawer(mLinearLayout);
}

@Override
public void setTitle(CharSequence title) {
    // TODO Auto-generated method stub
    mTitle = title;
    getActionBar().setTitle(mTitle);
}

/**
 * Fragment that appears in the "content_frame", shows a planet
 */
public static class CharacterFragment extends Fragment {
    public static final String ARG_CHAR_NUMBER = "character_number";

    public CharacterFragment() {
        // Empty constructor required for fragment subclasses
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_character, container, false);
        int i = getArguments().getInt(ARG_CHAR_NUMBER);
        String charName = getResources().getStringArray(R.array.characters_array)[i];

        TextView charTextView = (TextView) rootView.findViewById(R.id.text);
        charTextView.setText(charName);
        getActivity().setTitle(charName);
        return rootView;
    }
}