scrollToPosition(),使用片段更新RecyclerView时

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了scrollToPosition(),使用片段更新RecyclerView时相关的知识,希望对你有一定的参考价值。

我已经创建了一个列表,该列表使用LiveData从会议室数据库中获取项目。然后使用liveData<List>将此recyclerView绑定到BindingAdapter

我需要保持滚动状态的帮助,或者需要以某种方式返回到重新加载recyclerView之前的滚动索引:

//In ViewModel
    val movieList = moviesRepository.movies

..

//in Repo
  val movies: LiveData<List<Movie>> =
    Transformations.map(database.movieDao.getMovies()) {
        it.asDomainModel()
    }

每次数据库更新时,recyclerView都会返回顶部。

这是我的ListAdapter的副本,其中具有recyclerView处理更新的大部分方式:

class MovieListAdapter(val clickListener: MovieClickListener) :
ListAdapter<Movie, MovieListAdapter.MovieListViewHolder>(DiffCallback) {

/**
 * Allows the RecyclerView to determine which items have changed when the [List]
 * has been updated.
 */
companion object DiffCallback : DiffUtil.ItemCallback<Movie>() {
    override fun areItemsTheSame(oldItem: Movie, newItem: Movie): Boolean {
        return oldItem === newItem
    }

    /**
     * Create new [RecyclerView] item views (invoked by the layout manager)
     */
    override fun areContentsTheSame(oldItem: Movie, newItem: Movie): Boolean {
        return oldItem.id == newItem.id
    }

/**
 * The MovieListViewHolder constructor takes the binding variable from the associated
 * MovieList, which nicely gives it access to the full [MovieList] information.
 */
class MovieListViewHolder(
    private var binding: MovieListItemBinding
) :
    RecyclerView.ViewHolder(binding.root) {

    fun bind(clickListener: MovieClickListener, movie: Movie) {

        binding.clickListener = clickListener
        binding.movie = movie
        // This is important, because it forces the data binding to execute immediately,
        // which allows the RecyclerView to make the correct view size measurements
        binding.executePendingBindings()
    }

    companion object {
        fun from(parent: ViewGroup): MovieListViewHolder {
            val layoutInflater = LayoutInflater.from(parent.context)
            val binding = MovieListItemBinding.inflate(layoutInflater, parent, false)
            return MovieListViewHolder(binding)
        }
    }
}


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

    val movie = getItem(position)
    holder.itemView.setOnClickListener {
        clickListener.onClick(movie)
    }
    holder.bind(clickListener, movie)
}

/**
 * Replaces the contents of a view (invoked by the layout manager)
 */
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int):
        MovieListViewHolder {
    return MovieListViewHolder.from(parent)
}

}

这是bindingAdapter和列表的RecyclerView

@BindingAdapter("listData")
fun bindRecyclerView(recyclerView: RecyclerView, data: List<Movie>?) {
    val adapter = recyclerView.adapter as MovieListAdapter
    //Log.d("listData binding", "${data}")
    adapter.submitList(data)
}

我想我需要使用类似recyclerView.smoothScrollToPosition(la.getItemCount());更新发生后,但是我不知道更新发生后如何自动调用它]

recyclerView.smoothScrollToPosition(la.getItemCount());

我列出了一个列表,该列表使用LiveData从Room数据库中获取项目。然后,使用BindingAdapter将此liveData 绑定到recyclerView。我需要保持滚动状态或...

答案
    首先不使用Project Repo ListAdapter具有更优化的适配器RecyclerView
另一答案
None

以上是关于scrollToPosition(),使用片段更新RecyclerView时的主要内容,如果未能解决你的问题,请参考以下文章

在 RecyclerView 中,scrollToPosition 和 smoothScrollToPosition 有啥区别?

Android Leanback 库 Horizo​​ntalGridView scrollToPosition 不起作用

想要使用 SharePreferences 更新第二个片段中的数据,但第二个片段没有更新

如何更新所有片段? onProgressUpdate 不更新所有片段

在常规(支持)片段中使用 AsyncTaskLoader 更新适配器不更新 ListView

如何在使用片段和计时器的选项卡式活动上更新 UI