activity_main
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:id="@+id/draw">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.design.widget.TabLayout
android:id="@+id/tab"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<!--左边界面-->
<fragment
class="wanghuiqi.bawie.com.whq_day.NaviFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
/>
</android.support.v4.widget.DrawerLayout>
fragment_navi.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=".LeftFragment"
android:background="#f7f2f2">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="我是左侧边栏" />
</FrameLayout>
MainActivity
package wanghuiqi.bawie.com.whq_day;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private TabLayout tab;
private DrawerLayout draw;
private ViewPager pager;
private ActionBarDrawerToggle toggle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//1.初始化控件
iniView();
//2.初始化ActionBar
intiActionBar();
//3.初始化ViewPager
initViewPager();
}
//初始化DrawerLayout,ViewPager,Fragment
private void initViewPager() {
ArrayList<Fragment> fragments=new ArrayList<>();
fragments.add(new LeftFragment());
fragments.add(new RigthFragment());
MyFragmentAdapter adapter=new MyFragmentAdapter(getSupportFragmentManager());
adapter.setFragments(fragments);
pager.setAdapter(adapter);
//TabLayout指示器有几个就创建几个
tab.addTab(tab.newTab());
tab.addTab(tab.newTab());
//是Tablayout和ViewPager
tab.setupWithViewPager(pager);
//给Tablayout指示器设置文本,都从0开始
tab.getTabAt(0).setText("向左");
tab.getTabAt(1).setText("向右");
}
//设置点击左上角弹出侧边菜单
private void intiActionBar() {
//获取ActionBar对象
ActionBar actionBar = getSupportActionBar();
//给左上角一张图片
actionBar.setDisplayHomeAsUpEnabled(true);
//绑定DrawLayout功能
toggle = new ActionBarDrawerToggle(this,draw,R.string.open,R.string.close);
//DrawerLayout和ActionBar关联
toggle.syncState();//同步状态
draw.addDrawerListener(toggle);
}
//设置左上角按钮具备点击事件
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (toggle.onOptionsItemSelected(item)){
return true;
}
return super.onOptionsItemSelected(item);
}
private void iniView() {
tab = findViewById(R.id.tab);
draw = findViewById(R.id.draw);
pager = findViewById(R.id.pager);
//锁定模式,解锁
draw.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
}
}
Left_item
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="LeftFragment"/>
Rigth_item
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="RigthFragment"/>
NaviFragment
public class NaviFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//左侧边栏
return inflater.inflate(R.layout.fragment_navi, container, false);
}
}
MyFragmentAdapter
package wanghuiqi.bawie.com.whq_day;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import java.util.List;
public class MyFragmentAdapter extends FragmentPagerAdapter {
private List<Fragment> fragments;
public MyFragmentAdapter(FragmentManager fm) {
super(fm);
}
//创建一个方法,接收外界传来的Fragment的集合
public void setFragments(List<Fragment> fragments) {
this.fragments = fragments;
}
//返回对应条目Fragment
@Override
public Fragment getItem(int i) {
Fragment fragment = fragments.get(i);
return fragment;
}
//设置多少条目
@Override
public int getCount() {
return fragments.size();
}
}
依赖
implementation 'com.android.support:support-v4:28.0.0'
implementation 'com.android.support:cardview-v7:28.0.0'
implementation 'com.android.support:design:28.0.0'