滚动NestedScrollView后,RecyclerView内的onClick方法无法正常工作

问题描述 投票:0回答:1

我检查了this stackoverflow question,因为它非常相似,但Google的错误已在当前版本中得到修复,但我仍然遇到了问题。

我在NestedScrollView中有一个RecyclerView,在NestedScrollView滚动后,如果我点击RecyclerView中的项目,onClick方法不能正常工作。

谁能帮我?谢谢

android android-recyclerview android-nestedscrollview
1个回答
1
投票

哦,我找到了解决方案here,我们需要:

public class FixAppBarLayoutBehavior extends AppBarLayout.Behavior {

public FixAppBarLayoutBehavior() {
    super();
}

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

@Override
public void onNestedScroll(CoordinatorLayout coordinatorLayout, AppBarLayout child, View target,
                           int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed, int type) {
    super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed,
            dxUnconsumed, dyUnconsumed, type);
    stopNestedScrollIfNeeded(dyUnconsumed, child, target, type);
}

@Override
public void onNestedPreScroll(CoordinatorLayout coordinatorLayout, AppBarLayout child,
                              View target, int dx, int dy, int[] consumed, int type) {
    super.onNestedPreScroll(coordinatorLayout, child, target, dx, dy, consumed, type);
    stopNestedScrollIfNeeded(dy, child, target, type);
}

private void stopNestedScrollIfNeeded(int dy, AppBarLayout child, View target, int type) {
    if (type == ViewCompat.TYPE_NON_TOUCH) {
        final int currOffset = getTopAndBottomOffset();
        if ((dy < 0 && currOffset == 0)
                || (dy > 0 && currOffset == -child.getTotalScrollRange())) {
            ViewCompat.stopNestedScroll(target, ViewCompat.TYPE_NON_TOUCH);
        }
    }
}

}

并且,在我们的AppBarLayout中:

        <android.support.design.widget.AppBarLayout>
         ...
        app:layout_behavior="your.package.FixAppBarLayoutBehavior" 
         ... 
        </android.support.design.widget.AppBarLayout>
© www.soinside.com 2019 - 2024. All rights reserved.