Android解決CoordinatorLayout摺疊佈局RecyclerView最後一條數據顯示不全問題

今天使用CoordinatorLayout+AppBarLayout+CollapsingToolbarLayout+RecyclerView實現摺疊佈局出現RecyclerView最後一條數據顯示不全問題,問題如下圖:
在這裏插入圖片描述
找了很多方法最後發現是app:layout_behavior="@string/appbar_scrolling_view_behavior"的問題,是因爲在頂部toolbar未摺疊情況下,下方RecyclerView或者其他佈局無法顯示到最後一項。如果想要在未摺疊情況下顯示到最後一項,需要使用自定義的behavior,代碼如下:

public class FixScrollingBehavior extends AppBarLayout.ScrollingViewBehavior {

    private AppBarLayout appBarLayout;

    public FixScrollingBehavior() {
        super();
    }

    public FixScrollingBehavior(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) {

        if (appBarLayout == null) {
            appBarLayout = (AppBarLayout) dependency;
        }

        final boolean result = super.onDependentViewChanged(parent, child, dependency);
        final int bottomPadding = calculateBottomPadding(appBarLayout);
        final boolean paddingChanged = bottomPadding != child.getPaddingBottom();
        if (paddingChanged) {
            child.setPadding(
                    child.getPaddingLeft(),
                    child.getPaddingTop(),
                    child.getPaddingRight(),
                    bottomPadding);
            child.requestLayout();
        }
        return paddingChanged || result;
    }

    private int calculateBottomPadding(AppBarLayout dependency) {
        final int totalScrollRange = dependency.getTotalScrollRange();
        return totalScrollRange + dependency.getTop();
    }
}

xml中替換app:layout_behavior="@string/appbar_scrolling_view_behavior"屬性爲如下:

app:layout_behavior="com.xx.xx.ui.widget.FixScrollingBehavior"

就能解決最後一條顯示不全問題,解決後如圖:
在這裏插入圖片描述

參考文章:https://www.jianshu.com/p/bc56afbd88f2

希望能解決你的問題^ - ^

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章