DrawerLayout(抽屉布局)
2018-08-04 本文已影响0人
大灰狼zz
Android DrawerLayout使用总结
DrawerLayout使用详解
完美解决DrawerLayout抽屉实现不能点击后面的控件
效果图:
GIF.gif布局文件最外层使用DrawerLayout
只可以有两个子布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.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:id="@+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
</android.support.v4.view.ViewPager>
<android.support.design.widget.BottomNavigationView
android:id="@+id/bottomNavigationView"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@color/white"
app:itemIconTint="@drawable/home_nav_item_color_state"
app:itemTextColor="@drawable/home_nav_item_color_state"
app:menu="@menu/home_navigation">
</android.support.design.widget.BottomNavigationView>
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="@+id/navigationView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="left"
android:fitsSystemWindows="true"
app:headerLayout="@layout/home_nav_header"
app:menu="@menu/home_nav_drawer">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:background="@color/red"
android:gravity="center"
android:text="退出" />
</RelativeLayout>
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
NavigationView
NavigationView侧滑菜单,可以自行填充头部布局和菜单布局,还可以再添加任意布局
在需要使用的页面添加如下代码
public class HomeActivity extends BaseActivity {
private DrawerLayout drawerLayout;
private ViewPager viewPager;
private BottomNavigationView bottomNavigationView;
private NavigationView navigationView;
private TabFragmentPagerAdapter tabFragmentPagerAdapter;
private List<Fragment> fragments;
private MenuItem menuItem;
private TextView tvHeader;
private MenuItem menuItem1;
private MenuItem menuItem2;
private MenuItem menuItem3;
private MenuItem menuItem4;
@Override
public int getLayoutId() {
return R.layout.home_activity_home;
}
@Override
public void initData() {
initFragment();
}
@Override
public void initView() {
drawerLayout = findView(R.id.drawerLayout);
viewPager = findView(R.id.viewPager);
bottomNavigationView = findView(R.id.bottomNavigationView);
navigationView = findView(R.id.navigationView);
initViewPager();
initBottomNavigationView();
initNavigationView();
}
@Override
public void initListener() {
}
@Override
public void viewsClick(View view) {
}
private void initFragment() {
fragments = new ArrayList<>();
fragments.add(new MainFragment());
fragments.add(new ListFragment());
fragments.add(new ThreeFragment());
fragments.add(new StudyFragment());
}
private void initViewPager() {
tabFragmentPagerAdapter = new TabFragmentPagerAdapter(getSupportFragmentManager(), fragments);
viewPager.setAdapter(tabFragmentPagerAdapter);
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
if (menuItem != null) {
menuItem.setChecked(false);
} else {
bottomNavigationView.getMenu().getItem(0).setChecked(false);
}
menuItem = bottomNavigationView.getMenu().getItem(position);
menuItem.setChecked(true);
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
private void initBottomNavigationView() {
BottomNavigationViewHelper.disableShiftMode(bottomNavigationView);//取消位移动画
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
menuItem = item;
int i = item.getItemId();
if (i == R.id.navigation_home) {
viewPager.setCurrentItem(0);
return true;
} else if (i == R.id.navigation_list) {
viewPager.setCurrentItem(1);
return true;
} else if (i == R.id.navigation_Three) {
viewPager.setCurrentItem(2);
return true;
} else if (i == R.id.navigation_person) {
viewPager.setCurrentItem(3);
return true;
}
return false;
}
});
}
private void initNavigationView() {
//登录
tvHeader = navigationView.getHeaderView(0).findViewById(R.id.show_nav_head_text);
menuItem1 = navigationView.getMenu().findItem(R.id.nav_item1);
menuItem2 = navigationView.getMenu().findItem(R.id.nav_item2);
menuItem3 = navigationView.getMenu().findItem(R.id.nav_item3);
menuItem4 = navigationView.getMenu().findItem(R.id.nav_item4);
//通过actionbardrawertoggle将toolbar与drawablelayout关联起来
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawerLayout, R.string.home_drawer_open, R.string.home_drawer_close) {
@Override
public void onDrawerSlide(View drawerView, float slideOffset) {
//可以重新侧滑方法,该方法实现侧滑动画,整个布局移动效果
//获取mDrawerLayout中的第一个子布局,也就是布局中的RelativeLayout
//获取抽屉的view
View mContent = drawerLayout.getChildAt(0);
float scale = 1 - slideOffset;
float endScale = 0.8f + scale * 0.2f;
float startScale = 1 - 0.3f * scale;
//设置左边菜单滑动后的占据屏幕大小
drawerView.setScaleX(startScale);
drawerView.setScaleY(startScale);
//设置菜单透明度
drawerView.setAlpha(0.6f + 0.4f * (1 - scale));
//设置内容界面水平和垂直方向偏转量
//在滑动时内容界面的宽度为 屏幕宽度减去菜单界面所占宽度
mContent.setTranslationX(drawerView.getMeasuredWidth() * (1 - scale));
//设置内容界面操作无效(比如有button就会点击无效)
mContent.invalidate();
//设置右边菜单滑动后的占据屏幕大小
mContent.setScaleX(endScale);
mContent.setScaleY(endScale);
}
};
toggle.syncState();
drawerLayout.addDrawerListener(toggle);
//设置图片为本身的颜色
navigationView.setItemIconTintList(null);
//设置item的点击事件
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
if (item == menuItem1) {
drawerLayout.closeDrawer(GravityCompat.START);
} else if (item == menuItem2) {
drawerLayout.closeDrawer(GravityCompat.START);
} else if (item == menuItem3) {
drawerLayout.closeDrawer(GravityCompat.START);
} else if (item == menuItem4) {
drawerLayout.closeDrawer(GravityCompat.START);
}
return true;
}
});
//头部设置监听
tvHeader.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(HomeActivity.this, "登录成功", Toast.LENGTH_LONG).show();
}
});
}
}