java - 在另一个 RecyclerView 中填充 RecyclerView
问题描述
我是 RecyclerView 的新手。我创建了一个 RecyclerView,这个 RecycerView 显示了某人的所有订单。当有人单击 RecyclerView 中的一行时(当有人单击订单时),应在显示订单详细信息的位置显示一个弹出窗口。弹出窗口有效,但 RecyclerView 未填满,因为 RecyclerView 无法识别(经典 NullPointerException :D)。我的问题是 - 我怎样才能填写适配器内的 RecylerView。你可以在下面看到我的代码。
我创建了它应该是什么样子的“图片”。RecyclerView 与订单一起显示。当我单击某个订单时,应显示一个带有详细订单的弹出窗口。
非常感谢您!祝你有美好的一天/晚上。
适配器里面我想要什么
public class UserBestellAdapter extends RecyclerView.Adapter<UserBestellAdapter.ViewHolder> {
ArrayList<Bestellung> bestellung;
Context mContext;
Dialog epicDialog;
UserBestellAdapter.ViewHolder viewHolder;
ArrayList<ModelOverviewOrder> orderList;
RecyclerView recyclerView;
public UserBestellAdapter(Context context, ArrayList<Bestellung> list) {
mContext = context;
bestellung = list;
epicDialog = new Dialog(mContext);
}
@NonNull
@Override
public UserBestellAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mContext).inflate(R.layout.adapter_bestell, parent, false);
viewHolder = new UserBestellAdapter.ViewHolder(view);
return viewHolder;
}
@NonNull
@Override
public void onBindViewHolder(@NonNull UserBestellAdapter.ViewHolder holder, final int position) {
//Gesamtpreis: holder.item_betrag.setText(String.valueOf(bestellung.get(position).getBetrag()));
// Datum: holder.item_datum.setText(bestellung.get(position).getDatum());
holder.item_items.setText(bestellung.get(position).getProdukte());
//holder.item_code.setText(bestellung.get(position).getBestellnummer());
String bestellid =bestellung.get(position).getBestellnummer() + "";
holder.item_code.setText(bestellid);
holder.item_betrag.setText(Double.toString(bestellung.get(position).getSumme()));
holder.item_datum.setText(bestellung.get(position).getDatum());
holder.layout_user_bestellung.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
recyclerView = view.findViewById(R.id.recyclerview_order_scroll);
orderList = new ArrayList<>();
orderList.add(new ModelOverviewOrder("Toast", "5", "6.0"));
LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
RecyclerView.LayoutManager rvLayoutManager = layoutManager;
recyclerView.setLayoutManager(rvLayoutManager);
OrderOverviewAdapter adapter = new OrderOverviewAdapter(mContext, orderList);
recyclerView.setAdapter(adapter);
TextView order_overview_number = view.findViewById(R.id.order_overview_number);
System.out.println("------>" + order_overview_number);
epicDialog.setContentView(R.layout.user_popup_order_overview);
epicDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
Button btn_order_overview_finish = (Button) epicDialog.findViewById(R.id.btn_order_overview_finish);
//System.out.println(bestellung.get(position).getBestellnummer());
getBestellung(bestellung.get(position).getBestellnummer());
btn_order_overview_finish.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
epicDialog.dismiss();
}
});
epicDialog.show();
}
});
}
@Override
public int getItemCount() {
return bestellung.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
private TextView item_items, item_betrag, item_datum, item_code;
private ConstraintLayout layout_user_bestellung;
public ViewHolder(@NonNull View itemView) {
super(itemView);
item_items = itemView.findViewById(R.id.items);
item_betrag = itemView.findViewById(R.id.betrag);
item_datum = itemView.findViewById(R.id.datum);
item_code = itemView.findViewById(R.id.code);
layout_user_bestellung = itemView.findViewById(R.id.layout_user_bestellung);
}
}
}
适配器类
public class OrderOverviewAdapter extends RecyclerView.Adapter<OrderOverviewAdapter.ViewHolder> {
private Context mContext;
private ArrayList<ModelOverviewOrder> mlist;
OrderOverviewAdapter(Context context, ArrayList<ModelOverviewOrder> list) {
mContext = context;
mlist = list;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(mContext);
View view = layoutInflater.inflate(R.layout.user_popup_order_overview_adapter, parent, false);
OrderOverviewAdapter.ViewHolder viewHolder = new OrderOverviewAdapter.ViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
TextView artikel = holder.item_artikel;
TextView preis = holder.item_preis;
TextView anzahl = holder.item_anzahl;
ModelOverviewOrder artikelItem = mlist.get(position);
artikel.setText(artikelItem.getArtikel());
preis.setText(artikelItem.getPreis());
anzahl.setText(artikelItem.getAnzahl());
}
@Override
public int getItemCount() {
return mlist.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView item_preis, item_anzahl, item_artikel;
public ViewHolder(@NonNull View itemView) {
super(itemView);
item_preis = itemView.findViewById(R.id.order_overview_preis);
item_anzahl = itemView.findViewById(R.id.order_overview_anzahl);
item_artikel = itemView.findViewById(R.id.order_overview_artikel);
}
}
}
适配器 XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">
<TextView
android:id="@+id/order_overview_artikel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Artikel"
android:layout_marginRight="10dp"/>
<TextView
android:id="@+id/order_overview_anzahl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Anzahl"
android:layout_marginRight="10dp"/>
<TextView
android:id="@+id/order_overview_preis"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Preis" />
</LinearLayout>
弹出 XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:gravity="center">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
app:cardBackgroundColor="@color/white"
app:cardCornerRadius="15dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:layout_marginBottom="25dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:text="Bestellübersicht"
android:textAlignment="center"
android:textSize="25dp"
android:textStyle="bold"></TextView>
<TextView
android:id="@+id/order_overview_number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:text="0"
android:textAlignment="center"
android:textSize="25dp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="10dp"
android:text="Produkt"
android:textAlignment="center"
android:textSize="18dp"
android:textStyle="bold">
</TextView>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:text="Anzahl"
android:textAlignment="center"
android:textSize="18dp"
android:textStyle="bold">
</TextView>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:text="Preis"
android:textAlignment="center"
android:textSize="18dp"
android:textStyle="bold">
</TextView>
</LinearLayout>
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="true"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerview_order_scroll"
android:layout_width="match_parent"
android:layout_height="match_parent">
</androidx.recyclerview.widget.RecyclerView>
</androidx.core.widget.NestedScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
<com.airbnb.lottie.LottieAnimationView
android:layout_width="match_parent"
android:layout_height="128dp"
app:lottie_autoPlay="true"
app:lottie_loop="true"
app:lottie_rawRes="@raw/walkingburger" />
<Button
android:id="@+id/btn_order_overview_finish"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:layout_marginRight="10dp"
android:background="@drawable/button_order_checkout"
android:backgroundTint="#9BC3BF"
android:elevation="16dp"
android:text="FERTIG"
android:textColor="#FFFFFF"
android:textStyle="bold"
app:layout_constraintVertical_bias="0.777" />
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
</RelativeLayout>
模型类
public class ModelOverviewOrder {
private String artikel, anzahl, preis;
public ModelOverviewOrder(String artikel, String anzahl, String preis) {
this.artikel = artikel;
this.anzahl = anzahl;
this.preis = preis;
}
public String getArtikel() {
return artikel;
}
public void setArtikel(String artikel) {
this.artikel = artikel;
}
public String getAnzahl() {
return anzahl;
}
public void setAnzahl(String anzahl) {
this.anzahl = anzahl;
}
public String getPreis() {
return preis;
}
public void setPreis(String preis) {
this.preis = preis;
}
}
解决方案
您要显示的弹出对话框必须包含(膨胀)第二个 recyclerView 并且因为它有点复杂的 UI/UX 我建议您应该将弹出窗口设为自定义视图
示例:https ://medium.com/@makkenasrinivasarao1/android-custom-dialog-with-list-of-items-ba1ab0e78e16
推荐阅读
- excel - 使用表格中的行和列值查找值的 Excel 公式
- java - Android Java:EditText 文本选择缺少上下文菜单
- c# - C# Datetime - 添加天或月
- python - Python 的 Qt Web GUI 替代品
- python - AttributeError: Cutome User 模型对象没有属性“密码”
- python - 错误:找不到满足要求的版本 win32console
- android - Android Dark Theme - 在应用程序上实现深色主题的最佳方式
- node.js - Heroku postgres 查询仅返回由节点应用程序创建的行
- swift - UIVisualEffectView 在 effect = nil 后不会消失
- python - 使用 DBSCAN 聚类过程的奇怪结果