SwipeRefreshLayout阻止嵌套RecyclerView中的项目的onClickCallback

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

我有一个带有嵌套RecyclerView的SwipeRefreshLayout。回收器视图中的每个项目本质上都是一个附有onClickHandler的CardView。我有一个问题,如果将回收站视图滚动到最顶部,则不会调用clickHandler。如果我向下滚动一个像素,则可以单击内容。

在触摸事件和单击事件上添加打印输出语句后,似乎刷卡刷新布局也会拦截触摸事件和setTargetOffsetTopAndBottom。这将调用requestLayout,最终似乎导致了ACTION_CANCEL事件。如果我向下滚动浏览回收站视图,则1px canChildScrollUp()将返回true,并通过SwipRefreshLayout停止requestLayout调用。

// SwipeRefreshLayout.java
public boolean onInterceptTouchEvent(MotionEvent ev) {
    ...
    if (!isEnabled() || mReturningToStart || canChildScrollUp()
            || mRefreshing || mNestedScrollInProgress) {
        // Fail fast if we're not in a state where a swipe is possible
        return false;
    }
    ...
    switch (action) {
        case MotionEvent.ACTION_DOWN:
            setTargetOffsetTopAndBottom(mOriginalOffsetTop - mCircleView.getTop());
            mActivePointerId = ev.getPointerId(0);
            mIsBeingDragged = false;

            pointerIndex = ev.findPointerIndex(mActivePointerId);
            if (pointerIndex < 0) {
                return false;
            }
            mInitialDownY = ev.getY(pointerIndex);
            break;
    ...

布局

<?xml version="1.0" encoding="utf-8"?>
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/ticket_wallet_refresh"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/ticket_wallet_recycler"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:itemCount="3"
        tools:listheader="@layout/ticket_header_item"
        tools:listitem="@layout/active_ticket_item" />
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

ViewHolder

sealed class FooViewHolder(view: View) : RecyclerView.ViewHolder(view) {

    abstract fun bind(item: WalletItem)

    class ActiveFooViewHolder(
        view: View,
        private val remainingTimeFormatter: RemainingTimeFormatter,
        private val callback: (Int) -> Unit
    ) : FooViewHolder(view) {

        init {
            view.setOnClickListener {
                callback(adapterPosition)
            }
   ...

我的编译目标是Android API lvl 29。

android android-recyclerview onclick swiperefreshlayout
1个回答
0
投票

我一直遇到同样的问题。我确实有一个ConstraintLayout包装了SwipeRefreshLayout。当我卸下ConstraintLayout(用LinearLayout代替)时,问题已解决。希望能有所帮助。我不知道为什么它起作用。

© www.soinside.com 2019 - 2024. All rights reserved.