侧滑菜单drawerlayout的简单使用

注释:代码实现一个侧拉菜单 及一个listview 点击条目
把条目名传入fragment**

视图类:

 <!--侧拉菜单-->
    <android.support.v4.widget.DrawerLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/drlayout">

        <!--主页面的布局-->

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

        </FrameLayout>

        <!--侧拉菜单的布局   android:layout_gravity="start"  菜单在左边  end  菜单在右边 -->
        <RelativeLayout
            android:layout_width="240dp"
            android:layout_height="match_parent"
            android:layout_gravity="end"
            android:background="#ffff00"
            android:id="@+id/rel">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@mipmap/ic_launcher"
                android:id="@+id/img"/>

            <ListView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/lv"
                android:layout_below="@id/img"></ListView>

</RelativeLayout>




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

mian类:

public class MainActivity extends AppCompatActivity {

    private ListView listView;
    private DrawerLayout drawerLayout;
    private RelativeLayout rel;
    private List<String> datas;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //查找组件
        drawerLayout = (DrawerLayout) findViewById(R.id.drlayout);
        listView = (ListView) findViewById(R.id.lv);
        rel = (RelativeLayout) findViewById(R.id.rel);

        //构造数据
        datas = new ArrayList<String>();
        for(int i=0;i<10;i++){
            datas.add("菜单"+i);
        }

        //设置数组适配器
        ArrayAdapter<String>  adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, datas);
        listView.setAdapter(adapter);

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

                //设置参数
                ContentFragment contentFragment=new ContentFragment();
                Bundle bundle=new Bundle();
                bundle.putString("value",datas.get(position));
                contentFragment.setArguments(bundle);
                //切换fragment
                getSupportFragmentManager().beginTransaction().replace(R.id.fcontent,contentFragment).commit();




            }
        });

        drawerLayout.setDrawerListener(new DrawerLayout.DrawerListener() {

            //slideOffset:侧滑菜单 宽度的像素值
            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) {

            }

            @Override
            public void onDrawerOpened(View drawerView) {
                Log.d("zzz","open");

            }

            @Override
            public void onDrawerClosed(View drawerView) {
                Log.d("zzz","close");
            }

            @Override
            public void onDrawerStateChanged(int newState) {
                Log.d("zzz","状态改变:"+newState);

            }
        });

}
}

ContentFragment.java

public class ContentFragment extends Fragment {

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v=View.inflate(getActivity(),R.layout.item,null);
        TextView tvv= (TextView) v.findViewById(R.id.tvv);

        String value=getArguments().getString("value");
        tvv.setText(value);



        return v;
    }
}

item.xml类

 <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="我是主页面"
        android:id="@+id/tvv"/>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值