rachna
rachna

Reputation: 124

how to implement multiple view in one recyclerview in android

How to create multiple views in one activity, I had tried it by using the multi Type view but it's not working Is it the right way or I need to add multiple recyclerview in one activity and hide the visibility of the recyclerview according to the filter

           public class Report_Adapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {// MultipleView Adapter class where multpleview type is binding
                    Context context;
                    List<BillModel> list;
                    private static final int DAILY_REPORT = 1; //these are the view type position variable

                    private static final int YEARLY_REPORT = 3;
                    private static final int MONTHLY_REPORT = 2;
                    private static final int ITEM_WISE_REPORT = 4;
                    private static final int DETAILED_SALES_REPORT = 5;


            public Report_Adapter(Context context, List<BillModel> list) { //construct which is using into the activity for setAdapter.
                        this.context = context;
                        this.list = list;}

                    @Override
                    public int getItemViewType(int position) { //here checking the ViewType position which is getting wrong here
                        if (position == 1)  //if position equals to viewtype position return the Dailylayout View other wise will go on second position
                            return DAILY_REPORT;
                        else if (position==2){
                            return MONTHLY_REPORT;
                        }
                        else if (position ==3)
                            return CUSTOMER_REPORT;
                        else
                            return TOTAL_SALES_REPORT;
                    }

                    @Override
                    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {

                        View view = null;
                        RecyclerView.ViewHolder viewHolder = null;
                          if (i == DAILY_REPORT) {
                            view = LayoutInflater.from( viewGroup.getContext()).inflate( R.layout.daily_report, viewGroup, false );
                            viewHolder = new Daily_Sales_Report( view );
                        }
                       else if (i == CUSTOMER_REPORT) {
                            view = LayoutInflater.from( viewGroup.getContext()).inflate( R.layout.activity_report__adapter, viewGroup, false );
                            viewHolder = new ViewHolder( view );
                        }else if(i==MONTHLY_REPORT) {
                           view = LayoutInflater.from( viewGroup.getContext()).inflate( R.layout.monthly_sales_report_adapter, viewGroup, false );
                           viewHolder = new ViewHolder( view ) }
                       else { view = LayoutInflater.from( viewGroup.getContext()).inflate( R.layout.total_sales_report_adapter, viewGroup, false );
             viewHolder = new ViewHolder( view) }
              return viewHolder;
             }

                    @Override
                    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {

                        final BillModel model1 = list.get( position);
                        int type = getItemViewType(position);
                         if (type == DAILY_REPORT) {
           Daily_Sales_Report daily_sales_report =(Daily_Sales_Repor)holder;
        daily_sales_report.txt_bill_no.setText( Integer.valueOf(model1.getB_Id() ) ); daily_sales_report.txt_date.setText(model1.getB_create_date() );
                            daily_sales_report.txt_total.setText( Float.toString( model1.getB_grand_total() ) );
                            daily_sales_report.txt_gst.setText( Float.toString( model1.getTotal_with_Gst() ) );
                            daily_sales_report.txt_del.setText( Integer.toString( model1.getB_del_ch() ) );
                            daily_sales_report.txt_pkg.setText( Integer.toString( model1.getB_pack_ch() ) );
                            daily_sales_report.txt_dis.setText( Float.toString( model1.getB_discount() ) );
                            daily_sales_report.txt_amt.setText( Float.toString( model1.getB_total() ) ); }
  else if (type == MONTHLY_REPORT){
          Montly_Sales_Report  report= (Montly_Sales_Report)holder;
                              final BillModel model = list.get( position);}
                          else  if (type== CUSTOMER_REPORT) {
                              final BillModel model = list.get( position);
                      ViewHolder viewHolder = (ViewHolder) holder;
                     viewHolder.txt_cust_number.setText( model.getC_phone());
                           viewHolder.txt_cust_name.setText(model.getC_name());
                          }else {
                              Total_Sales_Report total_sales_report= (Total_Sales_Report)holder;
                              total_sales_report.txt_start_date.setText(model1.getB_create_date())} }
                 @Override
public int getItemCount() {
 return list.size();}
                 class ViewHolder extends RecyclerView.ViewHolder { //customer report viewholder
   TextView txt_cust_name, txt_cust_number;
                 public ViewHolder(View item) {
                  super( item );
            txt_cust_name = item.findViewById( R.id.txt_cust_name );
             txt_cust_number = item.findViewById( R.id.txt_cust_number)}}

     class Daily_Sales_Report extends RecyclerView.ViewHolder {
           private TextView txt_bill_no, txt_date, txt_gst, txt_total, txt_dis, txt_del, txt_pkg, txt_amt;

 public Daily_Sales_Report(View itemView) {
                                super( itemView );
    txt_bill_no=(TextView) itemView.findViewById( R.id.daily_adapt_bill_no );
    txt_date = (TextView) itemView.findViewById( R.id.daily_adapt_date );
    txt_gst = (TextView) itemView.findViewById( R.id.daily_adpt_GST );
    txt_total = (TextView) itemView.findViewById( R.id.daily_adpt_total );
    txt_dis = itemView.findViewById( R.id.daily_adapt_dis );
    txt_del = itemView.findViewById( R.id.daily_adapt_del );
    txt_pkg = itemView.findViewById( R.id.daily_adapt_pkg );
    txt_amt = itemView.findViewById( R.id.daily_adapt_amount );}}

      class Montly_Sales_Report extends RecyclerView.ViewHolder {
                private ImageView cover_image;
private TextView txt_start_date, txt_end_date, txt_cash, txt_credit, txt_received, txt_expense, txt_total;       
         //monthly wise report view holder
         public Montly_Sales_Report(View itemView) {
                                super( itemView );txt_start_date = (TextView) itemView.findViewById( R.id.total_adapt_startdate );
  txt_end_date = (TextView) itemView.findViewById( R.id.total_adapt_enddate );
    txt_cash = (TextView) itemView.findViewById( R.id.total_adapt_cash );
    txt_credit = itemView.findViewById( R.id.total_adapt_credit );
    txt_received = itemView.findViewById( R.id.total_adapt_recieved );
     txt_expense = itemView.findViewById( R.id.total_adapt_expense );
     txt_total = itemView.findViewById( R.id.total_adapt_sell );}
                 }

I want this type of functionality for multiple search data from the database as you can see in this image2

Upvotes: 1

Views: 685

Answers (1)

Genius8020
Genius8020

Reputation: 145

first you must create 2 layout xml . after that inside recyclerview adapter TYPE_CALL and TYPE_EMAIL are two static values with 1 and 2 respectively in adapter class.

now Define two static values ​​at the Recycler view Adapter class level for example : private static int TYPE_CALL = 1; private static int TYPE_EMAIL = 2;

Now create view holder with multiple views like this:

class CallViewHolder extends RecyclerView.ViewHolder {

    private TextView txtName;
    private TextView txtAddress;

    CallViewHolder(@NonNull View itemView) {
        super(itemView);
        txtName = itemView.findViewById(R.id.txtName);
        txtAddress = itemView.findViewById(R.id.txtAddress);
    }
}
class EmailViewHolder extends RecyclerView.ViewHolder {

    private TextView txtName;
    private TextView txtAddress;

    EmailViewHolder(@NonNull View itemView) {
        super(itemView);
        txtName = itemView.findViewById(R.id.txtName);
        txtAddress = itemView.findViewById(R.id.txtAddress);
    }
}

Now code as below in onCreateViewHolder and onBindViewHolder method in recyclerview adapter:

@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
    View view;
    if (viewType == TYPE_CALL) { // for call layout
        view = LayoutInflater.from(context).inflate(R.layout.item_call, viewGroup, false);
        return new CallViewHolder(view);

    } else { // for email layout
        view = LayoutInflater.from(context).inflate(R.layout.item_email, viewGroup, false);
        return new EmailViewHolder(view);
    }
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
    if (getItemViewType(position) == TYPE_CALL) {
        ((CallViewHolder) viewHolder).setCallDetails(employees.get(position));
    } else {
        ((EmailViewHolder) viewHolder).setEmailDetails(employees.get(position));
    }
}

Upvotes: 3

Related Questions