抽屉控件——DrawerLayout
来源:互联网 发布:淘宝图库官网 编辑:程序博客网 时间:2024/06/06 00:47
xml中设置
<android.support.v4.widget.DrawerLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"android:layout_width="match_parent"android:layout_height="match_parent"android:fitsSystemWindows="true"android:id="@+id/drawerLayout"><!--主布局--><FrameLayout android:id="@+id/framLayout" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"></FrameLayout><!--侧滑布局--><android.support.design.widget.NavigationView android:id="@+id/left_view" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" android:layout_gravity="left" app:headerLayout="@layout/headlayout" app:menu="@menu/menu"></android.support.design.widget.NavigationView>
说明
app:headerLayout:侧滑头部的布局
app:menu:侧滑列表的引用(menu资源文件)
<menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <group android:checkableBehavior="single"> <item android:id="@+id/home" android:icon="@mipmap/ic_home_white_24dp" android:title="Home"/> <item android:id="@+id/book" android:icon="@mipmap/ic_book_white_24dp" android:title="Book"/> <item android:id="@+id/music" android:icon="@mipmap/ic_music_note_white_24dp" android:title="Music"/> <item android:id="@+id/movie" android:icon="@mipmap/ic_tv_white_24dp" android:title="Movie"/> <item android:id="@+id/favorite" android:icon="@mipmap/ic_favorite_white_24dp" android:title="favorite"/> </group> <item android:title="dfsd"> <menu> <item android:id="@+id/help" android:icon="@mipmap/ic_menu_help" android:title="Help"/> <item android:id="@+id/info" android:icon="@mipmap/ic_menu_info_details" android:title="Info"/> <item android:id="@+id/infos" android:title=""/> </menu> </item></menu>
代码
public class DrawerLayoutActivity extends BaseActivity { private DrawerLayout drawerLayout; private NavigationView navigationView; private FragmentManager manager; List<Fragment> fragmentList=new ArrayList<>(); int curFragment=-1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_drawer_layout); drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout); navigationView = (NavigationView) findViewById(R.id.left_view); manager = getSupportFragmentManager(); initFragment(); initDrawerView(); switchTab(0); } private void initFragment() { fragmentList.add(new HomeFragment()); fragmentList.add(new BookFragment()); fragmentList.add(new MusicFragment()); fragmentList.add(new MovieFragment()); fragmentList.add(new FavoriteFragment()); } private void initDrawerView() { navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(@NonNull MenuItem item) { int position=switchTag(item.getItemId()); if(position!=curFragment){ switchTab(position); curFragment=position; } drawerLayout.closeDrawers(); return true; } }); } private void switchTab(int position){ Fragment fragment = manager.findFragmentByTag("" + position); FragmentTransaction beginTransaction = manager.beginTransaction(); if(fragment==null){ if(manager.findFragmentByTag("" + curFragment)!=null){ beginTransaction.hide(fragmentList.get(curFragment)); } beginTransaction.add(R.id.framLayout,fragmentList.get(position),""+position) .show(fragmentList.get(position)) .commit(); }else if(curFragment!=position){ beginTransaction.hide(fragmentList.get(curFragment)) .show(fragmentList.get(position)) .commit(); } curFragment=position; } private int switchTag(int selectItemId){ switch (selectItemId){ case R.id.home: return 0; case R.id.book: return 1; case R.id.music: return 2; case R.id.movie: return 3; case R.id.favorite: return 4; } return 0; }}
参考博客:http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2014/0925/1713.html
0 0
- 抽屉控件——DrawerLayout
- DrawerLayout抽屉控件
- Android——DrawerLayout抽屉效果
- DrawerLayout 抽屉
- DrawerLayout 抽屉
- DrawerLayout抽屉
- 完美解决DrawerLayout抽屉实现不能点击后面的控件
- iOS 自定义控件——抽屉功能
- Android 从小白到大拿之路——DrawerLayout抽屉的使用 (更新中)
- DrawerLayout 之 抽屉菜单
- Android DrawerLayout抽屉效果
- Android DrawerLayout抽屉效果
- Android DrawerLayout 抽屉
- 抽屉菜单(DrawerLayout)
- DrawerLayout 抽屉效果
- DrawerLayout 抽屉效果 侧滑
- DrawerLayout抽屉布局
- 理解DrawerLayout抽屉
- 6年技术栈总结
- 纵表和横表的概念及其相互转换
- JavaScript面向对象学习笔记——函数、匿名函数、回调函数、自调函数
- centos7不能ping通百度
- bootstrap table获取选中行的值
- 抽屉控件——DrawerLayout
- java后台处理APP表情
- 常用的英文缩写和短信缩写
- 反爬虫技术
- 微信小程序 事件冒泡,bangtap,catchtap事件绑定
- 【龙芯1c库】龙芯1c的中断分析
- php-ob缓存实现页面静态化
- git上出现:warning: LF will be replaced by CRLF
- Android Ashmem匿名共享内存 Java实例