简体   繁体   中英

How to get last item view from RecyclerView after adding new item and notifyDataSetChanged?

Not able to get last item view after adding new item into adapter and notifyDataSetChanged .

Getting null item view when I am going to get last item view from RecyclerView after calling notifyDataSetChanged().

Following some scenario for getting item view object but not able to get exact view object but size and item count is correct.

Scenario 1:

mListItem.add("Hello Data");
mAdapter.notifyDataSetChanged();
mRecyclerView.scrollToPosition(mAdapter.getItemCount() - 1);
View itemView= mRecyclerView.getChildAt(mAdapter.getItemCount()-1);

above itemView is always getting null value.

Scenario 2:

mListItem.add("Hello Data");
mAdapter.notifyDataSetChanged();
mRecyclerView.scrollToPosition(mAdapter.getItemCount() - 1);
int lastItemPosition = linearLayoutManager.findLastVisibleItemPosition();
View itemView= linearLayoutManager.findViewByPosition(lastItemPosition);

above itemView giving second last item view but size is correct.

I am not able to understand why last item view is not available after calling right back notifyDataSetChanged .

Please explain!

Thanks!

Please use this

 mListItem.add("Hello Data"); mAdapter.notifyDataSetChanged(); mRecyclerView.scrollToPosition(mListItem.size() - 1); 

You should use list size like

mListItem.size() instead of mAdapter.getItemCount()

And no need to call this

View itemView= mRecyclerView.getChildAt(mAdapter.getItemCount()-1);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM