Don't collapse Toolbar when RecyclerView fits the screen

前端 未结 10 1504
陌清茗
陌清茗 2020-11-30 21:08


I\'ve made an app using Android Design Library, with a Toolbar and TabLayout.
Actually 2 tabs are present, both with 2 RecyclerView, that automati

10条回答
  •  [愿得一人]
    2020-11-30 21:15

    I took a slightly different approach to solve this.

    I created a custom AppBarBehavior that disables its self based on cells.

    public class CustomAppBarBehavior extends AppBarLayout.Behavior {
    
        private RecyclerView recyclerView;
        private boolean enabled;
    
        public CustomAppBarBehavior() {
        }
    
        public CustomAppBarBehavior(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        @Override
        public boolean onInterceptTouchEvent(CoordinatorLayout parent, AppBarLayout child, MotionEvent ev) {
            updatedEnabled();
            return enabled && super.onInterceptTouchEvent(parent, child, ev);
        }
    
        @Override
        public boolean onStartNestedScroll(CoordinatorLayout parent, AppBarLayout child, View directTargetChild, View target, int nestedScrollAxes) {
            return enabled && super.onStartNestedScroll(parent, child, directTargetChild, target, nestedScrollAxes);
        }
    
        @Override
        public boolean onNestedFling(CoordinatorLayout coordinatorLayout, AppBarLayout child, View target, float velocityX, float velocityY, boolean consumed) {
            return enabled && super.onNestedFling(coordinatorLayout, child, target, velocityX, velocityY, consumed);
        }
    
        private void updatedEnabled() {
            enabled = false;
            if(recyclerView != null) {
                RecyclerView.Adapter adapter = recyclerView.getAdapter();
                if (adapter != null) {
                    int count = adapter.getItemCount();
                    RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
                    if (layoutManager != null) {
                        int lastItem = 0;
                        if (layoutManager instanceof LinearLayoutManager) {
                            LinearLayoutManager linearLayoutManager = (LinearLayoutManager) layoutManager;
                            lastItem = Math.abs(linearLayoutManager.findLastCompletelyVisibleItemPosition());
                        } else if (layoutManager instanceof StaggeredGridLayoutManager) {
                            StaggeredGridLayoutManager staggeredGridLayoutManager = (StaggeredGridLayoutManager) layoutManager;
                            int[] lastItems = staggeredGridLayoutManager.findLastCompletelyVisibleItemPositions(new int[staggeredGridLayoutManager.getSpanCount()]);
                            lastItem = Math.abs(lastItems[lastItems.length - 1]);
                        }
                        enabled = lastItem < count - 1;
                    }
                }
            }
        }
    
        public void setRecyclerView(RecyclerView recyclerView) {
            this.recyclerView = recyclerView;
        }
    }
    

    Then set the custom behavior on the app bar layout

    appBarBehavior = new CustomAppBarBehavior();
    CoordinatorLayout.LayoutParams appBarLayoutParams = (CoordinatorLayout.LayoutParams) appBarLayout.getLayoutParams();
    appBarLayoutParams.setBehavior(appBarBehavior);
    appBarLayout.setLayoutParams(appBarLayoutParams);
    

    Last on page change of the view pager updated the RecyclerView on the behavior

    private ViewPager.OnPageChangeListener pageChangeListener = new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { }
    
            @Override
            public void onPageSelected(final int position) {             
                appBarLayout.setExpanded(true, true);
                appBarLayout.post(new Runnable() {
                    @Override
                    public void run() {
                        appBarBehavior.setRecyclerView(childFragments.get(position).getRecyclerView());
                    }
                });
            }
    
            @Override
            public void onPageScrollStateChanged(int state) { }
        };
    

    This should work with changing datasets.

提交回复
热议问题