【问题标题】:Recycler View Item Click not working if its put under Swipe Refresh Layout - Android如果将 Recycler View Item Click 放在 Swipe Refresh Layout 下,则 Recycler View Item Click 不起作用 - Android
【发布时间】:2020-06-28 07:29:15
【问题描述】:

如果我将回收站视图放在滑动刷新布局中,则回收站视图项点击不起作用。添加下面的布局。

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:id="@+id/tv_no_order"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="90dp"
    android:layout_marginEnd="90dp"
    android:gravity="center_horizontal"
    android:lineSpacingExtra="6sp"
    android:text="@string/no_orders_found"
    android:textColor="#616161"
    android:textSize="16sp"
    android:visibility="gone"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<include
    android:id="@+id/progressView"
    layout="@layout/layout_progress"
    android:visibility="gone"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
    android:id="@+id/swipe_refresh_layout"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.0"
    android:clickable="false">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rcv_inventory"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clickable="true"
        android:padding="@dimen/padding_15dp"
        android:focusable="true" />
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

当我删除滑动刷新布局时,点击工作正常。如何在不移除滑动刷新布局的情况下使回收站视图可点击?

更多信息:此布局是片段的一部分,片段用作视图分页器 2 的页面。我在 Recycler 视图适配器中有两种视图类型

【问题讨论】:

    标签: android android-recyclerview onclicklistener swiperefreshlayout


    【解决方案1】:

    SwipeRefreshLayout 不应该成为RecyclerView 项目接收点击事件的拦截器,您能否也发布您的itemClickListener 实现?

    另外,考虑使用FrameLayout 来满足您的上述布局要求。

    【讨论】:

    • 谢谢,通过用框架布局替换约束布局解决了问题。
    【解决方案2】:

    使用最新稳定版的约束布局。我有同样的问题,因为我使用的是旧版本。

    dependencies {
        implementation "androidx.constraintlayout:constraintlayout:2.0.4"
    }
    

    【讨论】:

      【解决方案3】:

      尝试用LinearLayout替换ConstraintLayout

      <LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      android:layout_width="match_parent"
      android:layout_height="match_parent">
      
      <TextView
          android:id="@+id/tv_no_order"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_marginStart="90dp"
          android:layout_marginEnd="90dp"
          android:gravity="center_horizontal"
          android:lineSpacingExtra="6sp"
          android:text="@string/no_orders_found"
          android:textColor="#616161"
          android:textSize="16sp"
          android:visibility="gone"
        
       />
      
      <include
          android:id="@+id/progressView"
          layout="@layout/layout_progress"
          android:visibility="gone"
         />
      
      <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
          android:id="@+id/swipe_refresh_layout"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"   
          >
      
          <androidx.recyclerview.widget.RecyclerView
              android:id="@+id/rcv_inventory"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:padding="@dimen/padding_15dp"
              />
      </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
      
      </LinearLayout>
      

      【讨论】:

      • 谢谢,用框架布局替换了约束布局。 :)
      猜你喜欢
      • 2019-11-11
      • 1970-01-01
      • 2018-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多