如何使用滑动标签添加材料设计导航抽屉?

时间:2015-09-08 09:28:10

标签: android android-fragments android-navigation android-drawer

我想要一些与this非常相似的东西:

我已经添加了Sliding tabs并且它工作正常,但我无法弄清楚如何将导航抽屉添加到现有代码中。

到目前为止,我已经创建了一个PagerAdapter类来查看不同的Fragment类。以下是我的MainActivity课程:

public class MainActivity extends AppCompatActivity {

private Toolbar toolbar;


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

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
    tabLayout.addTab(tabLayout.newTab().setText("Tab1"));
    tabLayout.addTab(tabLayout.newTab().setText("Tab2"));
    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

    final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
    final PagerAdapter adapter = new PagerAdapter
            (getSupportFragmentManager(), tabLayout.getTabCount());
    viewPager.setAdapter(adapter);
    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            viewPager.setCurrentItem(tab.getPosition());
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }
    });

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();


    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}

Menu_main.xml

<menu 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"
tools:context=".MainActivity">
  ......
  ......
 <item
    android:id="@+id/action_user"
    android:orderInCategory="300"
    android:title="User"
    android:icon="@drawable/ic_drawer"
    app:showAsAction="ifRoom"></item>

 </menu>

在上面的代码中,我添加了一个打开抽屉的图标。这是我的˚activity_main.xml

<RelativeLayout
android:id="@+id/main_layout"
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.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:background="?attr/colorPrimary"
    android:elevation="6dp"
    android:minHeight="?attr/actionBarSize"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

<android.support.design.widget.TabLayout
    android:id="@+id/tab_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/toolbar"
    android:background="?attr/colorPrimary"
    android:elevation="6dp"
    android:minHeight="?attr/actionBarSize"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>


   <android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:layout_below="@id/tab_layout"/>

 </RelativeLayout>

任何提示如何将Navigation Drawer添加到a​​ndroid栏?

1 个答案:

答案 0 :(得分:0)

这对Android Studio很有用template,它添加了Material Design Navigation Drawer

enter image description here UPD

要添加滑动标签,请在activity_main布局文件中包含带有PagerTabStrip的ViewPager

<android.support.v4.view.ViewPager
    android:id="@+id/tabanim_viewpager"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" >
    <android.support.v4.view.PagerTabStrip
        android:id="@+id/pager_title_strip"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:background="@color/primary_500"
        android:textColor="@color/white"
        android:paddingTop="5dp"
        android:layout_marginBottom="5dp" />
</android.support.v4.view.ViewPager>

活动onCreate:

PagerTabStrip pagerTabStrip = (PagerTabStrip) findViewById(R.id.pager_title_strip);

    pagerTabStrip.setDrawFullUnderline(false);
    pagerTabStrip.setTabIndicatorColor(getResources().getColor(R.color.red_dark));
    final ViewPager viewPager = (ViewPager) findViewById(R.id.tabanim_viewpager);
    setupViewPager(viewPager);

活动代码:

private void setupViewPager(ViewPager viewPager) {
    ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
    //adding tabs
    adapter.addFrag(new PinsFragment(), "Pins");
    adapter.addFrag(new BoardsFragment(), "Boards");
    adapter.addFrag(new InterestsFragment(), "Likes");
    adapter.addFrag(new SubscriptionsFragment(), "Subscriptions");
    adapter.addFrag(new FollowersFragment(), "Followers");
    viewPager.setAdapter(adapter);
}