RecyclerView show data from 2 different layouts

709 views Asked by At

I am using the following code in Adapter class to show data in a RecyclerView, but now I would like to show data from some other ArrayList in a same RecyclerView (at some positions like: 1st position and 6th position) using different layout.

That different layout (assume: another_layout.xml) contains 2 TextViews and an Image, also want to implement click on listener for that layout too..

    @Override
    public PlaylistCardAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // inflate a card layout
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.youtube_video_card, parent, false);
        // populate the viewholder
        ViewHolder vh = new ViewHolder(v);
        return vh;
    }
4

There are 4 answers

4
Rajesh Gosemath On BEST ANSWER

RecyclerView for more than one layout
1. Override getItemViewType(int position) method
e.g I have two layouts layout1 and layout2.I want layout1 at the top and then layout2. So getItemViewType would be something like this

@Override
public int getItemViewType(int position) {

    if(position == 0){    //for layout1
        return 1;
    }else{
        return 2;        //for layout2
    }
}

2. Different viewholder for each layout like this

class ViewHolder_LayoutOne extends RecyclerView.ViewHolder{
    TextView name;
    //Constructor
}

class ViewHolder_LayoutTwo extends RecyclerView.ViewHolder{
    TextView name;
    //Constructor
}

3. Inflate different layouts according to the position

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    RecyclerView.ViewHolder vh = null;
    if(viewType == 1){

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_layout_one,parent,false);
        vh = new MyViewHolder_LayoutOne(view);
    }else if(viewType == 2){

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_layout_two,parent,false);
        vh = new MyViewHolder_LayoutTwo(view);
    }
    return vh;
}

4. Bind your views as per the position

@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {


    switch(getItemViewType(position)){

        case 1: //for layout 1
            ((ViewHolder_LayoutOne)holder).name.setText("");
            break;

        case 2:// for layout 2
            ((ViewHolder_LayoutTwo)holder).name.setText("");
            break;
    }
}

5.Now Most important getItemCount() method //return the number of views

@Override
public int getItemCount() {
    return toptitles.length + 1; 
   // as i have only layout at the top and the remaining size equals the length of the array toptitles.So the overall length would be 
   //number of views of layout1 + number of views of layout2
}

Hope this helps!!!

0
Yua On

you can override getItemViewType, and use different ViewHolder for different layout.

3
Sahil Munjal On

You need to override getItemViewType like this :

@Override
    public int getItemViewType(int position) {
      Add your condition and return viewType(Constant)
    }

Now, Check for viewType and retrun viewHolder accordingly.

    @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
          if (viewType == VIEWHOLDER1) {
                View v = LayoutInflater.from(activity).inflate(R.layout.layout1, parent, false);
                return new ViewHolder1(v);
          }else if(viewType == VIEWHOLDER2){
                View v = LayoutInflater.from(activity).inflate(R.layout.layout2, parent, false);
                return new ViewHolder2(v);
          }
        }

Now, Check for instance of viewHolder

@Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
         if (holder instanceof ViewHolder1) {
             Show values in UI accordingly.
         }else if(holder instanceof ViewHolder2){
             Show values in UI accordingly.
         }
}
0
Newbie Android On

You can set the type you want in getItemViewType(int position) with the layout you want. And onCreateViewHolder you can check viewType and inflate the row accordingly.

here is an example.