I am using RecyclerView.Adapter but I am little confused regarding working of its method onCreateViewHolder
.
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
if(viewType==TYPE_ITEM) {
View mView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.inflate_common_item, viewGroup, false);
ViewHolder vh = new ViewHolder(mView);
return vh;
} else {
View mView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.inflate_uncommon_item, viewGroup, false);
ViewHolderFooter vh = new ViewHolderFooter(mView);
return vh;
}
}
So incase I have 10 items in my list so for each item this method will be called and every time a new ViewHolder
will be created of course it'll one time for each view but now my question is when we were using ListView
and BaseAdapter
with them we store ViewHolder
in tag and use that. We don't create ViewHolder
for each item.
@Override
public View getView(int position, View convertView, ViewGroup parent) {
MyViewHolder mViewHolder;
if(convertView == null) {
convertView = inflater.inflate(R.layout.layout_list_item, null);
mViewHolder = new MyViewHolder();
convertView.setTag(mViewHolder);
} else {
mViewHolder = (MyViewHolder) convertView.getTag();
}
mViewHolder.tvTitle = detail(convertView, R.id.tvTitle, myList.get(position).getTitle());
mViewHolder.tvDesc = detail(convertView, R.id.tvDesc, myList.get(position).getDescription());
mViewHolder.ivIcon = detail(convertView, R.id.ivIcon, myList.get(position).getImgResId());
return convertView;
}
So are we not creating extra viewholders object. Please help me understand the pros and cons.
Thanks
The job of onCreateViewHolder() is to create instances of a ViewHolder , including working with the ViewHolder to set up the widgets. Since our widgets are defined in a layout resource, we will need a LayoutInflater to accomplish this.
This method calls onCreateViewHolder to create a new ViewHolder and initializes some private fields to be used by RecyclerView. Returns the position of the given ViewHolder in the given Adapter . Returns the total number of items in the data set held by the adapter. Return the stable ID for the item at position .
onCreateViewHolder only creates a new view holder when there are no existing view holders which the RecyclerView can reuse. So, for instance, if your RecyclerView can display 5 items at a time, it will create 5-6 ViewHolders , and then automatically reuse them, each time calling onBindViewHolder .
By default it have 5.
Show activity on this post. onCreateViewHolder only creates a new view holder when there are no existing view holders which the RecyclerView can reuse. So, for instance, if your RecyclerView can display 5 items at a time, it will create 5-6 ViewHolders, and then automatically reuse them, each time calling onBindViewHolder.
Android Mobile Development Apps/Applications Recycler view is a more advanced version of listview and works based on View holder design pattern. Using recyclerview we can show grids as well as a list of items. This example demonstrates how to integrate RecyclerView by creating a beautiful student records app that displays student name with age.
RecyclerView.Adapter base class for presenting List data in a RecyclerView, including computing diffs between Lists on a background thread. Adapters provide a binding from an app-specific data set to views that are displayed within a RecyclerView.
Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Open build.gradle and add Recycler view library dependency.
onCreateViewHolder
only creates a new view holder when there are no existing view holders which the RecyclerView
can reuse. So, for instance, if your RecyclerView
can display 5 items at a time, it will create 5-6 ViewHolders
, and then automatically reuse them, each time calling onBindViewHolder
.
Its similar to what your code in the ListView
does (checking if convertView
is null
, and if not, grabbing the existing ViewHolder
from the tag), except, with RecyclerView
, this is all done automatically.
I suppose this is one of the pros with using a RecyclerView
- you don't need to worry so much about reusing ViewHolders
as you do with ListView
. The con is, RecyclerView
is very customisable, but has very little built in functionality - unlike ListView
which is not very customisable, but has a lot of built in functionality.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With