android 如何在不使用适配器方法中的RecyclerView示例的情况下迭代onBindViewHolder中的项?

gfttwv5a  于 2023-03-16  发布在  Android
关注(0)|答案(1)|浏览(114)

我在我的应用中实现了一个奖励广告功能,点击一个锁图标会显示一个广告,成功完成广告后,我想用复制图标替换整个列表中的所有锁图标。但是,我目前用于替换图标的代码需要使用一个RecyclerView变量,并且需要在适配器方法中示例化该变量。因为我只在主活动中使用列表和上下文,所以导致了一些问题。我为语法错误道歉,我已经尽了最大努力来解释这种情况。
下面是代码

private final RecyclerView recyclerView;

    public MemeAdapter(List<String> memeList, Context mContext, RecyclerView recyclerView) {
        this.memeList = memeList;
        this.context = mContext;
        this.rewardGranted = recyclerView;

        mSharedPrefs = context.getSharedPreferences(SHARED_PREFS_NAME, Context.MODE_PRIVATE);
        rewardGranted = mSharedPrefs.getBoolean(REWARD_GRANTED_KEY, false);
    }

在装订夹内

@Override
                                public void onAdDismissedFullScreenContent() {
                                    // Called when ad is dismissed.
                                    // Set the ad reference to null so you don't show the ad a second time.
                                    Log.d(TAG, "Ad dismissed fullscreen content.");
                                    mRewardedVideoAd = null;
                                    for (int i = 0; i < memeList.size(); i++) {
                                        // Get the corresponding view holder for this item
                                        RecyclerView.ViewHolder holder = recyclerView.findViewHolderForAdapterPosition(i);

                                        // Update the icons for this item
                                        if (holder instanceof MemeAdapter.MemeViewHolder) {
                                            MemeAdapter.MemeViewHolder itemHolder = (MemeAdapter.MemeViewHolder) holder;
                                            itemHolder.copyImageView.setVisibility(View.VISIBLE);
                                            itemHolder.lockImageView.setVisibility(View.GONE);
                                        }
                                    }

//                                    holder.copyImageView.setVisibility(View.VISIBLE);
//                                    holder.lockImageView.setVisibility(View.GONE);
                                    mSharedPrefs.edit().putBoolean(REWARD_GRANTED_KEY, rewardGranted).apply();
                                }

她是我得到的错误

error: constructor MemeAdapter in class MemeAdapter cannot be applied to given types;
        MemeAdapter adapter = new MemeAdapter(asciiArts, this);
                              ^
  required: List<String>,Context,RecyclerView
  found: List<String>,MainActivity
  reason: actual and formal argument lists differ in length
v9tzhpje

v9tzhpje1#

recyclerview的所有更新都应该是由适配器引起的--这是recycler的“唯一真实来源”。基本思想是当你调用notifyXxxChanged时,你告诉适配器一些数据发生了变化,你想更新显示。它会为所有发生变化的数据调用onBindViewHolder,允许你选择一种新的方式来显示它。
作为起点,您可以执行以下操作:
将数据存储为List<Pair<String, Boolean>> memeList,第一个元素是之前存储的字符串,第二个元素是一个布尔标志,指示要显示哪个图像。
更新onAdDismissedFullScreenContent内存储的数据:

@Override
public void onAdDismissedFullScreenContent() {
    // Called when ad is dismissed.
    // Set the ad reference to null so you don't show the ad a second time.
    Log.d(TAG, "Ad dismissed fullscreen content.");
    mRewardedVideoAd = null;
    for (int i = 0; i < memeList.size(); i++) {
        memeList[i].second = true;
    }
    notifyDataRangeChanged(0, memeList.size()); // tell adapter to update all data. More efficient than notifyDatasetChanged
    mSharedPrefs.edit().putBoolean(REWARD_GRANTED_KEY, rewardGranted).apply();
}

onBindViewHolder内部显示正确图像:

@Override
public void onBindViewHolder(...) {
    ...
    if (memeList[position].second) {
        itemHolder.copyImageView.setVisibility(View.VISIBLE);                              
        itemHolder.lockImageView.setVisibility(View.GONE);
    } else {
        itemHolder.copyImageView.setVisibility(View.GONE);                              
        itemHolder.lockImageView.setVisibility(View.VISIBLE);
    }
}

相关问题