如何使用带有 ListView 而不是 Recycler View 的折叠工具栏

How to Use Collapsing ToolBar with a ListView instead of a Recycler View

有谁知道如何使用列表视图而不是回收器视图来实现折叠工具栏?

要让它正常工作,您应该:

  1. 在您的自定义 ListView 实现中实现 NestedScrollingChild。

  2. 添加字段 private final NestedScrollingChildHelper mScrollingChildHelper; 并在构造函数中初始化它

  3. 将 NestedScrollingChild 的方法委托给它

  4. mScrollingChildHelper 初始化后调用 setNestedScrollingEnabled(true);

例如,这是我的列表视图实现:

public class NestedScrollingListView extends ListView implements NestedScrollingChild {

    private final NestedScrollingChildHelper mScrollingChildHelper;

    public NestedScrollingListView(Context context) {
       super(context);
       mScrollingChildHelper = new NestedScrollingChildHelper(this);
       setNestedScrollingEnabled(true);
    }

    public NestedScrollingListView(Context context, AttributeSet attrs) {
       super(context, attrs);
       mScrollingChildHelper = new NestedScrollingChildHelper(this);
       setNestedScrollingEnabled(true);
    }

    @Override
    public void setNestedScrollingEnabled(boolean enabled) {
       mScrollingChildHelper.setNestedScrollingEnabled(enabled);
    }

    @Override
    public boolean isNestedScrollingEnabled() {
       return mScrollingChildHelper.isNestedScrollingEnabled();
    }

    @Override
    public boolean startNestedScroll(int axes) {
       return mScrollingChildHelper.startNestedScroll(axes);
    }

    @Override
    public void stopNestedScroll() {
        mScrollingChildHelper.stopNestedScroll();
    }

    @Override
    public boolean hasNestedScrollingParent() {
        return mScrollingChildHelper.hasNestedScrollingParent();
    }

    @Override
    public boolean dispatchNestedScroll(int dxConsumed, int dyConsumed, int dxUnconsumed,
                                    int dyUnconsumed, int[] offsetInWindow) {
        return mScrollingChildHelper.dispatchNestedScroll(dxConsumed, dyConsumed,
            dxUnconsumed, dyUnconsumed, offsetInWindow);
    }

    @Override
    public boolean dispatchNestedPreScroll(int dx, int dy, int[] consumed, int[] offsetInWindow) {
        return mScrollingChildHelper.dispatchNestedPreScroll(dx, dy, consumed, offsetInWindow);
    }

    @Override
    public boolean dispatchNestedFling(float velocityX, float velocityY, boolean consumed) {
        return mScrollingChildHelper.dispatchNestedFling(velocityX, velocityY, consumed);
    }

    @Override
    public boolean dispatchNestedPreFling(float velocityX, float velocityY) {
        return mScrollingChildHelper.dispatchNestedPreFling(velocityX, velocityY);
    }
}

仅将此代码添加到您的项目中。
它仅适用于 Lollipop 设备及更高版本。

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    listView.setNestedScrollingEnabled(true);
    listView.startNestedScroll(View.OVER_SCROLL_ALWAYS);
}

如果您想在 Lollipop 之前的设备上实现嵌套滚动(您可能会这样做),则必须使用支持库中的相应实用程序 classes。首先,您必须将 ScrollView 替换为 NestedScrollView。后者同时实现了 NestedScrollingParent 和 NestedScrollingChild,因此它可以用作父滚动容器或子滚动容器。

但是ListView不支持嵌套滚动,所以需要subclass实现NestedScrollingChild。幸运的是,Support 库提供了 NestedScrollingChildHelper class,所以你只需要创建这个 class 的实例并从视图的相应方法中调用它的方法 class.