为什么我的删除按钮在RecyclerView中不起作用?

时间:2016-05-15 04:34:48

标签: android android-studio android-recyclerview

我在实现删除按钮时遇到问题。如果单击它,它应该从RecyclerView中删除一个项目。我的布局文件包含两个TextView和一个像这样的ImageButton:

<TextView
    android:id="@+id/layout_list_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentStart="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:textStyle="bold"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
    android:id="@+id/layout_list_date"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignStart="@+id/layout_list_name"
    android:layout_alignLeft="@+id/layout_list_name"
    android:layout_below="@+id/layout_list_name" />

<ImageButton
    android:id="@+id/layout_list_delete"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:contentDescription="@string/layout_list_delete" />

这是我的适配器类,它管理RecyclerView的项目和功能:

List<RecyclerViewListSchema> list;

public RecyclerViewListAdapter(List<RecyclerViewListSchema> list) {
    this.list = list;
}

public static class RecyclerViewViewHolder extends RecyclerView.ViewHolder {
    TextView name;
    TextView date;
    ImageButton delete;

    public RecyclerViewViewHolder(View view) {
        super(view);

        name = (TextView) view.findViewById(R.id.layout_list_name);
        date = (TextView) view.findViewById(R.id.layout_list_date);
        delete = (ImageButton) view.findViewById(R.id.layout_list_delete);
    }
}

@Override
public RecyclerViewViewHolder onCreateViewHolder(ViewGroup viewGroup, int position) {
    View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.layout_list, viewGroup, false);
    RecyclerViewViewHolder viewHolder = new RecyclerViewViewHolder(view);

    return viewHolder;
}

@Override
public void onBindViewHolder(RecyclerViewViewHolder viewHolder, final int position) {
    viewHolder.name.setText(list.get(position).name);
    viewHolder.date.setText(list.get(position).date);
    viewHolder.delete.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            RecyclerViewListSchema item = list.get(position);
            list.remove(position);
            notifyItemRemoved(position);
        }
    });
}

@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
    super.onAttachedToRecyclerView(recyclerView);
}

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

更新的 这是我的删除功能用于删除项目的确切位置:

@Override
public void onBindViewHolder(RecyclerViewViewHolder viewHolder, final    int position) {
viewHolder.name.setText(list.get(position).name);
viewHolder.date.setText(list.get(position).date);
viewHolder.delete.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        RecyclerViewListSchema item = list.get(position);
        list.remove(position);
        notifyItemRemoved(position);
    }
});
}

我的RecyclerView使用旁边的按钮填充SQLite数据库中的所有项目。我试着点击它,但它没有做任何事情。我的代码中发生了什么?我做错了什么吗?关于如何解决这个问题的任何建议?

2 个答案:

答案 0 :(得分:2)

将您的onClick更改为:

public void onClick(View view) {
    RecyclerViewListSchema item = list.get(position);
    list.remove(position);
    notifyItemRemoved(position);
    notifyItemRangeChanged(position, getItemCount());
}

答案 1 :(得分:1)

您可以在删除期间依赖位置。这样做。

@Override
public void onBindViewHolder(RecyclerViewViewHolder viewHolder, int position) {
    final RecyclerViewListSchema item = list.get(position);
    viewHolder.name.setText(item.name);
    viewHolder.date.setText(item.date);
    viewHolder.delete.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            int pos = list.indexOf(item);
            list.remove(pos);
            notifyItemRemoved(pos);
        }
    });
}