我的应用程序中有底部导航栏,其中分别有3个标签首页,我的图书馆和帐户。我想在我的书本片段操作栏中添加2个选项卡,但又不想在其他两个fragment中看到它们。当我在我的库片段中添加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>
MyLibrary.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();
}
}
请让我知道我该怎么办。任何帮助将不胜感激。
谢谢
答案 0 :(得分:0)
您可以通过以下方式做到这一点:
<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>