Android的抽屉导航:
文件夹布局:
layout布局文件夹:
activity_main.xml 主入口文件
app_bar_main.xml 主内容文件:viewPager为多个页面,可以滑动,也可以对应抽屉的导航
nav_header_main 抽屉的头部
page1.xml
page2.xml
page3.xml
menu菜单文件夹
activity_main_drawer 抽屉的菜单导航
values文件夹
strings.xml内容就直接写:
<resources>
<string name="app_name">我的抽屉</string>
<string name="action_settings">Settings</string>
<string name="navigation_drawer_open">Open navigation drawer</string>
<string name="navigation_drawer_close">Close navigation drawer</string>
</resources>
1. 主要视图:activity_main.xml文件
<?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/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="@layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
2. app_bar_main.xml 文件为内容
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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"
android:fitsSystemWindows="true"
tools:context="ss.iflytek.admin.speechsuite.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_marginTop="?attr/actionBarSize"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".MainActivity" />
</LinearLayout>
</ScrollView>
</android.support.design.widget.CoordinatorLayout>
3.activity_main_drawer.xml 抽屉导航
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="@+id/nav_1"
android:title="11111" />
<item
android:id="@+id/nav_2"
android:title="22222222222" />
<item
android:id="@+id/nav_3"
android:title="3333333333" />
</group>
<!--<item android:title="Communicate">-->
<!--<menu>-->
<!--<item-->
<!--android:id="@+id/nav_5"-->
<!--android:title="55555555" />-->
<!--<item-->
<!--android:id="@+id/nav_6"-->
<!--android:title="666666666" />-->
<!--</menu>-->
<!--</item>-->
</menu>
主入口java:mainactivity.java
import android.support.v7.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private NavigationView mNavView; 抽屉的导航菜单
private MenuItem mNavFile;
private DrawerLayout drawerLayout; 抽屉外壳
private ViewPager mViewPager; 视图
private List<View> views; 视图总和
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); // 主入口布局
initView();
}
private void initView() {
抽屉
mNavView = (NavigationView) findViewById(R.id.nav_view); // 导航
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); // 抽屉
setNavItemListener(); // 设置导航监听事件
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); // 头部
setSupportActionBar(toolbar);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawerLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawerLayout.setDrawerListener(toggle);
toggle.syncState(); /// 控制抽屉的显示隐藏
views = new ArrayList<View>(); 页面集合
LayoutInflater mLi = LayoutInflater.from(this);
View view1 = mLi.inflate(R.layout.page1, null);
views.add(view1);
View view2 = mLi.inflate(R.layout.page2, null);
views.add(view2);
View view3 = mLi.inflate(R.layout.page3, null);
views.add(view3);
mViewPager = (ViewPager) findViewById(R.id.viewPager); /// 分页视图的框架
MyPagerAdapter adapter = new MyPagerAdapter();
mViewPager.setAdapter(adapter); /// 设置适配器
mViewPager.setCurrentItem(0); /// 设置初始页面
mViewPager.setOffscreenPageLimit(3); /// 设置缓存页面后,可以获取缓存页面的btn
/// 监听视图框滑动事件:
mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
if (position == 0) {
LogSdk.v("zdf .......滑动到:" + position);
mNavView.setCheckedItem(R.id.nav_1); // 视图滑动同时导航也要匹配
} else if (position == 1) {
mNavView.setCheckedItem(R.id.nav_1); // 视图滑动同时导航也要匹配
} else if (position == 2) {
mNavView.setCheckedItem(R.id.nav_1); // 视图滑动同时导航也要匹配
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
抽屉导航item 点击事件监听
private void setNavItemListener() {
mNavView.setCheckedItem(R.id.nav_1); // 初始选中
mNavView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
// 当前导航被选中:
item.setChecked(true);
drawerLayout.closeDrawers(); 关闭导航菜单
if (item.getItemId() == R.id.nav_1) {
mViewPager.setCurrentItem(0); // 对应的视图切换
} else if (item.getItemId() == R.id.nav_2) {
mViewPager.setCurrentItem(1);
} else if (item.getItemId() == R.id.nav_3) {
mViewPager.setCurrentItem(2);
}
return true;
}
});
}
// 创建viewPager的适配器
class MyPagerAdapter extends PagerAdapter {
@Override
public int getCount() {
// 返回要滑动的VIew的个数
// TODO Auto-generated method stub
return views.size();
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
// TODO Auto-generated method stub
return arg0 == arg1; //这行代码很重要,它用于判断你当前要显示的页面
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
// 第一:将当前视图添加到container中,第二:返回当前View
container.addView(views.get(position));
// 找到页面的btn
if (position == 0) {
initPageOne(); // 在适配器里面去获取响应的页面的id
} else if (position == 1) {
} else if (position == 2) {
}
return views.get(position);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// 从当前container中删除指定位置(position)的View;
container.removeView(views.get(position));
}
}
}