-3

Recycler View

I am adding images from gallery and text from edit text.

Two arrays for getting two different views:
private ArrayList<Bitmap> mImages = new ArrayList<Bitmap>(); private ArrayList<String> mDataSet = new ArrayList<String>();

Problem: my getItemViewType method shows Array out of index exception and not getting views properly.

`@Override
public int getItemViewType(int position) {
        if(modellist1.get(position) instanceof Bitmap) {
            Log.d(TAG, "GETTING IMAGE VIEW TYPE");
            return VIEW_TYPE_FIRST;
        }
        else if (modellist2.get(position) instanceof String) {
            Log.d(TAG, "GETTING TEXT VIEW TYPE");
            return VIEW_TYPE_SECOND;
     }`
shyam
  • 1,084
  • 12
  • 20

2 Answers2

0

I don't think you need to manage 2 separate list to populate a recyclerview, even if it's dynamically. Check out this link Multiple View types in a recyclerview.

Once you add different elements in the list, the recycler view gives you a function getItemViewType which lets you specify what view needs to be inflated for which item. As for adding element dynamically you can add the element to the list and let the recyclerview adapter know by using the variety of helper functions that the adapter provides such as notifyItemChanged, notifyItemRangeInserted etc. More at this link Recycler View Adapter Documentation

About the size of the images, that doesn't really fall under the recyclerview bracket, that's based on your custom logic, either you could make the imageview 40x40 or you could make it match_parent, you have control of the view that is being inflated along with the logic for populating the inflated view.

Jude Fernandes
  • 7,437
  • 11
  • 53
  • 90
  • Thank u i have a doubt.I had used 2 lists because to view 2 different types Bitmap and String.How can i use a single list?? @Jude – shyam Feb 05 '18 at 13:27
  • I'm guessing you did not go through the link that i posted, please go through that, the multiple view types, it'll give you exactly what you want, basically you'll have a function called getItemViewType() in your adapter where you can say that a particular item in the list is of type Bitmap so use the layout called item_bitmap.xml or the item is of type String so use the layout called item_string.xml – Jude Fernandes Feb 05 '18 at 16:28
  • Thank you @Jude i managed my single list with 2 layouts to get two different views.Got 2 different views now thank u – shyam Feb 07 '18 at 10:26
  • You're welcome, could you make the answer as correct please if everything worked out fine for you. – Jude Fernandes Feb 07 '18 at 11:57
0
public int getItemViewType(int position) {
    if (modellist.get(position).IfImage() != null) {
            return VIEW_TYPE_FIRST;
        } else {
            return VIEW_TYPE_SECOND;
        }

    }
shyam
  • 1,084
  • 12
  • 20