Recyclerview With Multiple view types

2:55 AM Nilesh Deokar 0 Comments

Ever wondered how to implement the multiple view types in recycler view?



Method invocation in Recyclerview:

1. Constuctor method
2. getItemViewType()
3. onCreateViewHolder()
4. onBindViewHolder()

to achieve the tast all we need to de is implement the  getItemViewType(), And it will take care of the  viewTyp parameter in   onCreateViewHolder().

/**
 * Created by Nilesh Deokar on 26/08/15.
 */
public class MyHoodAdapter extends RecyclerView.Adapter {


    LayoutInflater inflater;
    List data = Collections.emptyList();
    Context mCOn;
    Calendar calendar;

    private static final int TYPE_CRAVING = 1;
    private static final int TYPE_SHARED_DISH = 3;
    private static final int TYPE_ONLY_SHARE_DIHS = 7;

    public MyHoodAdapter(Context context, List list, int userId, String userName, int b_id) {
        this.mCOn = context;
        inflater = LayoutInflater.from(context);
        this.data = list;
        this.userId = userId;
        this.userName = userName;
        this.b_id = b_id;

    }


    @Override
    public int getItemViewType(int position) {

        return model.getStrType();

    }

    @Override
    public MainViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        switch (viewType) {
            case TYPE_SHARED_DISH:
                return new SharedDishViewHolder(LayoutInflater.from(mCOn).inflate(R.layout.myhood_custom_row_dish, parent, false));
            case TYPE_ONLY_SHARE_DIHS:
                return new OnlySharedDishViewHolder(LayoutInflater.from(mCOn).inflate(R.layout.nearby_custom_row_shared, parent, false));
            case TYPE_CRAVING:
                return new MyViewHolder(LayoutInflater.from(mCOn).inflate(R.layout.myhood_custom_row_craving, parent, false));
         
        }
        return null;


    }

    @Override
    public void onBindViewHolder(MainViewHolder holder, int position) {

        if (holder.getItemViewType() == TYPE_SHARED_DISH) {
            SharedDishViewHolder mholder = (SharedDishViewHolder) holder;
            MyHoodModel model = data.get(position);

           /*
            * set the values to the view from your model object
            */

          } else if(holder.getItemViewType() == TYPE_ONLY_SHARE_DIHS){
            OnlySharedDishViewHolder mholder = (OnlySharedDishViewHolder) holder;
            MyHoodModel mo = data.get(position);

          }else if(holder.getItemViewType() == TYPE_CRAVING){
            MyViewHolder mHolder = (MyViewHolder) holder;
            MyHoodModel model = data.get(position);

        }
      }

    }


    @Override
    public int getItemCount() {
        return data.size();
    }




    public class MyViewHolder extends MainViewHolder {
     
    public MyViewHolder(View itemView) {
            super(itemView);
            /* Bind your object here */
         }
    }

    public class SharedDishViewHolder extends MainViewHolder {

    public SharedDishViewHolder(View itemView) {
            super(itemView);
            /* Bind your object here */
         }
    }


    public class OnlySharedDishViewHolder extends MainViewHolder {

    public OnlySharedDishViewHolder(View itemView) {
            super(itemView);
            /* Bind your object here */
         }
    }

  public class MainViewHolder extends RecyclerView.ViewHolder {
        public MainViewHolder(View v) {
            super(v);
        }
    }
}


0 comments: