使用TabLayout,ViewPager导航抽屉活动

时间:2016-11-30 08:29:40

标签: android android-viewpager navigation-drawer android-tablayout

我使用本教程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);
    }
}

Frgament布局

<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布局时,它运行良好,但我不需要在其他一些片段中使用它(当我点击新的导航项目时)。

可以设置TabLayout-&gt; layout_height =&#34; 0dp&#34;在MainActivity布局中,和 将TabLayout传输到新Fragment ???的构造函数后

我如何清理行,如何在Fragment中将tabLayout与ActionBar结合起来???

1 个答案:

答案 0 :(得分:0)

app:elevation

设置AppBarLayout
<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">