首页 > 解决方案 > 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());
        }

    }
}

`

标签: androidandroid-recyclerview

解决方案


声明一个名为 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变量。所以,试试上面的代码。祝你好运。:)


推荐阅读