Tolulope
Tolulope

Reputation: 498

RecyclerView with multiple view types and data source

I am working with a recyclerView and i succeeded in inflating two views but each view content comes from different json data types. i tried passing the two datatypes in the adapter but they are not properly binded

Upvotes: 2

Views: 10996

Answers (3)

user4571931
user4571931

Reputation:

Make one interface to implement your data pojo class like below..

public interface Parent{
}

Then pojo class like

public class User implements Parent{
    private String name,addres;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddres() {
        return addres;
    }

    public void setAddres(String addres) {
        this.addres = addres;
    }
}

Then add all the data into parent list and bind into adapter .. Adapter bind data according to pojo class like below..

public class SearchListAdapter extends ArrayAdapter<Parent> {
    Context context;
    Parent parent[] = null;
        public SearchListAdapter(Context context, int layoutResourceId, Parent[] parent) {
        super(context, layoutResourceId, parent);
        this.context = context;
        this.parent = parent;
    }
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        final View outerContainer = inflater.inflate(R.layout.food_list_item, parent, false);
        if (this.parent != null) {
            Parent temp = this.parent[position];
            if (temp instanceof FoodItem) {
                final FoodItem item = (FoodItem) temp;
            }
        }   
    }
}

Upvotes: 0

Truong Giang Dam
Truong Giang Dam

Reputation: 500

You should merge data to one data source only. You can try this way:

  1. Create data source class

    public class Data {
       int type; // 1 is article and 2 is youtubeitem
       public Article article;
       public YouTubeItem youTubeItem;
    }
    
  2. Now merge two data source to only one

    public List<Data> merge(Articel[] articles, List<YouTubeItem> items) {
        List<Data> datas = new ArrayList<>();
        for(Article article : articles) {
           Data data = new Data();
           data.article = article;
           data.youTubeItem = null;
           data.type = 1;
           datas.add(data);
        }
    
        for(YouTubeItem item : items) {
           Data data = new Data();
           data.article = null;
           data.youTubeItem = item;
           data.type = 2;
           datas.add(data);
        }
    
       return datas;
    }
    
  3. Change constructor of adapter

    private List<Data> datas;
    
    public SimpleStringRecyclerViewAdapter(Context context, List<Data> datas )
    {
      this.datas = datas;
    }
    
  4. Change get Item count

    public override int ItemCount
    {
    
        get
        {
            return datas.Count();
        }
    }
    
  5. Change getViewType

    public override int GetItemViewType(int position)
    {
       if (datas.get(position).type == 1)
       {
            return Resource.Layout.List_Item;
       }
    
       else
       {
           return Resource.Layout.VideoList;
       }
    }
    

EDITED: For merge random method

 public List<Data> mergeRandom(Articel[] articles, List<YouTubeItem> items) {
     List<Data> datas = new ArrayList<>();

     List<Integer> random = new ArrayList<>();
     int maxLength = articles.length + items.size(); 
     for(int i = 0; i< maxLength; i++) { 
        random.add(i);
     }

     while (random.size() > 0) {
        // get random item
        int index = new Random().nextInt(random.size());
        int position = random.get(index);

        if(position <= article.length - 1) {
            Data data = new Data();
            data.article = articles[position];
            data.youTubeItem = null;
            data.type = 1;
            datas.add(data);
        } else {
            Data data = new Data();
            data.article = null;
            data.youTubeItem = items.get(position - article.length);
            data.type = 2;
            datas.add(data);
        }

        random.remove(index);
    }

    return datas;
 }

For merge odd&even method

List<Data> mergeOddEven(Articel[] articles, List<YouTubeItem> items) {
    List<Data> datas = new ArrayList<>();

    int articleIndex = 0;
    int youtubeIndex = 0;
    int length = articles.length + items.size();

    for(int i = 0; i< length; i++) {
        if(articleIndex >= articles.length || youtubeIndex >= items.size()) {
            if(articleIndex < articles.length) {
                for(int j = articleIndex; j < articles.length ; j++) {
                   Data data = new Data();
                   data.article = articles[j];
                   data.youTubeItem = null;
                   data.type = 1;
                   datas.add(data);

                }
            } else {
                for(int j = youtubeIndex; j < items.size() ; j++) {
                   Data data = new Data();
                   data.article = null;
                   data.youTubeItem = items.get(j);
                   data.type = 2;
                   datas.add(data);

                }
            }

            break;
        }


        if(i % 2 == 0) {
            Data data = new Data();
            data.article = articles[articleIndex];
            data.youTubeItem = null;
            data.type = 1;
            datas.add(data);

            articleIndex++;
        } else {
            Data data = new Data();
            data.article = null;
            data.youTubeItem = tems.get(youtubeIndex);
            data.type = 2;
            datas.add(data);

            youtubeIndex++;
        }
    }

    return datas;
}

Hope it help

Upvotes: 8

Hitesh Sarsava
Hitesh Sarsava

Reputation: 680

use this function to merge the data and use this in your adapter :

public List<Data> merge(Articel[] articles, List<YouTubeItem> items) {


        int counter = 0,counter1=0,size=0;


        size = articles.length + items.size();

        List<Data> datas = new ArrayList<>();
        for (int i = 0; i < size;i++) {

            if (i % 2 == 0) {

                if(counter1<articles.length){

                Article article = articles[counter1];
                Data data = new Data();
                data.article = article;
                data.youTubeItem = null;
                data.type = 1;
                datas.add(data);
                counter1++;

                }else if(counter<items.size()){

                 YouTubeItem item = items.get(counter);
                Data data = new Data();
                data.article = null;
                data.youTubeItem = item;
                data.type = 2;
                datas.add(data);
                counter++;
                }

            } else {

                if(counter<items.size()){

                   YouTubeItem item = items.get(counter);
                Data data = new Data();
                data.article = null;
                data.youTubeItem = item;
                data.type = 2;
                datas.add(data);
                counter++;
                }else if(counter1<articles.length){

                Article article = articles[counter1];
                Data data = new Data();
                data.article = article;
                data.youTubeItem = null;
                data.type = 1;
                datas.add(data);
                counter1++;
                }

            }

        }


        return datas;
    }

Upvotes: 0

Related Questions