gpt4 book ai didi

android - 如何从上下文菜单中获取有关 RecyclerView 项目的信息

转载 作者:太空狗 更新时间:2023-10-29 16:31:16 26 4
gpt4 key购买 nike

我有一个 RecyclerView,我已经为其实现了 ContextMenu,它在 RecyclerView 项的 onLongCLick 事件时触发。但是我无法获得在 onMenuItemClick(MenuItem item) 中创建 ContextMenu 的 RecyclerView 项目的位置。

我的适配器类中的代码:

   public static class PlaceViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnCreateContextMenuListener,MenuItem.OnMenuItemClickListener {
protected TextView vName;
protected TextView vDes;
protected ImageView vBanner;
CheckPlacesFragment fragmentCtx;
ArrayList<UserPlaces> places = new ArrayList<UserPlaces>();
Context ctx;
UserPlaces place;

public PlaceViewHolder(View v, Context ctx, ArrayList<UserPlaces> places, CheckPlacesFragment fragmentCtx) {
super(v);
v.setOnClickListener(this);
v.setOnCreateContextMenuListener(this);
this.places = places;
this.ctx = ctx;
this.fragmentCtx = fragmentCtx;
vName = (TextView) v.findViewById(R.id.place_name);
vDes = (TextView) v.findViewById(R.id.place_des);
vBanner = (ImageView) v.findViewById(R.id.banner_img);


}

@Override
public void onClick(View v) {
int position = getAdapterPosition();
place = this.places.get(position);
Toast.makeText(this.ctx,place.getName(),Toast.LENGTH_SHORT).show();


}


@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
MenuItem deleteAction = menu.add("Delete");
deleteAction.setOnMenuItemClickListener(this);
}

@Override
public boolean onMenuItemClick(MenuItem item) {

// int id = item.getItemId();
// ERROR : How to get item position which ContextMenu Created
switch (id) {
case 0 :
//Call delete Function from Fragment
fragmentCtx.test(pid);
return true;
case 1:

return true;

}
return false;
}
}

最佳答案

替换你的 onMenuItemClick()
public boolean onMenuItemClick(MenuItem item) {

int id = item.getItemId();
int recyclerId = getLayoutPosition();
place = this.places.get(recyclerId);
String pid=place.getId();
switch (id) {
case 0 :
//Call delete Function from Fragment
fragmentCtx.test(pid);
return true;
case 1:

return true;

}
return false;
}

关于android - 如何从上下文菜单中获取有关 RecyclerView 项目的信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38075224/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com