TabLayout与RecyclerView联动

private GridLayoutManager gManager;
private boolean stopTabLayoutScroll = false;
private boolean stopRecyclerViewScroll = false;


//初始化TabLayout
private void initTabLayout(){
 for (int i = 0; i < tabs.length; i++) {
            //插入tab标签
            tabLayout.addTab(tabLayout.newTab().setText(tabs[i]));
        }
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                if (stopTabLayoutScroll) {
                    stopTabLayoutScroll = false;
                    return;
                }
                final int position = headerPosition[tab.getPosition()];
                gManager.scrollToPositionWithOffset(position, 0);
                stopRecyclerViewScroll = true;
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
            }
        });
}

//初始化RecyclerView
private void initRecyclerView(){
int lastPosition=-1;
    private void initVerticalRecyclerView() {
        gManager = new GridLayoutManager(mActivity, 3);
        recyclerViewId.setLayoutManager(gManager);
        gManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                return mDatas.get(position).getType() == TYPE_TITLE ? gManager.getSpanCount() : 1;
            }
        });

        pAdapter = new AchivementAdapter(mDatas, mActivity);
        recyclerViewId.setAdapter(pAdapter);

        recyclerViewId.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                //如果true触发了页面中tablayout的点击事件
                if (stopRecyclerViewScroll) {
                    stopRecyclerViewScroll = false;
                    return;
                }
                LinearLayoutManager l = (LinearLayoutManager) recyclerView.getLayoutManager();
                int firstPosition = l.findFirstVisibleItemPosition();
                int pos = 0;
                if (!recyclerView.canScrollVertically(1)) {
                    //先判断滑到底部,tab定位到最后一个
                    pos = headerPosition.length - 1;
                } else {
                    for (int i = 0, size = headerPosition.length; i < size - 1; i++) {
                        if (firstPosition == headerPosition[i]) {
                            pos = i;
                            break;
                        } else if (firstPosition > headerPosition[i] && firstPosition < headerPosition[i+1]) {
                            pos = i;
                            break;
                        }
                    }
                }
                if(lastPosition != pos) {
                    stopTabLayoutScroll = true;
                    lastPosition = pos;
                    tabLayout.getTabAt(pos).select();
                }
//                    tabLayout.setScrollPosition(pos, 0f, true); //只改变选择效果,实际选中的position未改变
//                    DebugLog.d("onTab 选中的=" + pos + "  " + tabLayout.getSelectedTabPosition());
            }
        });

}

 

你可能感兴趣的:(笔记)