android – 在RecyclerView中添加删除按钮
作者:互联网
我正在尝试添加一个删除按钮,该按钮应该根据它在RecyclerView中的位置删除数据库中的项目.我正在努力解决这个问题.这是我的RecyclerView布局文件:
<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="Delete"
android:src="@drawable/icon_layout_delete_layout_item" />
这是我为ImageButton声明onItemClick的地方,它是删除按钮:
imageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getBaseContext(), "Delete hit!", Toast.LENGTH_SHORT).show();
}
});
这是我的RecyclerView监听器类:
private OnItemClickListener onItemClickListener;
public interface OnItemClickListener {
void onItemClick(View view, int position);
}
GestureDetector gestureDetector;
public ListsDatabaseItemListener(Context context, OnItemClickListener listener) {
onItemClickListener = listener;
gestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onSingleTapUp(MotionEvent motionEvent) {
return true;
}
});
}
@Override
public boolean onInterceptTouchEvent(RecyclerView recyclerView, MotionEvent motionEvent) {
View view = recyclerView.findChildViewUnder(motionEvent.getX(), motionEvent.getY());
if (view != null && onItemClickListener != null && gestureDetector.onTouchEvent(motionEvent)) {
onItemClickListener.onItemClick(view, recyclerView.getChildLayoutPosition(view));
return true;
}
return false;
}
@Override
public void onTouchEvent(RecyclerView recyclerView, MotionEvent motionEvent) {
}
@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
最后,这是我的适配器类:
List<ListsDatabaseList> list;
public ListsDatabaseListAdapter(List<ListsDatabaseList> list) {
this.list = list;
}
@Override
public ListsDatabaseListHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_list, parent, false);
ListsDatabaseListHolder holder = new ListsDatabaseListHolder(view);
return holder;
}
@Override
public void onBindViewHolder(ListsDatabaseListHolder holder, int position) {
holder.name.setText(list.get(position).getName());
holder.date.setText(list.get(position).getDate());
}
@Override
public int getItemCount() {
return list.size();
}
public static class ListsDatabaseListHolder extends RecyclerView.ViewHolder {
TextView name;
TextView date;
ImageButton delete;
public ListsDatabaseListHolder(View view) {
super(view);
this.name = (TextView) view.findViewById(R.id.layout_list_name);
this.date = (TextView) view.findViewById(R.id.layout_list_date);
this.delete = (ImageButton) view.findViewById(R.id.layout_list_delete);
}
}
有关如何实现这一目标的任何建议?
解决方法:
您可以在onBindViewHolder()方法中添加ImageButton单击侦听器:
@Override
public void onBindViewHolder(ListsDatabaseListHolder holder, final int position) {
holder.name.setText(list.get(position).getName());
holder.date.setText(list.get(position).getDate());
holder.delete.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
ListsDatabaseList theRemovedItem = list.get(position);
// remove your item from data base
list.remove(position); // remove the item from list
notifyItemRemoved(position); // notify the adapter about the removed item
}
});
}
标签:android,android-recyclerview,android-imagebutton 来源: https://codeday.me/bug/20190528/1167632.html