侧滑菜单xml和代码

来源:互联网 发布:淘宝怎样绑定手机号 编辑:程序博客网 时间:2024/06/18 19:58
<android.support.v4.widget.DrawerLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/drawer">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:id="@+id/lin_content">

    </LinearLayout>

    <LinearLayout
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_gravity="start"
        android:id="@+id/menu"
        android:background="#f0f">
        <ListView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/lv"></ListView>
    </LinearLayout>

</android.support.v4.widget.DrawerLayout>


       //代码部分
        ListView listView= (ListView) findViewById(R.id.lv);
        drawerLayout = (DrawerLayout) findViewById(R.id.drawer);
        linearLayout = (LinearLayout) findViewById(R.id.menu);


        lists.add(new TabBean("新闻","1"));
        lists.add(new TabBean("关注","2"));
        lists.add(new TabBean("动态","3"));
        lists.add(new TabBean("设置","4"));

        listView.setAdapter(new MyAdapter(this,lists));

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //关闭侧滑菜单
                drawerLayout.closeDrawer(linearLayout);

                //只将 菜单 对应的类型 传过去
                Bundle bundle=new Bundle();
                bundle.putString("key",lists.get(position).getType());
                ContentFragment cf=new ContentFragment();
                cf.setArguments(bundle);

                //动态添加一个fragment
                getSupportFragmentManager().beginTransaction().replace(R.id.lin_content,cf).commit();





            }
        });


原创粉丝点击