我试图开发一个应用程序(注:Pad),但是删除和编辑扣子对于每个不起作用的帖子,例如,如果我单击“删除”扣子对于POST编号4和每个帖子,POST编号0将删除,同样用于编辑。
怎么能得到position
每个职位RecyclerView
,它可以删除和编辑函数。
守则:
public class NoteAdapter extends RecyclerView.Adapter<NoteAdapter.NoteViewHolder> {
public static final int REQUEST_FOR_ACTIVITY_CODE = 102;
public static int itemPosition;
public static String state = "";
private Context context;
private List<Note> notes = new ArrayList<>();
Note note = new Note();
public NoteAdapter(Context context) {
this.context = context;
}
@NonNull
@Override
public NoteViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new NoteViewHolder(LayoutInflater.from(context).inflate(R.layout.layout_item_note, parent, false));
}
@Override
public void onBindViewHolder(@NonNull final NoteViewHolder holder, int position) {
holder.bindNote(notes.get(position));
holder.editButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
state = "edit";
Intent editintent = new Intent(context, NoteActivity.class);
editintent.putExtra("title", holder.textViewTitle.getText().toString());
editintent.putExtra("content", holder.textViewContent.getText().toString());
// context.startActivity(editintent);
((Activity) context).startActivityForResult(editintent, REQUEST_FOR_ACTIVITY_CODE);
}
});
holder.removeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
removeNote(itemPosition);
}
});
}
@Override
public int getItemCount() {
return notes.size();
}
public class NoteViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView textViewTitle;
public TextView textViewContent;
public ImageButton removeButton;
public ImageButton editButton;
public NoteViewHolder(View itemView) {
super(itemView);
textViewTitle = itemView.findViewById(R.id.txt_title);
textViewContent = itemView.findViewById(R.id.txt_note);
removeButton = itemView.findViewById(R.id.btn_remove);
editButton = itemView.findViewById(R.id.btn_edit);
}
public void bindNote(Note note) {
textViewTitle.setText(note.getTitle());
textViewContent.setText(note.getContent());
}
@Override
public void onClick(View v) {
itemPosition = note.getId();
}
}
public void setNote(Note note) {
notes.add(note);
notifyItemInserted(notes.size() - 1);
}
public void setEditNote(Note note) {
notes.set(itemPosition, note);
notifyDataSetChanged();
}
public void removeNote(int position) {
notes.remove(position);
notifyItemRemoved(position);
}
public void clearAll() {
notes = new ArrayList<>();
notifyDataSetChanged();
}
}
发布于 2018-06-11 17:26:26
将单击侦听器设置为NoteViewHolder,而不是NoteAdapter,以便在viewHolder调用中查找项目位置getAdapterPosition()
你的方法可以这样实现:
第一,创建接口NoteEditListener
interface NoteEditListener {
void edit(int position);
}
第二,实现此接口NoteAdapter
和覆盖edit
方法
public class NoteAdapter implements NoteEditListener
public void edit(int position){
// todo edit your item in position
}
第三,添加NoteEditListener
到viewHolder构造函数并传递this
从适配器
public class NoteViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView textViewTitle;
public TextView textViewContent;
public ImageButton removeButton;
public ImageButton editButton;
public NoteViewHolder(View itemView, final NoteEditListener listener) {
super(itemView);
textViewTitle = itemView.findViewById(R.id.txt_title);
textViewContent = itemView.findViewById(R.id.txt_note);
removeButton = itemView.findViewById(R.id.btn_remove);
editButton = itemView.findViewById(R.id.btn_edit);
editButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
listener.edit(getAdapterPosition());
}
}
}
https://stackoverflow.com/questions/-100004839
复制相似问题