首页 > 解决方案 > 如何在片段活动ActionBar中添加选项卡

问题描述

我的应用程序中有底部导航栏,其中分别有 3 个选项卡主页、我的图书馆和帐户。我想在我的书籍片段操作栏中添加 2 个标签,并且不想在其他两个片段中看到它们。当我在我的图书馆片段中添加 tabLayout 时。它显示的是这样的:在此处输入图像描述

在这里,我想在我的库片段的操作栏中添加 Tab1 和 Tab2,以便它只能在这个片段中可见,而不在其他两个片段中可见。

这是我的代码:

fragment_my_library.xml

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".MyLibrary"
android:orientation="vertical">


<android.support.design.widget.TabLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?actionBarSize"
    android:id="@+id/my_tab"
    app:tabGravity="fill"
    app:tabBackground="@color/colorPrimary"
    app:tabIndicatorColor="@color/colorAccent"
    app:tabMode="fixed"/>


 <android.support.v4.view.ViewPager
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="15dp"
    android:id="@+id/pager">


</android.support.v4.view.ViewPager>

</LinearLayout>

我的图书馆.java

public class MyBooks extends Fragment {

private TabLayout my_tab;
private ViewPager pager;
private TabAdapter adapter;

public MyBooks() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_my_library, container, false);

    my_tab = view.findViewById(R.id.my_tab);
    pager = view.findViewById(R.id.pager);

    adapter = new TabAdapter(getChildFragmentManager());
    adapter.addFragment(new Tab1Fragment(), "Tab 1");
    adapter.addFragment(new Tab2Fragment(), "Tab 2");

    my_tab.setupWithViewPager(pager);

    pager.setAdapter(adapter);

    return view;
  }

}

TabAdapter.java

public class TabAdapter extends FragmentStatePagerAdapter {

private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();

public TabAdapter(FragmentManager fm) {
    super(fm);
}

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

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

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

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

}

有人请让我知道我该怎么做。任何帮助,将不胜感激。

谢谢

标签: androidandroid-fragments

解决方案


你可以这样做:

<android.support.design.widget.AppBarLayout
  android:layout_width="match_parent"
  android:layout_height="wrap_content">

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

  <android.support.design.widget.TabLayout
      android:id="@+id/tabs"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      app:tabMode="fixed"
      app:tabGravity="fill"/>

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

推荐阅读