带 TabLayout、ViewPager 的导航抽屉 Activity

Navigation Drawer Activity with TabLayout,ViewPager

我使用本教程 Android Material Design working with Tabs。它在 BasicActivity、EmptyActivity 中工作。 但它在导航抽屉中无法正常工作。

当我单击导航项时 -> 我不想使用 tablayout 打开新片段。 在这个片段中,我想展示 3 个新片段。

它看起来像这样 screenshot,但我想要它看起来像原来的样子(没有线条)。

MainActivity 代码

public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();
    switch (id){
        case R.id.nav_free:
            radioFreeMain = new RadioFreeMain();
            getSupportFragmentManager().beginTransaction().replace(R.id.content_main, radioFreeMain).commit();
    }

activity_main.xml

<android.support.v4.widget.DrawerLayout 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/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">

<include
    layout="@layout/app_bar_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_main"
    app:menu="@menu/lst_menu_items" >
</android.support.design.widget.NavigationView>

app_bar_main.xml

<android.support.design.widget.CoordinatorLayout 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"
android:fitsSystemWindows="true"
tools:context="r12.news.MainActivity">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:elevation="0dp"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_main" /> </android.support.design.widget.CoordinatorLayout>

content_main.xml

<LinearLayout 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/content_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="r12.news.MainActivity"
tools:showIn="@layout/app_bar_main">

片段代码

public class RadioFreeMain extends Fragment {

private TabLayout tabLayout;
private ViewPager viewPager;

public RadioFreeMain(){
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_radio_free_main, container, false);

    viewPager = (ViewPager) view.findViewById(R.id.viewpager);
    setupViewPager(viewPager);

    tabLayout = (TabLayout) view.findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(viewPager);
    return view;
}

private void setupViewPager(ViewPager viewPager) {
    RadioFreeMain.ViewPagerAdapter adapter = new RadioFreeMain.ViewPagerAdapter(getChildFragmentManager());
    adapter.addFragment(new RadioFreeWorld(), "Мир");
    adapter.addFragment(new RadioFreeRussia(), "Россия");
    adapter.addFragment(new RadioFreePolitics(), "Политика");
    viewPager.setAdapter(adapter);
}

class ViewPagerAdapter extends FragmentPagerAdapter {
    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();

    public ViewPagerAdapter(FragmentManager manager) {
        super(manager);
    }

    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

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

    public void addFragment(Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitleList.get(position);
    }
}

片段布局

<tools:android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
tools:context="r12.news.Fragments.RadioFree.RadioFreeMain">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabMode="fixed"
        app:tabGravity="fill"/>
</android.support.design.widget.AppBarLayout>

<android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"  />

当我将 TabLayout、ViewPager 添加到 MainActivity 布局中时效果很好,但我不需要在其他一些片段中使用它(当我单击新的导航项时)。

可以在MainActivity布局中设置TabLayout->layout_height="0dp",并且 将 TabLayout 转移到新 Fragment 的构造函数后 ???

如何清理行,如何在 Fragment 中结合 tabLayout 和 ActionBar???

为您的 AppBarLayout

设置 app:elevation
<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:elevation="0dp">