0

我使用本教程Android Material Design working with Tabs。它在BasicActivity,EmptyActivity中工作。 但它在导航抽屉中无法正常工作。带TabLayout的导航抽屉活动,ViewPager

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

它看起来像这个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布局它的作品不错,但我不需要在一些其他碎片中使用这个(wh我点击新的导航项目)。

可以在MainActivity布局中设置TabLayout-> layout_height =“0dp”,在将TabLayout转换为新Fragment的构造函数后,可以设置 ?

我该如何清理线,如何将TabLayout与ActionBar在片段中结合?

+0

把你的logcat错误 –

回答

0

设置app:elevationAppBarLayout

<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"> 
+0

它的工作,如果我只有一个在onNavigationItemSelected项目。 case R.id.nav_free: radioFreeMain = new RadioFreeMain(); getSupportFragmentManager()。beginTransaction()。replace(R.id.content_main,radioFreeMain).commit(); case R.id.nav_rain: rain = new Rain(); getSupportFragmentManager()。beginTransaction()。replace(R.id.content_main,rain).commit(); 此变体不起作用 –

+0

'onNavigationItemSelected'是为布局抽屉菜单选择的操作项目。这与主屏幕无关。你能发布你的布局吗?R.id.content_main'。 –

+0

好的,我这样做。抬头。 –