当你使用RecyclerView
创建动态列表时,你的目标是向用户显示所需的项目,而不是一次显示所有的项目,这会导致等待用户看到列表。但我最近注意到,当你把RecyvlerView
内ScrollView
或NestedScrollView
deveice等待recyclerview插入所有项目,然后显示recyclerview给用户,我怀疑recyclerView不回收项目,使用户等待近1.5秒。
**问题:**我如何将Recyclerview
放入NestedScrollView
中,并使recyclerview正常行为,即回收项目不会一次全部显示。
RecyclerView实现代码:
fragment_translate_from.xml代码:
<?xml version="1.0" encoding="utf-8"?>
<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.coordinatorlayout.widget.CoordinatorLayout
android:id="@+id/main_layout_translate_from"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".features.translate_from_from.ui.translate_fromFromFragment">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/translate_from_action_bar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:elevation="@dimen/_20sdp"
android:fitsSystemWindows="true">
<androidx.appcompat.widget.Toolbar
android:id="@+id/translate_from_tool_bar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_gravity="center"
android:layout_marginStart="@dimen/_minus7sdp"
android:elevation="@dimen/_10sdp"
android:gravity="center"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:layout_collapseMode="pin"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/tool_bar_layout"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layoutDirection="rtl">
<TextView
android:id="@+id/tv_translate_from_title"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_centerVertical="true"
android:fontFamily="@font/per_vazir_regular_font"
android:gravity="center"
android:text="@string/translate_From_title"
android:textColor="?android:textColorPrimary"
android:textSize="@dimen/_17ssp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/iv_translate_from_search"
app:layout_constraintStart_toEndOf="@+id/iv_translate_from_back_arrow"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.imageview.ShapeableImageView
android:id="@+id/iv_translate_from_back_arrow"
android:layout_width="?android:actionBarSize"
android:layout_height="?android:actionBarSize"
android:layout_marginStart="@dimen/_2sdp"
android:background="@drawable/back_arrow_ripple_effect"
android:clickable="true"
android:scaleType="centerInside"
android:src="@drawable/baseline_arrow_back_24"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/til_search"
android:layout_width="0dp"
android:layout_height="0dp"
android:visibility="gone"
app:endIconDrawable="@drawable/baseline_close_24"
app:endIconMode="clear_text"
app:hintEnabled="false"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/iv_translate_from_back_arrow"
app:layout_constraintTop_toTopOf="parent"
app:startIconDrawable="@drawable/baseline_search_24">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/et_search_input"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/transparent"
android:backgroundTint="@color/transparent"
android:hint="@string/languages_search_str"
android:paddingStart="@dimen/_20sdp"
android:paddingEnd="@dimen/_20sdp"
android:textColor="@color/enter_text_color"
android:textColorHint="@color/enter_text_color"
android:textCursorDrawable="@drawable/edit_text_cursor_drawable"
android:textSize="@dimen/_12ssp" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.imageview.ShapeableImageView
android:id="@+id/iv_translate_from_search"
android:layout_width="?android:actionBarSize"
android:layout_height="?android:actionBarSize"
android:layout_marginStart="@dimen/_2sdp"
android:background="@drawable/back_arrow_ripple_effect"
android:clickable="true"
android:scaleType="centerInside"
android:src="@drawable/baseline_search_24"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/views_layout_translate_from"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<androidx.core.widget.NestedScrollView
android:id="@+id/views_scroll_views"
android:layout_width="0dp"
android:layout_height="0dp"
android:fillViewport="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/inner_views_layout_translate_from"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tv_all_languages"
android:layout_width="0dp"
android:layout_height="@dimen/_40sdp"
android:gravity="top|start"
android:paddingStart="@dimen/_10sdp"
android:paddingTop="@dimen/_10sdp"
android:paddingEnd="@dimen/_10sdp"
android:text="@string/all_languages_str"
android:textColor="?android:textColorPrimary"
android:textSize="@dimen/_14ssp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/all_langs_recycler_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:nestedScrollingEnabled="false"
android:orientation="vertical"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tv_all_languages" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
片段中的RecyclerView:
private fun setAllLanguagesRecyclerView() {
lifecycleScope.launch(Dispatchers.IO) {
languagesList = viewModel.getAllLanguagesName().toMutableList()
val layoutManager = LinearLayoutManager(requireContext(), LinearLayoutManager.VERTICAL, false)
val position = viewModel.getAllSelectedPosition()
val adapter = AllLanguagesAdapter { allLangModel ->
viewModel.insertRecentLanguages(allLangModel)
sendBackValueToPreviousBackStack(allLangModel.langCode)
}
binding.allLangsRecyclerView.layoutManager = layoutManager
adapter.submitList(languagesList)
withContext(Dispatchers.Main) {
binding.allLangsRecyclerView.adapter = adapter
setBackgroundToLanguagesRecyclerViewItem(
recyclerView = binding.allLangsRecyclerView,
position = position
)
setAdiveryNativeAd(adapter)
setSearch(adapter)
}
}
}
所有语言适配器
class AllLanguagesAdapter constructor(private val langOnClick: (AllLangModel) -> Unit) :
ListAdapter<AllLangModel, AllLanguagesAdapter.ViewHolder>(AllLanguagesDiffCallBack) {
class ViewHolder constructor(
private val binding: TranslateFromLanguageItemBinding,
private val langOnClick: (AllLangModel) -> Unit
) : RecyclerView.ViewHolder(binding.root) {
private var currentLangModel: AllLangModel? = null
val innerBinding: TranslateFromLanguageItemBinding = binding
init {
itemView.setOnClickListener {
currentLangModel?.let { allLangModel ->
langOnClick(allLangModel)
}
}
}
fun bind(langModel: AllLangModel) {
if (langModel.langCode.equals("en", true)){
langModel.downStatus = true
}
this.currentLangModel = langModel
binding.langModel = langModel
}
}
override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int
): ViewHolder {
val inflate = LayoutInflater.from(parent.context)
val binding: TranslateFromLanguageItemBinding =
DataBindingUtil.inflate(inflate, R.layout.translate_from_language_item, parent, false)
return ViewHolder(binding, langOnClick)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val langModel: AllLangModel = getItem(position)
holder.bind(langModel = langModel)
}
}
object AllLanguagesDiffCallBack : DiffUtil.ItemCallback<AllLangModel>() {
override fun areItemsTheSame(oldItem: AllLangModel, newItem: AllLangModel): Boolean {
return oldItem == newItem
}
override fun areContentsTheSame(
oldItem: AllLangModel,
newItem: AllLangModel
): Boolean {
return oldItem.hashCode() == newItem.hashCode()
}
}
2条答案
按热度按时间yqyhoc1h1#
在NestedScrollView中使用recycler视图并将此属性添加到recylerview。
这将管理其余的事情
aor9mmx12#
您的
RecyclerView
正在显示其所有内容,因为您已将其高度设置为wrap_content
。您需要限制其高度,以便在内容大于该高度时获得滚动行为。(特别是,滚动系统是您获得RecyclerView
的好处的地方,因为它交换了一个视图池来显示列表的可见部分,而不是为每个项目创建视图。因此,您可能应该为
RecyclerView
或其父ConstraintLayout
设置高度,同时允许RecyclerView
填充可用空间。但是,为什么首先要将RecyclerView
放在ScrollView
中呢?它们都是ScrollingView
,而RecyclerView
已经在处理它的特定用例--您不需要将它 Package 在另一个滚动器中。如果是因为你想让
TextView
滚动到视图之外,比如当你滚动到它出现的列表的顶部时,你可能会更容易地把它显示在RecyclerView
本身内部,作为第一个项目。也许使用它自己的ViewHolder
类型,或者可能使用ConcatAdapter
来拥有一个显示该文本的Adapter,然后是“真实的的”适配器,因此您可以按原样使用后者,而无需添加逻辑来解释额外的虚拟项。