0

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;
    }
Sophie
  • 2,594
  • 10
  • 41
  • 75

4 Answers4

1

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!!!

Rajesh Gosemath
  • 1,812
  • 1
  • 17
  • 31
  • I tried the way you mentioned me, but I am getting issues / errors at many places in my Adapter class, can you make modifications in my existing Adapter class ... which one I posted above – Sophie Dec 13 '16 at 11:18
  • take a look at this (udacity popular movie application project)https://github.com/gosemathraj/Cinema/blob/master/app/src/main/java/com/gosemathraj/cinema/adapters/RecyclerViewCustomAdapter.java – Rajesh Gosemath Dec 13 '16 at 11:21
  • for every 3rd position your getItemViewType just check for position like this if((position + 1) % 4 == 0){ return 1;} – Rajesh Gosemath Dec 13 '16 at 12:07
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/130533/discussion-between-android-jack-and-sophie). – Rajesh Gosemath Dec 13 '16 at 15:46
0

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

Yua
  • 23
  • 8
0

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.
         }
}
Sahil Munjal
  • 463
  • 2
  • 15
  • ohk... getItemViewType(...) what exactly I need to write... my logic is to show data from different layout in 1st and 6th position – Sophie Dec 13 '16 at 08:59
  • In your case return position from getItemViewType and in onCreateViewHolder check for viewType using if-else OR switch and accordingly return ViewHolder. – Sahil Munjal Dec 13 '16 at 10:18
  • I tried the way you mentioned me, but I am getting issues / errors at many places in my Adapter class, can you make modifications in my existing Adapter class ... which one I posted above – Sophie Dec 13 '16 at 11:19
0

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.

Newbie Android
  • 389
  • 1
  • 8