2

我添加了一个带有 4 个选项卡的 Android 支持库 TabLayout(Parent)。我在父 tablayout 的一个选项卡视图中添加了另一个 TabLayout(Child)。在子选项卡布局的选项卡内滑动未正确发生(主要是由于滑动黑白父选项卡和子选项卡布局中的冲突)。如何禁用子标签布局内的滑动?

我对两个标签布局都使用了单独的 viewpager。

4

1 回答 1

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

于 2016-06-23T10:43:53.850 回答