RecycleView瀑布流header向上滑動時tab固定在頂部實現方法

Main-zy發表於2017-10-13
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);
               
            }
        });

相關文章