android - RecyclerView 中的弹出菜单在不同的项目中工作
问题描述
我有 RecyclerView,除了信息之外,每个项目都有一个带有三个点的按钮,代表一个弹出菜单。当用户单击该按钮时,弹出菜单会显示更多信息。项目的一部分变得可见。我不能使用消耗性 RecyclerView 我必须使用这个。但问题是当我点击它时,它只会展开和折叠我的 RecyclerView 上的最后一项,而不是点击它。我试过这个:为 RecyclerView-Item 创建选项菜单和其他一些解决方案,我的代码是基于这个 SO question。
我的代码:
@Override
public void onBindViewHolder(@NonNull AccountsHolder holder, int position) {
AccountsModel model = list.get(position);
holder.bind(model);
holder.binding.moreInfoIB.setOnClickListener(v -> {
//creating a popup menu
PopupMenu popup = new PopupMenu(context, v);
//inflating menu from xml resource
popup.inflate(R.menu.assets_more_info_menu);
popup.setOnMenuItemClickListener(item -> {
Log.i("ItemID: ", String.valueOf(item.getItemId()));
if (item.getItemId() == R.id.item1) {
if (isExpanded) {
collapse(group);
isExpanded = false;
} else {
isExpanded = true;
expand(group);
}
} else {
return false;
}
return true;
});
//displaying the popup
popup.show();
});
}
编辑我的整个适配器类:`
public class CashAccAdapter extends RecyclerView.Adapter<CashAccAdapter.AccountsHolder> {
private List<AccountsModel> list;
private Context context;
private boolean isExpanded;
private Group group;
//SparseBooleanArrays map integers to booleans.
private SparseBooleanArray expandedList = new SparseBooleanArray();
public CashAccAdapter(List<AccountsModel> list, Context context) {
this.list = list;
this.context = context;
}
@NonNull
@Override
public AccountsHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
ItemDetailedListAssetsBinding binding = ItemDetailedListAssetsBinding.inflate(layoutInflater, parent, false);
group = binding.group;
return new AccountsHolder(binding);
}
@Override
public void onBindViewHolder(@NonNull AccountsHolder holder, int position) {
AccountsModel model = list.get(position);
holder.bind(model);
holder.binding.moreInfoIB.setOnClickListener(v -> {
//creating a popup menu
PopupMenu popup = new PopupMenu(context, v);
//inflating menu from xml resource
popup.inflate(R.menu.assets_more_info_menu);
popup.setOnMenuItemClickListener(item -> {
Log.i("ItemID: ", String.valueOf(item.getItemId()));
if (item.getItemId() == R.id.item1) {
if (expandedList[position]) {
collapse(group);
expandedList[position] = false;
} else {
expandedList [position]= true;
expand(group);
}
} else {
return false;
}
return true;
});
//displaying the popup
popup.show();
});
}
@Override
public int getItemCount() {
return list != null ? list.size() : 0;
}
class AccountsHolder extends RecyclerView.ViewHolder {
private ItemDetailedListAssetsBinding binding;
public AccountsHolder(@NonNull ItemDetailedListAssetsBinding binding) {
super(binding.getRoot());
this.binding = binding;
}
public void bind(final AccountsModel modelClass) {
binding.setListAssets(modelClass);
binding.executePendingBindings();
binding.accNameValueTV.setText(modelClass.getAccName());
binding.accTypeValueTV.setText(modelClass.getAccType());
binding.accCurentValueTV.setText("$" + modelClass.getCurrentValue());
binding.accNumberValueTV.setText(modelClass.getAccNumber());
}
}
}
`
解决方案
声明一个名为 SparseBooleanArrayexpandedList
并将这部分更改为
popup.setOnMenuItemClickListener(item -> {
Log.i("ItemID: ", String.valueOf(item.getItemId()));
if (item.getItemId() == R.id.item1) {
if (expanded.get(position)) {
collapse(group);
expandedList.put(position, false);
} else {
expandedList.put(position, true);
expand(group);
}
this.notifyItemChanged(position);
} else {
return false;
}
return true;
});
只有最后一项更改,因为您使用的是单个isExpanded
变量。所以,试试上面的代码。祝你好运。:)
推荐阅读
- ms-access - MS Access:当基础查询具有参数时,报表中的图表为空白
- javascript - 获取 ajax 请求以更新 SQL 表中的列
- python - 使用 TemplateView 将模型的字符串参数传递给 URL
- android - Android 8 上的 AltBeacon 首次运行速度很慢(应用程序没有响应)
- reactjs - 我得到了两个我无法解决的错误作为回报 ESLint 和破坏
- android - 我删除了所有通话记录权限,并且我的应用未在 Play 商店中显示
- c - 在 while 循环条件中读取大小为 1 的无效
- java - 在 Hibernate 中创建 sessionFactory 失败(如何使用 hibernate 将对象保存到 Oracle 数据库中)
- here-api - iOS 初学者的 HERE API 许可证密钥
- javascript - JavaScript 中的 Promise 问题