如何在 Firebase 中將多個 RecycleView 設定為單個配接器?
我正在創建一個應用程式,我的應用程式中有很多類別,創建多個配接器、模型和 RecycleView 使應用程式變得復雜。很難為多個 RecycleView 制作不同的配接器。
我可以將多個 RecycleView 設定為一個配接器嗎?通過使用 if 或 else 或 switch case 如何做到這一點?
或者有什么替代方案?
第一類模型
public class FirstModel {
private String Image, Text;
FirstModel() {
}
public FirstModel(String image, String text) {
Image = image;
Text = text;
}
public String getImage() {
return Image;
}
public void setImage(String image) {
Image = image;
}
public String getText() {
return Text;
}
public void setText(String text) {
Text = text;
}
}
第二類模型
public class SecondModel {
private String Image, Text;
SecondModel() {
}
public SecondModel(String image, String text) {
Image = image;
Text = text;
}
public String getImage() {
return Image;
}
public void setImage(String image) {
Image = image;
}
public String getText() {
return Text;
}
public void setText(String text) {
Text = text;
}
鏈接到一個配接器
public class Adapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private final int FirstLayout = 0;
private final int SecondLayout = 1;
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view;
if (viewType == FirstLayout) {
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.sample_categories, null);
return new GiftsForHerViewHolder(view);
} else {
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.sample_categories, null);
return new GiftsForHimViewHolder(view);
}
}
@Override
public int getItemViewType(int position) {
????
return super.getItemViewType(position);
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
switch (holder.getItemViewType()) {
case 1:
?????
}
}
@Override
public int getItemCount () {
return ???? .size();
}
class FirstViewHolder extends RecyclerView.ViewHolder {
private ImageView Image;
private TextView Text;
public FirstViewHolder(@NonNull View itemView) {
super(itemView);
Image = (ImageView) itemView.findViewById(R.id.Image);
Text = (TextView) itemView.findViewById(R.id.Text);
}
}
class SecondViewHolder extends RecyclerView.ViewHolder {
public SecondViewHolder(@NonNull View itemView) {
super(itemView);
}
}
}
uj5u.com熱心網友回復:
當您想使用回收視圖和配接器在串列中顯示專案時,這些專案通常具有相似的 UI 并且基于相同型別的資料(例如汽車、人員、建筑物等)。在單個配接器中混合不同的物體不是一個好主意。資料源可以有不同的長度,串列項會有不同的 UI。因此,堅持這個想法 - 一個帶有一個配接器的回收視圖。
uj5u.com熱心網友回復:
編程中最重要的思想之一是可重用性,我參考“在計算機科學和軟體工程中,可重用性是在軟體產品開發程序中以某種形式使用現有資產”。
因此,如果唯一不同的是模型類和布局檔案,那么您可以創建單個配接器類。解決這個問題的關鍵是使用泛型。這是一個作業示例:
- 創建通用基礎配接器?
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/369206.html
上一篇:如何在狀態中設定監聽器?
