I saw similar questions in stackoverflow but they doesn't give clear answers to my question. Don't mark it as duplicate before reading my full questions. I saw this link , this , and this too. Thanks for spending your time to read this.
I gave my three questions below the Source code, kindly have a look at it.
I'll make it simple. I am trying to use two ViewHolder in Recycler Adapter which i am going to use in ViewPager for TabLayout. Both View Holder having different Xml and different elements (ie textview , imageview etc..) But got struck with several confusions inside it.
I implemented my RecyclerView adapter class as follows
public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
public class MainViewHolder extends RecyclerView.ViewHolder {
public MainViewHolder(View v) {
super(v);
}
class ViewHolder0 extends MainViewHolder {
...
}
class ViewHolder2 extends MainViewHolder {
...
}
@Override
public int getItemViewType(int position) {
/**** I don't know where and when this method will be called and what will be the value present in the variable "position" ****/
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
switch (viewType) {
case 0: return new ViewHolder0(...);
case 2: return new ViewHolder2(...);
...
}
}
public int getItemCount() {
/**** I don't know where and when this method will be called and what will be the value present in the variable "position" ****/
}
}
And my Questions are,
Q1. When and where getViewType is called and what will be in "position" variable and what do we need to return
Q2. When and where getItemCount is called and how can i return correctly (because i am using two view holders and each will have different count )
Q3. I created seperate Recyclerview Adapter class but it gave an error that RecyclerViewAdapter class clashes with the another one. (Since i am using them in same activity for TabLayout, i thought that error was thrown, am I correct? or is there any way to create seperate Adapter class)
If you can explain the full process of RecyclerViewAdapter, that would be awesome :) But please clarify my above confusions.
Any type of help welcomed, Thanks in advance... :)
getItemCount() : RecyclerView calls this method to get the size of the data set. For example, in an address book app, this might be the total number of addresses. RecyclerView uses this to determine when there are no more items that can be displayed.
Adapter. RecyclerView includes a new kind of adapter. It's a similar approach to the ones you already used, but with some peculiarities, such as a required ViewHolder . You will have to override two main methods: one to inflate the view and its view holder, and another one to bind data to the view.
In, them adapter method, just add that new data in your main arraylist and do notifyItemInserted() instead of notifyDataSetChanged, in this way you will also see the default adding animation of recyclerView. Save this answer.
By default it have 5. you can increase as per your need. Save this answer.
Q1) The getViewType() method will be called before the onCreateViewHolder() method each time your custom view is created.
You need to create a list with your custom list items List<CustomItem> list=method_to_return_your_list()
and each of them with a getViewType() getter method.
In your get getItemViewType()
public int getItemViewType(int position) {
return list.get(position).getViewType();
/*this returns the view type of each element in the list*/
}
This can be either 0 or 1 considering your switch case statement in the onCreateViewHolder() method
Q2) The getItemCount() method should return the number of list items.
public int getItemCount() {
return list.size();
}
Q3) Do not create another recyclerview adapter for the same recyclerview
Also I fogot. Instead of creating new ViewHolders ,just change the itemView in the view holder like
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
switch (viewType) {
case 0: return new ViewHolder(itemView0);
case 2: return new ViewHolder(itemView1);
...
}
}
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