如何使用ListView而不是Recycler视图使用Collapsing ToolBar

时间:2015-08-31 00:48:22

标签: android listview android-collapsingtoolbarlayout

有没有人知道如何使用listview而不是回收者视图来实现折叠工具栏?

3 个答案:

答案 0 :(得分:4)

为了让它成为你应该:

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

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

  3. 从NestedScrollingChild委派给它的方法

  4. setNestedScrollingEnabled(true);初始化

  5. 后调用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);
        }
    }
    

答案 1 :(得分:0)

仅将此代码添加到项目中。
它只能在Lollipop设备中使用,以后再使用。

about:config

答案 2 :(得分:0)

如果您想在棒棒糖之前的设备上进行嵌套滚动工作,则必须使用支持库中的相应实用程序类。首先,您必须用NestedScrollView替换ScrollView。后者同时实现了NestedScrollingParent和NestedScrollingChild,因此可以用作父级或子级滚动容器。

但是ListView不支持嵌套滚动,因此您需要将其子类化并实现NestedScrollingChild。幸运的是,支持库提供了NestedScrollingChildHelper类,因此您只需创建该类的实例并从视图类的相应方法中调用其方法即可。