简单侧拉抽屉加Fragment

MainActivity

package com.bwie.a20181018_a;

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;
import java.util.List;

public class MainActivity extends AppCompatActivity {

private DrawerLayout drawerLayout;
private TabLayout tabLayout;
private ViewPager viewPager;
private ActionBarDrawerToggle mToggle;
private List fragments;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //初始化控件
    initView();
    //初始化ActionBar
    initActionBar();
    //初始化ViewPager
    initViewPager();
}

private void initViewPager() {
    //创建List集合
    fragments = new ArrayList<>();
    //添加到fragments集合里
    fragments.add(new LeftFragment());
    fragments.add(new RightFragment());
    //创建适配器
    MyAdapter adapter = new MyAdapter(getSupportFragmentManager());
    //把fragment添加到adapter
    adapter.setFragments(fragments);
    //吧adapter添加到viewPager
    viewPager.setAdapter(adapter);

    //tabLayouut有几个创建几个
    tabLayout.addTab(tabLayout.newTab());
    tabLayout.addTab(tabLayout.newTab());
    //是tabLayout和viewPager  关联同步一下
    tabLayout.setupWithViewPager(viewPager);
    //设置创建的名字  万物从0开始
    tabLayout.getTabAt(0).setText("别来无恙");
    tabLayout.getTabAt(1).setText("黑暗萝莉");



}

private void initActionBar() {
    //创建ActionBar
    ActionBar actionBar = getSupportActionBar();
    //在左上角设置按钮
    actionBar.setDisplayHomeAsUpEnabled(true);
    //创建对象ActionBarDrawerToggle
    mToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.open, R.string.close);
    //同步
    mToggle.syncState();
    //关联
    drawerLayout.addDrawerListener(mToggle);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (mToggle.onOptionsItemSelected(item)){
        return true;
    }
    return super.onOptionsItemSelected(item);
}

/**
 * 点击事件
 */


private void initView() {
    drawerLayout = findViewById(R.id.drawerLayout);
    tabLayout = findViewById(R.id.tabLayout);
    viewPager = findViewById(R.id.viewPager);
    drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
}

}

MyAdapter

package com.bwie.a20181018_a;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import java.util.List;

public class MyAdapter extends FragmentPagerAdapter {

private List fragments;

public void setFragments(List fragments) {
    this.fragments = fragments;
}

public MyAdapter(FragmentManager fm) {
    super(fm);
}

@Override
public Fragment getItem(int i) {
    return fragments.get(i);
}

@Override
public int getCount() {
    return fragments.size();
}

}

activity_main.xml

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”
tools:context=".MainActivity">


    
    



你可能感兴趣的:(简单侧拉抽屉加Fragment)