【笔记】android滑动菜单

来源:互联网 发布:淘宝u站九块邮 编辑:程序博客网 时间:2024/06/08 04:09

万恶的产品总是没事找事,侧滑菜单这种东西感觉都已经过时了,却还要用。实在没办法只能找了下资料,这里自己把认为实现起来比较容易的方法做下记录。


DrawerLayout + NavigationView

布局

<?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"    xmlns:app="http://schemas.android.com/apk/res-auto"    android:id="@+id/dl"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.sjl.slidemenu.NavigationViewActivity"    tools:openDrawer="start">    <LinearLayout        android:layout_width="match_parent"        android:layout_height="match_parent"        android:orientation="vertical">        <Button            android:id="@+id/btnSlide"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:text="滑动" />        <TextView            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:id="@+id/tvSelect"/>    </LinearLayout>    <android.support.design.widget.NavigationView        android:id="@+id/navView"        android:layout_width="wrap_content"        android:layout_height="match_parent"        android:layout_gravity="start"        android:fitsSystemWindows="true"        app:headerLayout="@layout/navigation_view_header"        app:menu="@menu/navigation_view_menu" /></android.support.v4.widget.DrawerLayout>

初始化滑动菜单

   /**     * 初始化侧滑菜单     */    private void initSlideMenu() {        //获取NavigationView头部        View header = navView.getHeaderView(0);        ivHeaderImg = (ImageView) header.findViewById(R.id.ivHeaderImg);        ivHeaderImg.setOnClickListener(this);        tvHeaderTitle = (TextView) header.findViewById(R.id.tvHeaderTitle);        tvHeaderTitle.setOnClickListener(this);        navView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {            @Override            public boolean onNavigationItemSelected(MenuItem item) {                //菜单选中                item.setChecked(true);                //关闭侧滑                dl.closeDrawers();                tvSelect.setText(item.getTitle());                return true;            }        });    }

DrawerLayout + ListView

布局

<?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/dl"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.sjl.slidemenu.ListMenuActivity">    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="vertical">        <Button            android:id="@+id/btnSlide"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:text="滑动" />        <TextView            android:id="@+id/tvSelect"            android:layout_width="match_parent"            android:layout_height="wrap_content" />    </LinearLayout>    <ListView        android:id="@+id/lvMenu"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:layout_gravity="start"        android:background="#ffffffff"></ListView></android.support.v4.widget.DrawerLayout>


初始化滑动菜单

   /**     * 初始化侧滑菜单     */    private void initSlideMenu() {        View header = LayoutInflater.from(this).inflate(R.layout.navigation_view_header,null);        //添加不可被选择的头部        lvMenu.addHeaderView(header,null,false);        ivHeaderImg = (ImageView) header.findViewById(R.id.ivHeaderImg);        ivHeaderImg.setOnClickListener(this);        tvHeaderTitle = (TextView) header.findViewById(R.id.tvHeaderTitle);        tvHeaderTitle.setOnClickListener(this);        adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,items);        lvMenu.setAdapter(adapter);        lvMenu.setOnItemClickListener(new AdapterView.OnItemClickListener() {            @Override            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {                tvSelect.setText(items[position-1]);                dl.closeDrawers();            }        });    }

源码






0 0
原创粉丝点击