[英]Scroll on item inserted in RecyclerView
I've made a RecylcerView
(A) composed by ViewHolder
containing another RecyclerView
(B) . 我已经制作了一个由
ViewHolder
组成的RecylcerView
(A) , ViewHolder
包含另一个RecyclerView
(B) 。 When an item is inserted/modified ( notifyItemInserted()
/ notifyItemChanged()
) in B I want A to scroll until the B item just inserted/modified appears on screen. 当插入/修改B中的项目(
notifyItemInserted()
/ notifyItemChanged()
)时,我希望A滚动直到屏幕上刚插入/修改的B项目出现。
I've got the position of B in A and with that I get B and I retrive the last element, which is the one just inserted. 我在A中拥有B的位置,因此我得到了B,并且检索了最后一个元素,即刚刚插入的元素。 When I call the findViewHolderForAdapterPosition I get
NullPointerExeption
. 当我调用findViewHolderForAdapterPosition时,我得到了
NullPointerExeption
。
My code: 我的代码:
RecyclerView B = ((MyAdapter.MyViewHolder)A
.findfindViewHolderForAdapterPosition(posB))
.getRecyclerViewB();
B.scrollToPosition(B.getAdapter().getItemCount()-1);
What am I doing wrong? 我究竟做错了什么? Is this method right?
这种方法对吗?
If this position is (far) beyond displayed area it will not have bound ViewHolder
. 如果此位置(远)超出显示区域,则它将没有绑定
ViewHolder
。 Just use RecyclerView.scrollToPosition(int position)
here. 只需在这里使用
RecyclerView.scrollToPosition(int position)
。
Like A.scrollToPosition(positionOfB)
像
A.scrollToPosition(positionOfB)
I use the layout manager on my Recycler view 我在“回收站”视图上使用布局管理器
fun setData(listItems: List<ItemDataModel>) {
customAdapter.clear()
customAdapter.addAll(listItems)
(layoutManager as? LinearLayoutManager)?.scrollToPositionWithOffset(customAdapter.itemCount - 1, 0)
}
-----Custom Adapter methods ---- -----自定义适配器方法----
fun clear() {
items.clear()
notifyDataSetChanged()
}
fun addAll(items: List<T>) {
this.items.addAll(items)
notifyItemRangeInserted(this.items.lastIndex, items.size)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.