0

I have a questions about Recyclerview when the types of items are dynamic.It means I don't know how many types of Recyclerview's items.So I don't know how to write the onCreateViewHolder() in MyAdapter extends RecyclerView.Adpter<RecylerView.ViewHolder>.

But in fact, the items follow some laws.For example:

item1:

Text(title)
Pic(Vertical) Pic(Vertical) Pic(Vertical)
Pic(Vertical) Pic(Vertical) Pic(Vertical)

item2:

Text(title)
Pic(Horizontal) Pic(Horizontal)
Pic(Horizontal) Pic(Horizontal)

If the picture is Vertical,the column is three.If the picture is Horizontal,the column is two.And the num of pictures maybe one,two,three,four,five,six and so on,So the types I can't confirm. I am sorry because I can't show a picture.I have ask a question RecyclerView with GridView

But it doesn't solve my questions.I have see How to create RecyclerView with multiple view type?

But the types are unchanged. PS:I know the pictures are vertical or Horizontal from data.

Community
  • 1
  • 1
Egos Zhang
  • 1,334
  • 10
  • 18

1 Answers1

2

Can't you create two custom ViewHolder's and check which one is passing into onCreateviewHolder as seen in this answer? How to create RecyclerView with multiple view type?

public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

class ViewHolder0 extends RecyclerView.ViewHolder {
   //viewHolder for horitzontal pics
    ...
}

class ViewHolder2 extends RecyclerView.ViewHolder {
    ...
    //viewHolder for vertical pics
}

@Override
public int getItemViewType(int position) {
    // Just as an example, return 0 or 2 depending on position
    // Note that unlike in ListView adapters, types don't have to be contiguous
    return position % 2 * 2;
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
     switch (viewType) {
         case 0: return new ViewHolder0(...);
         case 2: return new ViewHolder2(...);
         ...
     }
}

}

In docs you have the way to extend RecyclerView.ViewHolder

// Provide a reference to the views for each data item
// Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder
public static class ViewHolder extends RecyclerView.ViewHolder {
    // each data item is just a string in this case
    public TextView mTextView;
    public ViewHolder(TextView v) {
        super(v);
        mTextView = v;
    }
}
Community
  • 1
  • 1
PereCullera
  • 177
  • 2
  • 12
  • How's the data you want to bind in onBindViewHolder(ViewHolder holder, int position) Here you can just make a for and loop through the images you are getting – PereCullera Oct 09 '15 at 07:34
  • Where I write a for and loop through the images I am getting? Are you sure you know the meaning of my question ? – Egos Zhang Oct 09 '15 at 07:41
  • In adapter constructor i assume your getting your data: views, pics... In your onCreateViewHolder you can switch from viewHolders, And in onCreateViewHolder you can loop through the data from your constuctor atributes and add views to viewHolder programatically (ViewGroup) view.addView() http://developer.android.com/intl/es/reference/android/view/ViewGroup.html#addView(android.view.View, int, android.view.ViewGroup.LayoutParams) – PereCullera Oct 09 '15 at 07:58