recyclerView setAdapter is very slow - android

I used recyclerView in my android app and filled it by a list with 120 items. my problem is that it takes a couple of time to load recyclerView. it seems that setAdapter() method is very slow . what can I do to fix it?
list.addAll(db.getKarevanMembers());
KarevanMembersAdapter adapter = new KarevanMembersAdapter(list);
binding.membersLayout.karevanMembersList.setAdapter(adapter);

Instead of setting new adapter change data of current adapter and call adapter.notifyDataSetChanged().
If you want to change/remove only some elements in the list, use the methods notifyItemInserted, notifyItemRangeInserted, notifyItemChanged, notifyItemRangeChanged, notifyItemRangeRemoved, notifyItemRemoved to update adapter (see https://developer.android.com/reference/android/support/v7/widget/RecyclerView.Adapter.html).

Related

Is there an event called by changing the count of items in the recyclerview?

Is there an event called by changing the count of items in the recyclerview?
I want to call function every time recyclerview items are added.
Update:
It turns out I misunderstood the original question. What was really asked is how to get notified when the underlying data in an Adapter has changed.
For this you could use a RecyclerView.AdapterDataObserver in conjunction with RecyclerView.Adapter's registerAdapterDataObserver(AdapterDataObserver observer) method to register for changes in the underlying list data.
Original Answer:
Your RecyclerView is backed by a RecyclerView.Adapter which owns the actual list of items being displayed.
If you change the backing list, you should call one of the notifyXXX methods on your adapter to notify your RecyclerView what has changed (and optionally where in the list the change happened.)
At the very least, you could call notifyDataSetChanged on your adapter to tell your RecyclerView that something somewhere in the list has changed. This can be expensive since you're not being specific about what changed, so the RecyclerView has to query the adapter for more information and potentially redraw its entire client area of items.
Something like:
RecyclerView.Adapter adapter = recyclerView.getAdapter();
if (adapter != null) {
adapter.notifyDataSetChanged();
}
Behind the scenes, the RecyclerView will call the RecyclerView.Adapter#getItemCount() method on the Adapter to determine what the current item count is.

SwipeMenuListView new Row added is showing First time data

I've read many of the previous posts on this topic, but i'm not getting it right.
I have an adapter which has a private values of the items in the list.
When i update the values(add a new items), i watch the value in debugger and the "getView" func and see that the value is correct.
BUT the actual rowView i see is just the first item in the list.
I have no clue what may cause this.
This listview is on the same activity while i show a different layout and hide the listview to add a new item.
Can there be a connection while the listview visibility is "GONE"?
When i remove items from it it updates listview fine(that is done when listview is visible).
private void updateAdapter() {
this.values.clear();
this.values.addAll(staticlistIndifferentclass);
notifyDataSetChanged();
}
~~~~UPDATE~~~~
Ok,
So i discovered the cause of the problem, though i'm not sure why it is.
The code was fine the way it was with regular Listview but the bug is on:
com.baoyz.swipemenulistview.SwipeMenuListView
Try use ListView.invalidateViews
This should cause views rebuilding
Use new Adapter object like this :
listView.setAdapter(new yourCustomAdapter());
When you delete all dataSet items ,it is better to bind new Adapter object .

ListView refresh taking it to the top of the list

Scenario: I am using a listview with custom Adapter. I am fetching data every 5 seconds and then displaying it in the listview.
Problem: After every refresh the list scrolls at the top. I would want the list to stay as is from the scroll standpoint and just the underlying data to be refreshed.
Current code: This is called every time I fetch a new data.
mListAdapter = new CustomListAdapter(this, mListData);
mList.setAdapter(mListAdapter);
Instead of your code please try to put every time you fetch data this one:
adapter.notifyDataSetChanged();
Instead of calling this, you should set the new data to the adapter and then call notifyDataSetChanged.
Hope this helps!
As you get new mListData then just invalidate the adapter not re-initialized the adapter you can call like this
mListAdapter.notifyDataSetChanged();
You can use
mList.setSelection(mListData.size() - CONSTANT.CONTENT_SIZE);
This way the listview will not refresh to the top.
Or you can also use
adapter.notifydatasetchanged
by updating the binded list.
This Worked For Me:
adapter.setData(list);
adapter.notifyDataSetChanged();

how to stop executing the getView() method while scrolling the ListView items using SimpleAdapter in android

i have used SimpleAdapter to display a list of items in a listview.While displaying the getView() is called every time to display every list item with different layout.Here i got succeeded in doing so.But here the problem is whenever i scroll the list items again the getView() method is executing and iam getting the unreliable results.Here my requirement is i dont want to execute the getView() method while scrolling the listview.Any body please guide me out of this situation.
This is the intended behaviour of the ListView, it is automatically buffered. So as you scroll, new items are inflated and added and the old ones are knocked off. This mechanism works by calling your overridden getView() method.
If you don't want this then just use a LinearLayout inside a scrollview and manually populate it with your list items. You can even still use your adapter by using:
linear.addView(adapter.getView(index, null, linear));
Hope this helps!
I want to add one thing that if
you have more than one records in your adapter just use a for loop like this.
ArrayAdapter<Question> adapter = MyCustomArrayAdapter
.GetArrayAdapterInstance(this, flag1, flag1, lst_question);
for(int i=0;i<adapter.getCount();i++)
{
linear.addView(adapter.getView(i, null, linear));
}
I also get the same problem, and here is our response !
Android, how to stop reading getView() function again, if the content is already downloaded in the ListView
Let me know if it work for you ;)

Android: Possible solution for updating the custom list view at random times

I am having a situation where I want to update my Custom List View using BaseAdapter whenever my Database is updated. I have tried calling invalidate() on this Custom List but it didn't work, similarly I even tried having a timer to update my list after sometime, that didn't work either. Please let me know of possible solution.
Update:
This is how I am making my custom list view
li= (ListView)findViewById(R.id.id_lv_row);
ColorDrawable divcolor = new ColorDrawable(Color.DKGRAY);
registerForContextMenu(li);
li.setDivider(divcolor);
li.setDividerHeight(2);
li.setAdapter(new FriendsPositionAdapter(this));
BaseAdapter.notifyDataSetChanged() should do the trick as long as the data behind the adapter actually changed. That's all you need to do to refresh the list.
Invalidate is for repainting views only, you have to tell to the List adapter (BaseAdapter) that dataset has changed.
When the data changes, asign the new dataset to the adapter, and later call notifyDataSetChanged()...
in order to make functional notifyDataSetChanged() the adapter data must be changed. Remember that the original data that change is not reflected automatically to the adapter.
//here i retrieve the new list, named "beans"
lista = (BeanList) result.getDataObject();
Vector<Bean>beans = list.getBeanList();
((BeanListAdapter)listAdapter).syncData(beans);
((BeanListAdapter)listAdapter).notifyDataSetChanged();
//now the syncData method
public void syncData( List<PINPropiedad> newData ){
for(Object o : newData){
add(o);
}
}

Categories

Resources