How recycler view Data bind? - java

How Recycler view Bind Data One By One or All in one time?

void onBindViewHolder (VH holder,int position,List<Object> payloads)
Called by RecyclerView to display the data at the specified position.
This method should update the contents of the itemView to reflect the
item at the given position.
So one by one. More here .

Your recycler view calls RecyclerView.Adapter when you set adapter to recycler view or
notify dataset changes to the adapter.
Recycler view calls OnBindViewHolder for each item in your recycler.
Hence, recyclerview binds data one by one.
You can clearly see this when you implement animation to the recycler view items.

RecyclerView is a list drawing library that essentially provides a fixed-size window to load a large dataset into. It recycles the views it created at the begining when the views go out of scope (window) and then if there is a need, reuses them to make it seem as if the views were never offloaded and were virtually present outside the window.It binds the rows which are visible to you and recycles the rest

Related

ProgressBar in Recyclerview with Java

For Home fragment, I use a ProgressBar in in RecyclerView.Adapter. When the App starts everything render without problem. But when I come back from the other fragment with this code:
Navigation.findNavController(view).navigate(R.id.action_nav_dashboard_to_boxFragment);
Then the ProgressBar will be rendered for all recycler items with the same state
holder.progressBar.setProgress(percent, true);
RecyclerView uses one view multiple times, when it contains the list which is not displaying on the screen at a time(means a list contain large amount of items which is not displaying on screen at same time you need to scroll up and down). When user scroll the list the offscreen items are reused to display the remaining list items which is called recycling.
To Stop recycling the items call this method in your onBindViewHolder method:
holder.setIsRecyclable(false);

Understanding RecyclerView.ViewHolder

I'm having hard time understanding working of view holder, here are my some question that could increase my understanding of viewholder:
It is said that oncreateViewHolder returns viewholder object, What is viewholder object does it contain all the views in single row? if there is list of 1000 item how many viewobjects will be created?
My understanding:
If we are creating viewholder object it contains reference of view like findviewbyid, since findviewbyid is expansive operation, so by viewholder we can create single viewholder object and reuse by just setting image or text(happens in onBindView).
But onCreateViewHolder runs multiple times and as a result findviewbyid will also execute multiple time, isn't performance issue?
Also how its different from convertView of base adapter of simple listview
Thanks!
View holder it is thing which helps u you to reduce find view by id calls.
Let give you an example.
Suppose you have 1k items, each have a 5 view you need to find by id and only 5 full items can be shown once at screen.
So, recyclerView will create 7 (5 + one not-full bottom and one not-full top) view holders. Next time when recyclerView will be scrolled it will use existing viewHolders. Exactly as name says : "RecyclerView"
So findViewById will be called 7*5=35 times.
If you do not use viewHolder you would get 5*1000 = 5000 calls.
35 vs 5000, so you understood I think.
It is said that oncreateViewHolder returns viewholder object, What is
viewholder object does it contain all the views in single row? if
there is list of 1000 item how many viewobjects will be created?
One ViewHolder object for one view row. One ViewHolder object is created for every time the onCreateViewHolder is called. It is called based on the number of visible items in the device. Even if you have 100 items, if ony 10 items are visible, the onCreateViewHolder will be called 10 times and there will be 10 ViewHolders. (There might be one or two extra item based on the RecyclerView optimizations because if you scroll the list, the next item should be visible instantaneously)
My understanding: If we are creating viewholder object it contains
reference of view like findviewbyid, since findviewbyid is expansive
operation, so by viewholder we can create single viewholder object and
reuse by just setting image or text(happens in onBindView).
RecyclerView is already recycling and reusing the Views and the corresponding ViewHolders. The number of ViewHolder (and View) present at any time depends on the number visible items on the screen.
But onCreateViewHolder runs multiple times and as a result
findviewbyid will also execute multiple time, isn't performance issue?
As said previously, the number of times this will be called is only for the number of visible items. When you scroll, the views and viewholders are reused. You have distinct Views for each row. So there will be distinct ViewHolder for each row.
Also how its different from convertView of base adapter of simple
listview
In ListView, the convertView is the old view, which provides an option to reuse the same view for new rows as you scroll the list. But it's optional because the developer might not use the convertView at all. In RecyclerView the reusing of old views is done automatically.
RecyclerView.ViewHolder is a helper class that holds the View of a row or rows.
One or more ViewHolder is created for each viewType.
if several rows have the same ViewType then the same View can be reused for several rows.
Overriding getItemViewType(int position) is the way to have several view types. If getItemViewType returns a not used before viewType then onCreateViewHolder will be called to create a new ViewHolder.
Adapter onBindViewHolder is the place to fill the view with specific data for each row.
ADDED:
A concept must be clear: what makes a ViewHolder to be reused is that it shares the same viewType.
Instead if you make getItemViewType(int position) return a different value for each row then each row will have its independent ViewHolder and view.
assume that you want to show a list of 1000 items and there are just 10 items visible to the user in the screen. your adapter creates 10 ViewHolder instances to show them at the same time. when user scrolls and the adapter has to show more items, instead of creating new instances of ViewHolder, it reuses items that are not visible anymore. your adapter prevents creating new Views and saves CPU time by doing so.

