首页 > 解决方案 > 刷新数据后,RecyclerView 似乎在屏幕上向下移动

问题描述

使用 LiveData<PagedList> 显示命中 Room 数据库的回收器视图。当我在屏幕上进行搜索时,返回的项目数量较少,但是,回收站视图和其他按钮之间似乎有很多额外的空白。回收站视图本身会缩小,然后在屏幕上向下移动。

这是刷新数据之前的样子(在我进行搜索之前):

普通的

这是之后:

空白

这是我的 recyclerView 适配器:

class WorkPackagesRecyclerAdapter(
  private val onWorkPackageClickListener: OnClickWorkPackage
   ) : PagedListAdapter<Workpackage, 
 WorkPackagesRecyclerAdapter.WorkPackagesViewHolder>(
  REPO_COMPARATOR
) {


interface OnClickWorkPackage {
    fun clickWorkPackage(workPackageId: String)
}

override fun onCreateViewHolder(viewGroup: ViewGroup, i: Int): WorkPackagesViewHolder {
    val inflater = LayoutInflater.from(viewGroup.context)
    val binding = inflate<ItemWorkPackageBinding>(
        inflater,
        R.layout.item_work_package,
        viewGroup,
        false
    )
    return WorkPackagesViewHolder(binding, viewGroup.context)
}


override fun onBindViewHolder(holder: WorkPackagesViewHolder, position: Int) 
{

    getItem(position)?.let {
        holder.bind(it)
    }

}

inner class WorkPackagesViewHolder(
    internal var binding: ItemWorkPackageBinding,
    internal val context: Context
) : RecyclerView.ViewHolder(binding.root), KoinComponent {

    fun bind(data: Workpackage) {
        val itemWorkPackagesViewModel: ItemWorkPackagesViewModel by inject{ parametersOf(data)}
        this.binding.listItem.setOnClickListener {
            onWorkPackageClickListener.clickWorkPackage(data.id)
        }
        this.binding.viewmodel = itemWorkPackagesViewModel
        this.binding.executePendingBindings()
    }

}

companion object {
    private val REPO_COMPARATOR =
        object : DiffUtil.ItemCallback<Workpackage>() {
            override fun areItemsTheSame(
                oldItem: Workpackage,
                newItem: Workpackage
            ): Boolean =
                oldItem.id == newItem.id

            override fun areContentsTheSame(
                oldItem: Workpackage,
                newItem: Workpackage
            ): Boolean =
                oldItem == newItem
        }


}

以下是我设置 PagedList 的方法:

val searchQuery: MutableLiveData<SearchAndSort> = MutableLiveData(
    SearchAndSort("",
        WorkpackagesRepository.Companion.SortedBy.WorkPackageNumber,
        AscendDescend.ASC))

var workPackagesList = Transformations.switchMap(searchQuery) { searchQuery ->
    val factory = workPackageStorageDao.searchWorkpackages(
        searchQuery.searchText,
        searchQuery.sortBy.type + " " + searchQuery.ascendDescend.text
    )
    val pagedListBuilder = LivePagedListBuilder<Int, Workpackage>(factory, pagingLimit)
    pagedListBuilder.build()
}

这是我观察适配器的地方:

    workPackagesViewModel.workPackagesList.observe(this, Observer { wpList ->
        wpList ?: return@Observer

        adapter = WorkPackagesRecyclerAdapter(this)
        adapter.submitList(wpList)
        binding.workPackagesRecyclerView.adapter = adapter
        adapter.notifyDataSetChanged()

    })

这是回收站视图的布局

<?xml version="1.0" encoding="utf-8"?>
<layout
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<data>

    <variable
        name="viewModel"
        type="com.bechtel.pf.ui.workpackages.WorkPackagesViewModel" />
</data>

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="727dp"
    tools:layout_editor_absoluteY="1dp">

    <EditText
        android:id="@+id/search_bar_edit_text"
        android:layout_width="match_parent"
        android:layout_height="55dp"
        android:layout_marginTop="@dimen/work_package_margin"
        android:hint="@string/work_packages_search_hint"
        app:layout_constraintBottom_toBottomOf="@id/guideline"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        />

    <ImageButton
        android:id="@+id/search_bar_magnifying_glass"
        android:layout_width="32dp"
        android:layout_height="32dp"
        android:layout_marginTop="@dimen/work_package_margin"
        android:background="@drawable/icons_search"
        app:layout_constraintBottom_toBottomOf="@id/guideline"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:orientation="horizontal"
        app:layout_constraintGuide_begin="@dimen/work_package_search_guideline_start"

        />
    <com.google.android.material.button.MaterialButton
        android:id="@+id/btnSortBy"
        android:layout_width="0dp"
        android:layout_height="45dp"
        android:layout_marginTop="5dp"
        android:layout_marginStart="10dp"
        android:layout_marginEnd="10dp"
        android:backgroundTint="@color/colorPrimary"
        app:layout_constraintEnd_toStartOf="@+id/btnAscendDescend"
        app:layout_constraintHorizontal_weight="1"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/guideline"
        app:layout_constraintHorizontal_chainStyle="spread"
        android:text="@string/work_packages_sort"
        />

    <com.google.android.material.button.MaterialButton
        android:id="@+id/btnAscendDescend"
        android:layout_width="0dp"
        android:layout_height="45dp"
        android:layout_marginTop="5dp"
        android:layout_marginEnd="10dp"
        android:backgroundTint="@color/colorPrimary"
        app:layout_constraintHorizontal_weight="1"
        app:layout_constraintEnd_toStartOf="@+id/btnFilterBy"
        app:layout_constraintStart_toEndOf="@+id/btnSortBy"
        app:layout_constraintTop_toBottomOf="@+id/guideline"
        app:layout_constraintHorizontal_chainStyle="spread"
        android:text="@string/work_packages_ascend"
        android:onClick="@{() -> viewModel.ascendingDescending()}"
        />

    <com.google.android.material.button.MaterialButton
        android:id="@+id/btnFilterBy"
        android:layout_width="0dp"
        android:layout_height="45dp"
        android:layout_marginTop="5dp"
        android:layout_marginEnd="10dp"
        android:backgroundTint="@color/colorPrimary"
        app:layout_constraintHorizontal_weight="1"
        app:layout_constraintHorizontal_chainStyle="spread"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/btnAscendDescend"
        app:layout_constraintTop_toBottomOf="@+id/guideline"
        android:text="@string/work_packages_filter"/>

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/workPackagesRecyclerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="vertical"
        android:layout_marginTop="75dp"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/btnFilterBy">

    </androidx.recyclerview.widget.RecyclerView>


</androidx.constraintlayout.widget.ConstraintLayout>

以前有人见过这样的事情吗?我还确保我在我的 recyclerview 布局和 item.xml 文件中使用了 layout_height = wrap_content。

截图检查器:

布局检查器

标签: androidandroid-recyclerviewandroid-livedatapagedlist

解决方案


好的,看起来我已经将 recyclerview layout_constraintBottom_toBottomOf 设置为“parent”,并且由于 recyclerview 的总高度在项目数量缩小时缩小了,它正在将视图移动到屏幕底部。我还可以摆脱顶部边距属性,并且回收站视图保持在按钮下方。所以现在我有:

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/workPackagesRecyclerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="vertical"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/btnFilterBy"/>

一切看起来都不错!


推荐阅读