gpt4 book ai didi

java - 如何在android中的一个recyclerview中实现多个 View

转载 作者:行者123 更新时间:2023-11-29 04:06:45 26 4
gpt4 key购买 nike

如何在一个 Activity 中创建多个 View ,我曾使用多类型 View 尝试过但它不起作用 这是正确的方法还是我需要在一个 Activity 中添加多个 recyclerview 并根据过滤器隐藏 recyclerview 的可见性

           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 );}
}

我想要这种类型的功能,可以从数据库中搜索多个数据,正如您在 image 中看到的那样2

最佳答案

首先您必须创建 2 个布局 xml。之后在 recyclerview 适配器里面TYPE_CALL和TYPE_EMAIL是adapter类中分别为1和2的两个静态值。

现在在Recycler View Adapter类级别定义两个静态值例如 :private static int TYPE_CALL = 1;private static int TYPE_EMAIL = 2;

现在像这样创建具有多个 View 的 View 持有者:

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);
}
}

现在在 recyclerview 适配器的 onCreateViewHolder 和 onBindViewHolder 方法中的代码如下:

@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));
}
}

关于java - 如何在android中的一个recyclerview中实现多个 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58111834/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com