我添加了一个包含4个标签的Android支持库TabLayout(Parent)。我在父tablayout的一个选项卡视图中添加了另一个TabLayout(Child)。在子tablayout的选项卡内滑动没有正确发生(主要是由于滑动黑白父级和子级选项卡布局中的冲突)。 如何禁用子项tablayout中的滑动?
我已经为两个tablayouts使用了单独的viewpager。
答案 0 :(得分:0)
这个简单的代码可以帮助您。在父片段中使用此示例代码添加子选项卡:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_index_markets, container, false);
viewPager = (ViewPager) view.findViewById(R.id.markets_viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) view.findViewById(R.id.markets_tabs);
tabLayout.setupWithViewPager(viewPager);
return view;
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getChildFragmentManager()/*getActivity().getSupportFragmentManager()*/);
adapter.addFrag(new yourInnerFragment(), "tab1");
adapter.addFrag(new yourSecondInnerFragment, "tab2");
viewPager.setAdapter(adapter);
viewPager.setCurrentItem(1);
}
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 addFrag(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
请注意,有时最好使用getActivity().getSupportFragmentManager()
代替getChildFragmentManager()
。
另请查看此链接:https://code.google.com/p/android/issues/detail?id=180462