如果单击NavigationDrawer,如何显示ListView

时间:2016-05-25 12:35:50

标签: java android listview navigation-drawer

当我点击DrawerNavigation的一个项目时,我想显示一个ListView。 所以,我已经构建了这段代码:

activity_main.xml中:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".activity.MainActivity">

    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <fragment
        android:id="@+id/navigation_drawer"
        android:name="it.eresult.decipher.fragment.NavigationDrawerFragment"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        tools:layout="@layout/fragment_navigation_drawer"
        />
</android.support.v4.widget.DrawerLayout>

这是MainActivity.java的代码:

public class MainActivity extends ActionBarActivity implements
        NavigationDrawerFragment.NavigationDrawerCallbacks,
        AlertsAgentDialogFragment.OnAgentListener,
        AlertsReactionDialogFragment.OnReactionListener{

    private DrawerLayout drawer;
    private String[] rightListStrings;
    private NavigationDrawerFragment mNavigationDrawerFragment;
    private CharSequence mTitle;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTitle = getTitle();

        drawer = (DrawerLayout) findViewById(R.id.drawer_layout);

        mNavigationDrawerFragment = (NavigationDrawerFragment)
                getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
        mNavigationDrawerFragment.setUp(
                R.id.navigation_drawer,
                (DrawerLayout) findViewById(R.id.drawer_layout));


        drawer.setDrawerListener(new DrawerLayout.DrawerListener() {

            @Override
            public void onDrawerStateChanged(int arg0) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onDrawerSlide(View arg0, float arg1) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onDrawerOpened(View arg0) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onDrawerClosed(View arg0) {
                // TODO Auto-generated method stub
                //rightList.setVisibility(View.INVISIBLE);
            }
        });
    }


    public void onNavigationDrawerItemSelected(int position) {
        Fragment fragment;
        switch(position) {
            default:
            case 0:
                fragment = new ListMainMenuFragment();
                break;
            case 1:
                fragment = new EncounterFragment();
                break;

        }
        if (fragment != null) {
            // update the main content by replacing fragments
            FragmentManager fragmentManager = getSupportFragmentManager();
            fragmentManager.beginTransaction()
                    .replace(R.id.container, fragment)
                    .commit();
        }

    }

    class MayAdapter extends ArrayAdapter<String>{

        List<String> myList = null;
        public MayAdapter(Context context, int resource, List<String> objects) {
            super(context, resource,  objects);
            myList = objects;
        }
        public List<String> getMyList() {
            return myList;
        }
        public void setMyList(List<String> myList) {
            this.myList = myList;
        }
    }

    public void restoreActionBar() {
        ActionBar actionBar = getSupportActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
        actionBar.setDisplayShowTitleEnabled(true);
        actionBar.setTitle("omnia");
    }

    public void onSectionAttached(int number) {
        switch (number) {
            case 1:
                mTitle = getString(R.string.treatments);
                break;
            case 2:
                mTitle = getString(R.string.setting);
                break;
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
         return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        return super.onOptionsItemSelected(item);
    }

这是ListMainMenuFragment.java(这个类有第二个listview)。

public class ListMainMenuFragment extends Fragment {
    private ListView leftList;
    private String[] leftListStrings;
    private static final int ARG_SECTION_NUMBER = 3;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        ((MainActivity) context).onSectionAttached(
                ARG_SECTION_NUMBER);
    }

    public View onCreateView(LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        View v=inflater.inflate(R.layout.list_fragment_main_menu, container, false);
        leftList = (ListView)  v.findViewById(R.id.left_list);
        leftListStrings = getResources().getStringArray(R.array.sub_menu_treatments);
        leftList.setAdapter(new ArrayAdapter<String>(getContext(),android.R.layout.simple_list_item_1,leftListStrings));

        /**
         * Inflate the layout for this fragment
         */
        return v;
    }

    class MayAdapter extends ArrayAdapter<String>{

        List<String> myList = null;
        public MayAdapter(Context context, int resource,List<String> objects) {
            super(context, resource,  objects);
            myList = objects;
        }
        public List<String> getMyList() {
            return myList;
        }
        public void setMyList(List<String> myList) {
            this.myList = myList;
        }
    }
}
  

修改

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <ListView
        android:id="@+id/left_list"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:background="@color/blue"
        android:foreground="@color/white"/>

    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />



</LinearLayout>

现在,使用此代码,我可以显示第一个NavigationDrawer,如果我点击第一个Item,我可以看到“仅”ListView。

现在,如何查看NavigationDrawer,如果点击一个项目,还可以看到ListView ???

  

修改

如果我点击一个项目,我想显示另一个“菜单”,如下所示:

enter image description here

在这一刻,使用此代码,如果我尝试单击一个项目,NavigationDrawer隐藏,我只能看到ListView

0 个答案:

没有答案