<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.design.widget.TabLayout
android:id="@+id/tablelayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v4.view.ViewPager>
</LinearLayout>
<fragment
class="com.example.li.com.lichao20181017.BlankFragment"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start">
</fragment>
</android.support.v4.widget.DrawerLayout>
MainActivity
package com.example.li.com.lichao20181017;
import android.os.Bundle;
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.view.MenuItem;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private DrawerLayout mDrawerLayout;
private ViewPager mViewPager;
private TabLayout mTablayout;
private ActionBarDrawerToggle mToggle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化控件
initview();
//设置左上角的图标与drawerlayout结合
initActionBar();
//设置viewpager
initViewpager();
}
private void initViewpager() {
ArrayList<Fragment> fragments = new ArrayList<>();
fragments.add(new VRPanoFragment());
fragments.add(new VrVideoFragment());
//写一个适配器
MyPagerAdapter adapter = new MyPagerAdapter(getSupportFragmentManager());
adapter.setdate(fragments);
//tabLayout指示器有几个,创建几个
mTablayout.addTab(mTablayout.newTab());
mTablayout.addTab(mTablayout.newTab());
//是tablayout和VIewPager
mTablayout.setupWithViewPager(mViewPager);
//给TabLayout指示器设置文本,万物从0开始
mTablayout.getTabAt(0).setText("licc");
mTablayout.getTabAt(1).setText("cc");
}
private void initActionBar() {
//获取一个actionBar对象
ActionBar supportActionBar = getSupportActionBar();
//设置左上角的图片
supportActionBar.setDisplayHomeAsUpEnabled(true);
//有一个绑定DrawarLayout功能
mToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.open, R.string.close);
//同步
mToggle.syncState();
mDrawerLayout.addDrawerListener(mToggle);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(mToggle.onOptionsItemSelected(item)){
return true;
}
return super.onOptionsItemSelected(item);
}
private void initview() {
mDrawerLayout = findViewById(R.id.drawer_layout);
mTablayout = findViewById(R.id.tablelayout);
mViewPager = findViewById(R.id.viewpager);
}
}
适配器MyPagerAdapter
package com.example.li.com.lichao20181017;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import java.util.List;
public class MyPagerAdapter extends FragmentPagerAdapter {
private List<Fragment> lists;
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
public void setdate(List<Fragment> list){
lists = list;
}
@Override
public Fragment getItem(int i) {
return lists.get(i);
}
@Override
public int getCount() {
return lists.size();
}
}