Android DrawerLayout抽屉的使用

使用DrawerLayout的步骤:

①在布局文件中添加<android.support.v4.widget.DrawerLayout> 根元素

②在这个根元素中首先添加一个 内容视图组件,比如:我们添加一个<FrameLayout>容器

③再在这个根元素中添加侧滑菜单视图组件,一般来说就是一个ListView组件

④为ListView设定Adapter,和点击事件监听器

⑤为DrawerLayout添加 开、关 状态监听器

View层:

 activity_main.xml

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <!--main layout-->
    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        
    </FrameLayout>
    <!--navigation list view-->
    <!--
    android:layout_gravity="start"表示:从左向右滑出菜单
    android:choiceMode="singleChoice"表示:菜单为单选模式
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp"表示:菜单项分隔线颜色和高度
    -->
    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:background="#3E86A0"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp">

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

fragment_content.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ContentFragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</FrameLayout>

Control层:

MainActivity.xml

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {

    private DrawerLayout drawerLayout;
    private ListView listView;
    private ArrayList<String> list;
    private ArrayAdapter<String> adapter;
    private String title;
    private ActionBarDrawerToggle toggle;
    /*
      drawerLayout.setDrawerListener(DrawerLayout.DrawerListener),ActionBarDrawerToggle是
      DrawerLayout.DrawerListener的具体实现类(功能:1.改变android.R.id.home图标(构造方法)
      2.Drawer拉出、隐藏,带有android.R.id.home动画效果(syncState())
      3.监听Drawer拉出、隐藏事件)。重写两个方法onDrawerOpened()和onDrawerClosed()来几件听抽屉的拉出和隐藏
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        title= (String) getTitle();
        drawerLayout=findViewById(R.id.drawer_layout);
        listView=findViewById(R.id.left_drawer);
        list=new ArrayList<>();//数据集合
        for(int i=1;i<10;i++)
            list.add("计算机科学与技术0"+i);
        adapter=new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,list);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        //动态插入一个Fragment到FrameLayout中

        Fragment fragment=new ContentFragment();  //定义一个fragment
        Bundle bundle=new Bundle();// 使用bundle传递数据
        bundle.putString("data",list.get(position));
        fragment.setArguments(bundle);  //fragment携带上数据,通过FragmentManager传递到fragment中

        FragmentManager fragmentManager=getSupportFragmentManager();//获取fragment管理对象
        fragmentManager.beginTransaction().replace(R.id.content_frame,fragment).commit();//开启事务代替content_frame
        drawerLayout.closeDrawer(listView);//主布局关掉listView
    }
}

ContentFragment.java

public class ContentFragment extends Fragment {

    private TextView textView;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_content,container,false);
        textView=view.findViewById(R.id.text);
        textView.setText(getArguments().getString("data"));//getArguments():获取传进来的参数
        return view;
    }

}

运行效果:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章