gpt4 book ai didi

android - 使用 onbindviewholder 在 cardview 中填充线性布局

转载 作者:行者123 更新时间:2023-11-29 00:08:03 27 4
gpt4 key购买 nike

我的 cardview 中有一个线性布局,而 cardview 又位于 recyclerview 中。我试图通过数组列表动态地将数据填充到线性布局中——其中 cardview 的每个线性布局中的元素数量将具有不同的大小。当我执行以下操作时,每次我将其滚动出 View 时都会调用 onBindViewHolder,一次又一次地将元素添加到线性布局中。这是回收站 View 的适配器。请告诉我如何以正确的方式添加这些元素,这样线性布局就不会过多。

public class CardRecyclerAdapter extends RecyclerView.Adapter<CardRecyclerAdapter.DataObjectHolder> {
private static String LOG_TAG = "MyRecyclerViewAdapter";
private ArrayList<ElementCard> cardElements;
private static MyClickListener myClickListener;
private Context context;

public static class DataObjectHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView title;
LinearLayout listView;
RelativeLayout relativeLayout;

public DataObjectHolder(View itemView) {
super(itemView);
title = (TextView) itemView.findViewById(R.id.card_title);
listView = (LinearLayout) itemView.findViewById(R.id.card_listview);
relativeLayout = (RelativeLayout) itemView.findViewById(R.id.card_rellay);
Log.i(LOG_TAG, "Adding Listener");
itemView.setOnClickListener(this);
}

@Override
public void onClick(View v) {
myClickListener.onItemClick(getAdapterPosition(), v);
}
}

public void setOnItemClickListener(MyClickListener myClickListener) {
this.myClickListener = myClickListener;
}

public CardRecyclerAdapter(ArrayList<ElementCard> myDataset, Context context) {
cardElements = myDataset;
this.context = context;
}

@Override
public DataObjectHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.element_card, parent, false);

DataObjectHolder dataObjectHolder = new DataObjectHolder(view);
return dataObjectHolder;
}

@Override
public void onBindViewHolder(DataObjectHolder holder, int position) {
holder.title.setText(cardElements.get(position).mTransactions.get(0).tCategory);
for (Transaction transaction : cardElements.get(position).mTransactions) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.element_listitem, null, false);
CheckBox checkBox = (CheckBox) view.findViewById(R.id.listitem_checkBox);
checkBox.setText(transaction.tSentence); // set sentence for now -> change later
checkBox.setChecked(transaction.tCompleted); // set checkbox completed or not

holder.listView.addView(view);
}
}

public void addItem(ElementCard dataObj, int index) {
cardElements.add(index, dataObj);
notifyItemInserted(index);
}

public void deleteItem(int index) {
cardElements.remove(index);
notifyItemRemoved(index);
}

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

@Override
public long getItemId(int position) {
return super.getItemId(position);
}

public interface MyClickListener {
public void onItemClick(int position, View v);
}
}

最佳答案

View 在滚动时会被回收,所以LinearLayout中仍然会包含之前绑定(bind)中添加的 subview 。您可以在 LinearLayout 上使用以下代码清除所有 View (只需将 View ID 替换为您在项目中使用的内容):

LinearLayout myLinearLayout = (LinearLayout) cardView.findViewById(R.id.my_linear_layout);
myLinearLayout.removeAllViews();

关于android - 使用 onbindviewholder 在 cardview 中填充线性布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32330735/

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