Android 如何将折叠工具栏与列表视图而不是回收器视图一起使用
有人知道如何使用listview而不是recycler视图实现折叠工具栏吗?要使其正常工作,您应该:Android 如何将折叠工具栏与列表视图而不是回收器视图一起使用,android,listview,android-collapsingtoolbarlayout,Android,Listview,Android Collapsingtoolbarlayout,有人知道如何使用listview而不是recycler视图实现折叠工具栏吗?要使其正常工作,您应该: 在自定义ListView实现中实现NestedScrollingChild 添加字段private final NestedScrollingChildHelper mScrollingChildHelper并在构造函数中初始化它 从NestedScrollingChild委托给it方法 调用setNestedScrollingEnabled(true)在mScrollingChildHelpe
private final NestedScrollingChildHelper mScrollingChildHelper代码>并在构造函数中初始化它
mScrollingChildHelper
初始化之后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);
}
}
仅将此代码添加到项目中
它只适用于棒棒糖设备和更高版本
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
listView.setNestedScrollingEnabled(true);
listView.startNestedScroll(View.OVER_SCROLL_ALWAYS);
}
如果您想在棒棒糖前的设备上进行嵌套滚动,您可能会这样做,那么您必须使用支持库中相应的实用程序类。首先,必须将ScrollView替换为NestedScrollView。后者实现了NestedScrollingParent和NestedScrollingChild,因此它可以用作父滚动容器或子滚动容器
但是ListView不支持嵌套滚动,因此您需要将其子类化并实现NestedScrollingChild。幸运的是,支持库提供了NestedScrollingChildHelper类,因此您只需创建该类的实例,并从视图类的相应方法调用其方法。要使折叠
工具栏
,您需要使用协调布局
。并且它仅适用于ScrollingView
和NestedScrollingChild
接口<代码>回收视图实现它ListView
不是。只是想知道:你能不能编写一个自定义视图来扩展ListView并实现ScrollingView和NestedScrollingChild,然后在CollasingToolbar中使用它?好的,谢谢@Foxinsocks,我想我必须重新排列一组ListView代码以回收视图。@BenjaminScharbau让我试一下,作为演示为什么我们不能包装嵌套ScrollView?中的listview。NestedScrollView也实现了nestedscrollingchild,不是吗?