Update RecyclerView adapter item without calling OnCreateViewHolder

how to update ReyclerView adapter without make OnCreateViewHolder(...) being called?
Because when i do
mRecyclerView.getAdapter().notifiItemChanged(position)
it goes inside OnCreateViewHolder(...) what i don't want to. The reason why i don't want is because I have expanded item view and onCreateViewHolder(...) will reinflate it so make collapsed. I only want to OnBindViewHolder(...) being called
Have anyone faced with it?
UPD:
Just found that OnCreateViewHolder(...) is called only one first time, in all next times it's not being called. What is the reason?
t
You could add a boolean isExpanded to your data objects in your dataset, and then in the onBindViewHolder method check whether isExpanded is true, if so expand the view programmatically. (and ofcourse don't forget to switch the boolean when expanding/collapsing the view)
This is the whole point of RecyclerView : it handle a pool of view holders, which are created once (calling OnCreateViewHolder()) then reused (or recycled, hence the view name) as much as possible via OnBindViewHolder().
OnCreateViewHolder() is used to create the views, while OnBindViewHolder (re)set the views state (like text content, selected state, ...)
It follows that any state associated with an item (like the expanded / collapsed state) should not be stored in the ViewHolder. You can for example use a private List in the adapter.

Android - Scroll to position in ListView in ListFragment after data of adapter has changed

I am using a ListFragment with a custom adapter, which is used to inflate the views. The data is loaded via an AsyncTaskLoader once the fragment is created.
My problem is now that I want to scroll (not smooth scroll) to a certain position to show the last selected element, i.e. The fragment gets destroyed when I select an element and I want to show the same position in the list once the fragment is displayed again.
So creating the view and everything works fine, but I do not know where to add the getListView().setSelection(x) line to scroll to a certain position. I tried to invoke it after clearing and adding the elements to the adapter in Loader<?>.onLoadFinished() function, but that does not work, I get the following exceptoins:
java.lang.IllegalStateException: Content view not yet created
at android.support.v4.app.ListFragment.ensureList(ListFragment.java:328)
at android.support.v4.app.ListFragment.getListView(ListFragment.java:222)
at fs.pokemon.effect.PokemonFragment.updateData(PokemonFragment.java:239)
at fs.pokemon.effect.PokemonFragment$2.onLoadFinished(PokemonFragment.java:129)
...
My question is: Is there a function/callback so I get notified, when the ListView has finished updating its content? Or how else can I scroll the View to a position after the content has loaded?
Thanks to pskink comment I tried it again and noticed that the exception does not occur in setSelection() but rather in getListView() - I though my loader would reload data after the view was created, but instead it used the cached results, so onLoadFinished() was actually called, before the initialization of the fragments view was finished, thus causing an exception.
The correct way to do it is to determine the position in onLoadFinished() and then override the fragments onViewCreated() and execute getListView().setSelection(curPosition) there.
Probably also the case that the loader cleared its cache and the onLoadFinished() will be called after the onViewCreated() method has to be considered, but so far it works fine.
Set the transcript mode and stackfromBottom for the listview while your listfragment activity is created. Now every time the listview is updated it will be scrolled to bottom to the latest item.
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
getListView().setTranscriptMode(ListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
getListView().setStackFromBottom(true);
}

adapter.notifyDatasetChanged() without changing current view

Is there a way to add views to a ViewPager (by adding data and calling notifyDatasetChanged() on its PagerAdapter) without changing the view currently being displayed?
I want the dataset to update in the background while the user is viewing other views. My problem now is that, when it finishes updating, it goes back to displaying the initial view. How can I prevent it from doing so?
As far as i know if you call notifyDatasetChanged() it only notifies the pager adapter that there is new data, the data has to be there before. so i think you should just not call notifyDatasetChanged() and when the user goes to the next view, the pageradapter loads the current (new) data. What about this solution?

Categories

Resources