如何在抽屉导航菜单项上单击切换到列表视图

时间:2017-02-17 04:44:10

标签: android listview android-fragments navigation android-navigation-drawer

我想打开一个列表视图,点击这个我用过的listfragment的项目和抽屉导航我自定义默认抽屉导航活动当我点击导航抽屉的菜单项时,整个listfragment打开而不是片段里面的抽屉导航因为这个我无法回到抽屉导航活动(用于点击其他项目),因为listfragment作为单独的片段打开,这是我的代码

public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    if (id == R.id.teams) {
        Toast.makeText(this, "Teams clicked", Toast.LENGTH_SHORT).show();
        ListFragment listfrag
      =
(ListFragment)getSupportFragmentManager().findFragmentByTag("listfragment");

        if(listfrag==null){
           listfrag =new listFragment();

FragmentTransaction transact=getSupportFragmentManager().beginTransaction();
            transact.add(android.R.id.content,listfrag,"lstfragment");
            transact.commit();

        }

和listfragment的代码

public class listFragment extends ListFragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup 
container,  Bundle savedInstanceState) {

   ViewGroup rootview = 
(ViewGroup) inflater.inflate(R.layout.customlistview, container, false);

    //data source
    String[] from = { "flag","txt","cur" };

 ArrayAdapter<String> adapter=new 
ArrayAdapter<String>(getActivity(),R.layout.listviewitem, R.id.tvteamsname, from);

    setListAdapter(adapter);
    //  Retain the ListFragment instance across Activity re-creation
    setRetainInstance(true);

    return rootview;

}

1 个答案:

答案 0 :(得分:0)

首先向您添加Activity的{​​{1}}添加一个框架。

navigationDrawerMenu

替换此代码
FragmentTransaction transact=getSupportFragmentManager().beginTransaction();
transact.add(android.R.id.content,listfrag,"lstfragment");
transact.commit();