首页 > 解决方案 > recyclerview 项目内的滚动视图不起作用

问题描述

我正在使用 Recycler 视图来显示一些卡片视图。回收站视图的每一项都包含一个长文本。我正在尝试使该文本可滚动。我尝试将文本包装在嵌套滚动视图中。但是在滑动项目时,会滚动整个回收站视图,而不是项目内的文本。

我的回收站视图项目布局的更简单版本。

 <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/parent"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:onClick="@{()->viewHolder.navigateToDetails()}"
            android:paddingHorizontal="25dp"
            android:paddingVertical="15dp">

           <androidx.core.widget.NestedScrollView
                android:id="@+id/crib_text_scroll_view"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                app:layout_constrainedHeight="true"
                app:layout_constraintHeight_max="300dp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/category">
                
                <com.example.presentation.custom_views.ReadMoreTextView
                    android:id="@+id/crib_text"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="5dp"
                    app:mainText="@{cribResponse.post}"
                    tools:mainText="@tools:sample/lorem[20]" />

            </androidx.core.widget.NestedScrollView>
 </androidx.constraintlayout.widget.ConstraintLayout>

包含回收站视图的片段布局

<androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
            android:id="@+id/swipe_refresh_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/crib_rv"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:nestedScrollingEnabled="true"
             app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
                tools:listitem="@layout/item_text_crib" />

        </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
    </androidx.coordinatorlayout.widget.CoordinatorLayout>

适配器类

class CribAdapter(private val rvHost: RVHost) :
    PagingDataAdapter<CribResponse, CribViewHolder>(diffCallback) {
    private val allAudioViewHolders: MutableList<AudioCribViewHolder> = mutableListOf()
    var playingViewHolder: AudioCribViewHolder? = null

    companion object {
        val diffCallback = object : DiffUtil.ItemCallback<CribResponse>() {
            override fun areItemsTheSame(oldItem: CribResponse, newItem: CribResponse): Boolean {
                return oldItem == newItem
            }

            override fun areContentsTheSame(oldItem: CribResponse, newItem: CribResponse): Boolean {
                return oldItem.postId == newItem.postId
            }
        }
    }

    override fun onBindViewHolder(holder: CribViewHolder, position: Int) {
        val item = getItem(position)
        item ?: return
        holder.bind(item)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CribViewHolder {
        return if (viewType == AUDIO_CRIB) {
            AudioCribViewHolder(parent = parent, RVHost = rvHost).apply {
                markCreated()
                allAudioViewHolders.add(this)
            }
        } else {
            TextCribViewHolder(parent = parent, rvHost = rvHost)
        }
    }

    override fun getItemViewType(position: Int): Int {
        val item = getItem(position)
        val audio = item?.postAudio
        return if (audio == null) TEXT_CRIB else AUDIO_CRIB
    }

    override fun onViewAttachedToWindow(holder: CribViewHolder) {
        super.onViewAttachedToWindow(holder)
        if (holder is AudioCribViewHolder) {
            holder.markAttach()
        }
    }

    override fun onViewDetachedFromWindow(holder: CribViewHolder) {
        super.onViewDetachedFromWindow(holder)
        if (holder is AudioCribViewHolder) {
            val isPlaying = holder.playing.value ?: false
            if (isPlaying) {
                holder.resetUI()
                playingViewHolder = null
                rvHost.stop()
            }
            holder.markDetach()
        }
    }

    fun selfLifecycleDestroyed() {
        allAudioViewHolders.forEach {
            it.markDestroyed()
        }
    }

    fun getCrib(position: Int): CribResponse? {
        return getItem(position)
    }
}

标签: androidandroid-recyclerviewandroid-nestedscrollview

解决方案


推荐阅读