【问题标题】:recyclerView child won't be clickable after putting recyclerView in SwipeRefreshLayout将 recyclerView 放入 SwipeRefreshLayout 后,recyclerView 子项将不可点击
【发布时间】:2020-07-29 15:39:12
【问题描述】:

我有一个 recyclerView,我在 ViewHolder 类中为其子级实现了一个监听器,如下所示:

inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

        fun binding(model: NewsAdapterModel) {


            itemView.setOnClickListener {
                onClick.onClick(model.id)
            }
}

并将此代码用于我的 onBindViewHolder:

   override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.binding(items[position])
    }

这也是我的 xml 代码,其中包含 recyclerView 和 SwipeRefreshLayout:

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <data>

    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".main.news.NewsFragment">

        <ProgressBar
            android:id="@+id/loading"
            style="@style/Widget.AppCompat.ProgressBar.Horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:indeterminate="true"
            android:visibility="visible"
            app:layout_constraintTop_toTopOf="parent" />


        <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
            android:id="@+id/refresh"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/loading">

                <androidx.recyclerview.widget.RecyclerView
                    android:id="@+id/recyclerView"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    app:layout_constraintTop_toBottomOf="@+id/loading" />

        </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>


    </LinearLayout>
</layout>

我的问题是,一旦我将我的 recyclerView 放入 Swipe Refreshing 布局,recycler 视图的项目就不再可点击。当我删除滑动刷新布局时,它再次变为可点击, 我该如何解决这个问题?

【问题讨论】:

  • 尝试同时设置 swiperefresh 和 recyclerView matxh 父项
  • @Rinat,不,没有任何改变
  • 这可能听起来很愚蠢,但你永远不会知道.. 尝试将 android:clickable="false" 添加到 swiperefreshLayout

标签: android kotlin android-recyclerview swiperefreshlayout


【解决方案1】:

你应该使用 SwipeRefreshLayout 作为你的 LinearLayout 的父级,并在 setOnRefreshListener 上通知回收视图,代码如下-

主布局文件

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <data></data>

    <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
        android:id="@+id/refresh"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/loading">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            tools:context=".main.news.NewsFragment">

            <ProgressBar
                android:id="@+id/loading"
                style="@style/Widget.AppCompat.ProgressBar.Horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:indeterminate="true"
                android:visibility="visible"
                app:layout_constraintTop_toTopOf="parent" />

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/recyclerView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_constraintTop_toBottomOf="@+id/loading" />

        </LinearLayout>

    </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</layout>

这样做,您可以选择显示进度库,直到您的回收视图更新为新信息。

SwipeRefreshListener

mySwipeRefreshLayout.setOnRefreshListener(
    new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            Log.i(LOG_TAG, "onRefresh called from SwipeRefreshLayout");

            // This method performs the actual data-refresh operation.
            // The method calls setRefreshing(false) when it's finished.
            myUpdateOperation();
        }
    }
);

【讨论】:

  • 我改变了布局,但没有任何改变。我还应该提到我的问题是单击 recyclerView 子不刷新 SwipeRefreshLayout
  • 我得到了类似的结果,因为我的回收视图项目是可点击的,这是一个错误。我无法重现该问题,我在 SwipeGestureLayout 中添加了 RecycleView,它工作正常。请检查您是否在 SwipeGestureLayout 上设置了任何点击监听器。
猜你喜欢
  • 1970-01-01
  • 2023-04-04
  • 1970-01-01
  • 1970-01-01
  • 2014-09-30
  • 1970-01-01
  • 1970-01-01
  • 2015-10-31
  • 1970-01-01
相关资源
最近更新 更多