1. 程式人生 > >側拉選單

側拉選單

mainactivity

package com.example.drawerlayout;

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 android.view.View;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

private DrawerLayout drawer;
private TabLayout tab;
private ViewPager vp;
private ActionBarDrawerToggle toggle;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
   
    iniView();
    initActionBar();
    initViewPager();
   

}

private void initViewPager() {
    ArrayList<Fragment> list=new ArrayList<>();
    list.add(new VRPanoFragment());
    list.add(new VrVideoFragment());

    MyPagerAdapter adapter=new MyPagerAdapter(getSupportFragmentManager());
    adapter.setFragments(list);
    vp.setAdapter(adapter);
    tab.addTab(tab.newTab());
    tab.addTab(tab.newTab());
    tab.setupWithViewPager(vp);
    tab.getTabAt(0).setText("趙穎冰");
    tab.getTabAt(1).setText("趙");
}

private void initActionBar() {
    ActionBar actionBar = getSupportActionBar();
    actionBar.setDisplayHomeAsUpEnabled(true);
    toggle = new ActionBarDrawerToggle(this,drawer,R.string.open,R.string.close);
    toggle.syncState();
    drawer.addDrawerListener(toggle);
}

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

private void iniView() {
    drawer = findViewById(R.id.drawer_layout);
    tab = findViewById(R.id.tab);
    vp = findViewById(R.id.vp);
    drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
}

}

adapter

package com.example.drawerlayout;

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

import java.util.ArrayList; import java.util.List;

class MyPagerAdapter extends FragmentPagerAdapter { List mfragments;

public MyPagerAdapter(FragmentManager fragmentManager) {
    super(fragmentManager);
}


@Override
public Fragment getItem(int i) {
    Fragment fragment=mfragments.get(i);
    return fragment;
}

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

public void setFragments(ArrayList<Fragment> list) {
    mfragments=list;
}

}

xml

<?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/tab"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"></android.support.design.widget.TabLayout>
    <android.support.v4.view.ViewPager
        android:id="@+id/vp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"></android.support.v4.view.ViewPager>
</LinearLayout>
<fragment
    class="com.example.drawerlayout.NaviFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="start"></fragment>

</android.support.v4.widget.DrawerLayout>