使用onbindviewholder

时间:2015-09-01 11:44:01

标签: android android-recyclerview

我的cardview中有一个线性布局,而这个布局又在Recyclerview中。我试图通过动态arraylist将数据填充到线性布局中 - 其中cardview的每个线性布局中的元素数量将具有不同的大小。当我执行以下操作时,它会导致每次将其滚出视图时调用onBindViewHolder,一次又一次地将元素添加到线性布局中。这是回收者视图的适配器。请告诉我如何以正确的方式添加这些元素,以便线性布局不会过多。

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

1 个答案:

答案 0 :(得分:2)

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

{{1}}