Ask a question of TabLayout sliding


as shown in the figure, at the top is a TabLayout, outer layer, and at the bottom of the HorizontalScrollView, is a ViewPager.. How can the top ViewPager slide and be visible when you slide the bottom TabLayout? Now slide the ViewPager top Tablayout to select the corresponding one, but will not follow the slide display.

Code:

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

    <include
        android:id="@+id/toolbar"
        layout="@layout/layout_title_bar_home" />

    <HorizontalScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="none">

        <android.support.design.widget.TabLayout
            android:id="@+id/tl_game"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

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

    </HorizontalScrollView>

    <android.support.v4.view.ViewPager
        android:id="@+id/vp_game"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

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

</LinearLayout>

Class:

public class GameFragment extends LazyFragment implements ViewPager.OnPageChangeListener {

    @BindView(R.id.tl_game)
    TabLayout mTabLayout;
    @BindView(R.id.vp_game)
    ViewPager mViewPager;

    private static final String TAG = "GameFragment";
    private static final String[] titles = {"2018LOL", "", "2018", "2018LOL", "", "2018", "2018LOL", "", "2018", "2018LOL", "", "2018"};
    private List<FragmentPage> mPageList = new ArrayList<>();

    @Override
    public int contentViewId() {
        return R.layout.fragment_game;
    }

    @Override
    public void initView(Bundle savedInstanceState, View view) {
        setCenterTitle("");
        initData();
    }

    private void initData() {
        //ViewPager
        for (int i = 0; i < titles.length; iPP) {
            SpringGameFragment fragmentOne = new SpringGameFragment();
            FragmentPage onePage = new FragmentPage(fragmentOne, titles[i]);
            mPageList.add(onePage);
        }
        BaseFragmentPagerAdapter adapter = new BaseFragmentPagerAdapter(getChildFragmentManager(), mPageList);
        mViewPager.setAdapter(adapter);
        mTabLayout.setupWithViewPager(mViewPager);
    }

    @Override
    public void initEvent() {
        mViewPager.addOnPageChangeListener(this);
    }

    @Override
    protected void onLazyLoad() {

    }

    @Override
    public void onPageSelected(int position) {
        Log.d(TAG, "onPageSelected: " + position);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
//        Log.d(TAG, "onPageScrolled: "+position);
    }

    @Override
    public void onPageScrollStateChanged(int state) {
//        Log.d(TAG, "onPageScrollStateChanged: "+state);
    }
}
Mar.15,2021

Portal: How to enable horizontal scroll in tab like Google Play?


tablayout itself is a HorizontalScrollView

Menu