我是android的新手,目前我正致力于recyclerView
实施。问题是我想在调用onCreateContextMenu后删除一个项目。我在获取对删除选项的正确引用时遇到了一些问题。
持有人类
public class UploadDrugsHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnCreateContextMenuListener, MenuItem.OnMenuItemClickListener {
//OUR VIEWS
UploadDrugsAdapter uploadDrugsAdapter;
TextView drugName;
ImageView drugImage;
ItemClickListener itemClickListener;
ArrayList<UploadedDrugs> drugs;
public UploadDrugsHolder(View itemView) {
super(itemView);
this.drugName= (TextView) itemView.findViewById(R.id.shopName);
this.drugImage= (ImageView) itemView.findViewById(R.id.model_menu);
itemView.setOnClickListener(this);
itemView.setOnCreateContextMenuListener(this);
}
public void setItemClickListener(ItemClickListener itemClickListener) {
this.itemClickListener=itemClickListener;
}
@Override
public void onClick(View v) {
this.itemClickListener.onItemClick(v,getLayoutPosition());
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
MenuItem myActionItem = menu.add("delete");
myActionItem.setOnMenuItemClickListener(this);
}
@Override
public boolean onMenuItemClick(MenuItem item) {
// what do i need to write here?
Toast.makeText(itemView.getContext(), "Drug deleted successfully", Toast.LENGTH_SHORT).show();
return false;
}
}
答案 0 :(得分:0)
类似的东西:
drugs.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position, drugs.size());
答案 1 :(得分:0)
我总是将适配器和支架放在同一个文件中 你应该这样做:
public class UploadedDrugsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private List<UploadedDrugs> dataList = new ArrayList<>();
private OnClickUploadedDrugs event;
public UploadedDrugsAdapter (List<UploadedDrugs> dataList, OnClickUploadedDrugs event) {
this.dataList = dataList;
this.event = event;
}
public interface OnClickUploadedDrugs{
void onClickUploadedDrugs(UploadedDrugs uploadedDrugs);
}
public void changeDataSet(List<UploadedDrugs> dataSet){
dataList = dataSet;
notifyDataSetChanged();
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.your_xml_file, parent, false);
return new ViewHolderUploadedDrugs(view);
}
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
try {
final UploadedDrugs model = dataList.get(position);
final ViewHolderUploadedDrugs vh = (ViewHolderUploadedDrugs) holder;
// configure view values
//Event
vh.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(event != null){
event.onClickUploadedDrugs(model);
}
}
});
}catch (Exception e){
Log.e(getClass().getSimpleName(), "Erro ao exibir informaçeõs", e);
}
}
@Override
public int getItemCount() {
return (null != dataList ? dataList.size() : 0);
}
static class ViewHolderUploadedDrugs extends RecyclerView.ViewHolder {
TextView drugName;
ImageView drugImage;
ViewHolderUploadedDrugs(View itemView) {
super(itemView);
this.drugName= (TextView) itemView.findViewById(R.id.shopName);
this.drugImage= (ImageView) itemView.findViewById(R.id.model_menu);
}
}
public List<UploadedDrugs> getDataList() {
return dataList;
}
public void setEvent(OnClickUploadedDrugs event) {
this.event = event;
}
}