更新LiveData RecyclerView时保持滚动级别

问题描述 投票:0回答:1

我已经创建了一个列表,该列表使用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。我需要保持滚动状态或...

android kotlin android-recyclerview android-livedata
1个回答
0
投票
    首先不使用Project Repo ListAdapter具有更优化的适配器RecyclerView
© www.soinside.com 2019 - 2024. All rights reserved.