DrawerLayout侧滑简单应用

来源:互联网 发布:被淘宝卖家投诉怎么办 编辑:程序博客网 时间:2024/05/22 05:02
//布局将原来的更改为android.support.v4.widget.DrawerLayout
<android.support.v4.widget.DrawerLayout    android:id="@+id/drawerlayout"    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"    android:layout_height="match_parent" tools:context="com.example.mydrawerlayout.MainActivity">
//帧布局    <FrameLayout        android:id="@+id/frame"        android:layout_width="match_parent"        android:layout_height="match_parent"/>    <LinearLayout        android:gravity="bottom"        android:layout_width="wrap_content"        android:layout_height="wrap_content">        <Button            android:id="@+id/lw"            android:layout_width="100dp"            android:layout_height="40dp"            android:layout_marginLeft="20dp"            android:text="联网"/>        <Button            android:id="@+id/dtm"            android:layout_width="100dp"            android:layout_height="40dp"            android:layout_marginLeft="40dp"            android:text="多条目"/>        <Button            android:id="@+id/sxl"            android:layout_width="100dp"            android:layout_height="40dp"            android:layout_marginLeft="40dp"            android:button="@null"            android:text="上下拉"/>    </LinearLayout>    <!--    和drawerLayout配合需要配这两个属性        android:layout_gravity="left"        android:choiceMode="singleChoice"        -->//滑动出来的内容<LinearLayout    android:id="@+id/ll"    android:orientation="vertical"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:layout_gravity="left"    android:choiceMode="singleChoice">    <ImageView        android:id="@+id/img"        android:layout_width="100dp"        android:layout_height="100dp"        android:src="@mipmap/fruit2"/>    <ListView        android:id="@+id/lv"        android:background="@color/colorAccent"        android:layout_width="200dp"        android:layout_height="match_parent"/>    </LinearLayout></android.support.v4.widget.DrawerLayout>
//Main方法
protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    //获取控件    initView();    //drawerlayout里面lv适配器    initData();    //点击listview监听并且更改帧布局    setListener();    //获得自定义的frament    add();}
 private void initView() {//        vp = (ViewPager) findViewById(R.id.vp);        drawerlayout = (DrawerLayout) findViewById(R.id.drawerlayout);        frame = (FrameLayout) findViewById(R.id.frame);        lv = (ListView) findViewById(R.id.lv);        ll = (LinearLayout) findViewById(R.id.ll);
}
private void initData() {    //drawerlayout里面lv适配器    arr = new ArrayList<>();    for (int i = 0; i < 5; i++) {        arr.add("chenxu"+i);    }    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(MainActivity.this, android.R.layout.simple_list_item_1, arr);    lv.setAdapter(arrayAdapter);}
//点击listview的条目,把名字赋值到帧布局上
private void setListener() {
    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {        @Override        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {            frament = new frament();            Bundle bundle = new Bundle();            bundle.putString("name",arr.get(i));            frament.setArguments(bundle);            //添加帧布局              getSupportFragmentManager().beginTransaction().replace(R.id.frame,frament).commit();            //关闭drawer            drawerlayout.closeDrawer(R.layout.activity_main);        }    });




 
原创粉丝点击