RecycleView瀑布流header向上滑动时tab固定在顶部实现方法

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                //这段代码逻辑很复杂~~~~~~~~~~
                try {
                    alldy=recyclerView.computeVerticalScrollOffset();//alldy,int常量
                    if(alldy>=headerHeight && headerHeight!=0){//headerHeight,header的view的高度
//                    LogUtil.d(LogUtil.BABY_SHOW,"显示:alldy="+alldy+",headerHeight="+headerHeight);
                        if(w_act_tab.getVisibility()==View.GONE ){//tab的显示或隐藏
                            w_act_tab.setVisibility(View.VISIBLE);
                        }
                    }else{
//                    LogUtil.d(LogUtil.BABY_SHOW,"隐藏:alldy="+alldy+",headerHeight="+headerHeight);
                        if(w_act_tab.getVisibility()==View.VISIBLE){
                            head.getLocationOnScreen(a);//header向上滑动一段之后会回收,这时alldy会突然减少
                            if(!(a[0]==0 && a[1]==0)){
                                w_act_tab.setVisibility(View.GONE);
                            }else{
                                w_act_tab.setVisibility(View.VISIBLE);
                            }
                        }
                    }
                }catch (Throwable e){
                    e.printStackTrace();
                }
            }

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

你可能感兴趣的:(android)