简体   繁体   English

滚动到RecyclerView中插入的项目

[英]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.

相关问题 通知适配器插入 RoomDatabase 中的项目并更新 recyclerView - Notify adapter on item inserted in RoomDatabase and update the recyclerView 插入项目时 RecyclerView 行闪烁/闪烁 - RecyclerView row flashes/blinks when item inserted 聚焦/滚动回收站查看最后一个项目 - Focus/Scroll recyclerView to last Item 滚动到回收站视图中下一个项目的顶部 - Scroll to top of next item in a recyclerview Android RecyclerView滚动和位置项位于顶部 - Android RecyclerView scroll and position item at top 在 Recyclerview 中,如何将初始滚动位置更改为第一项的顶部? - In Recyclerview, how to change initial scroll position to the top of first item? LinearLayout 和 NestedScrollView 中的 RecyclerView - 如何滚动到某个位置的项目顶部? - RecyclerView inside LinearLayout and NestedScrollView - How to scroll to top of item on a certain position? recyclerView 中只显示 1 个项目,它甚至没有向下滚动 - Only 1 item is showed in recyclerView and it doesn't even scroll down 每次将项目添加到列表时如何使RecyclerView不会滚动到底部 - How to make RecyclerView not scroll to the bottom each time an item is added to the list 如何在RecyclerView中执行从顶部到某个位置的滚动项目? - How in RecyclerView perform scroll item with some position to top?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM