赞
踩
1.做Module思路,做这个需要三个依赖:
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’
2.主界面布局
<android.support.v4.widget.DrawerLayout xmlns:android=“http://schemas.android.com/apk/res/android”
xmlns:app=“http://schemas.android.com/apk/res-auto”
android:id="@+id/drawer_layout"
xmlns:tools=“http://schemas.android.com/tools”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
tools:context=".MainActivity">
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <android.support.design.widget.TabLayout android:id="@+id/tab_layout" android:layout_width="match_parent" android:layout_height="wrap_content"/> <android.support.v4.view.ViewPager android:id="@+id/vp" android:layout_width="match_parent" android:layout_height="wrap_content"/> </LinearLayout> <!--一次界面,也就是左边的界面,Android:layout_gravity="start"--> <fragment class="com.bwie.textbegin.NaviFragment" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="start"/>
</android.support.v4.widget.DrawerLayout>
3.MainActivity代码
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); //初始化控件对象 initViews(); //初始化ActionBar initActionBar(); //初始化ViewPager initViewPager(); } private void initViewPager() { ArrayList<Fragment> fragments = new ArrayList<>(); fragments.add(new VRPanoFragment()); fragments.add(new VrVideoFragment()); MyAdapter adapter = new MyAdapter(getSupportFragmentManager()); adapter.setFragment(fragments); mViewPager.setAdapter(adapter); //tabLayout指示器有几个,创建几个 mTabLayout.addTab(mTabLayout.newTab()); mTabLayout.addTab(mTabLayout.newTab()); //是tablayout和ViewPaer mTabLayout.setupWithViewPager(mViewPager); //给TabLayout指示器设置文本,万物从0begin mTabLayout.getTabAt(0).setText("小辉"); mTabLayout.getTabAt(1).setText("二期"); } //设置我点击左上角,能够弹出侧边菜单 private void initActionBar() { //获取一个ActionBar对象 ActionBar actionBar = getSupportActionBar(); //给左上角一张图片,4.0意思默认图片,给true可以直接使用 actionBar.setDisplayHomeAsUpEnabled(true); //有一类提供了绑定DrawerLayout功能 mToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.open, R.string.close); //DrawerLayout和ActionBar关联 mToggle.syncState();//同步状态 mDrawerLayout.addDrawerListener(mToggle); } //设置左上角按钮具备点击事件 @Override public boolean onOptionsItemSelected(MenuItem item) { if(mToggle.onOptionsItemSelected(item)){ return true; } return super.onOptionsItemSelected(item); } private void initViews() { mDrawerLayout = findViewById(R.id.drawer_layout); mViewPager = findViewById(R.id.vp); //注意:这里是TabLayout,不是tableLayout.会报强类型转换异常,TabLayout提供了一个水平的布局来展示tabs mTabLayout = findViewById(R.id.tab_layout); //LOCK_MODE_UNLOCKED锁定模式解锁 mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED); } } 4.适配器 public class MyAdapter extends FragmentPagerAdapter { public MyAdapter(FragmentManager fm) { super(fm); } private List<Fragment> mFragments; //创建一个方法,接收外界传来的Fragment的集合 public void setFragment(List<Fragment> fragment){ mFragments = fragment ; } @Override public Fragment getItem(int i) { Fragment fragment = mFragments.get(i); return fragment; } @Override public int getCount() { return mFragments.size(); }
}
//5.效果
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。