【问题标题】:onClick method inside RecyclerView not working after NestedScrollView scrolled在 NestedScrollView 滚动后,RecyclerView 中的 onClick 方法不起作用
【发布时间】:2017-12-21 12:00:41
【问题描述】:

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

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

谁能帮帮我?谢谢

【问题讨论】:

    标签: android android-recyclerview android-nestedscrollview


    【解决方案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>
    

    【讨论】:

    • 谢谢!你拯救了我的一天!
    • 我没有应用栏布局。有什么选择?
    【解决方案2】:

    你的RecyclerView 不应该允许嵌套滚动,所以它必须有nestedScrollingEnabled="false"

    <androidx.recyclerview.widget.RecyclerView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:nestedScrollingEnabled="false"/>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-28
      • 2017-02-05
      • 1970-01-01
      • 2016-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-01
      相关资源
      最近更新 更多