Toolbar in AppBarLayout is scrollable although RecyclerView has not enough content to scroll

前端 未结 9 758
挽巷
挽巷 2020-12-12 15:34

Is it really intended that the Toolbar in a AppBarLayout is scrollable although the main container with the "appbar_scrolling_view_behavior" has not enough content

9条回答
  •  鱼传尺愫
    2020-12-12 16:11

    I've implemented it using my own Behavior class which might be attached to AppBarLayout:

    public class CustomAppBarLayoutBehavior extends AppBarLayout.Behavior {
    
    private RecyclerView recyclerView;
    private int additionalHeight;
    
    public CustomAppBarLayoutBehavior(RecyclerView recyclerView, int additionalHeight) {
        this.recyclerView = recyclerView;
        this.additionalHeight = additionalHeight;
    }
    
    public boolean isRecyclerViewScrollable(RecyclerView recyclerView) {
        return recyclerView.computeHorizontalScrollRange() > recyclerView.getWidth() || recyclerView.computeVerticalScrollRange() > (recyclerView.getHeight() - additionalHeight);
    }
    
    @Override
    public boolean onStartNestedScroll(CoordinatorLayout parent, AppBarLayout child, View directTargetChild, View target, int nestedScrollAxes) {
        if (isRecyclerViewScrollable(mRecyclerView)) {
            return super.onStartNestedScroll(parent, child, directTargetChild, target, nestedScrollAxes);
        }
        return false;
    }
    

    }

    And below is the code how to set this behavior:

    final View appBarLayout = ((DrawerActivity) getActivity()).getAppBarLayoutView();
    CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) appBarLayout.getLayoutParams();
    layoutParams.setBehavior(new AppBarLayoutNoEmptyScrollBehavior(recyclerView, getResources().getDimensionPixelSize(R.dimen.control_bar_height)));
    

提交回复
热议问题