如何为单个 RecyclerView 行更改 ImageView 可绘制对象

How To Change ImageView drawable For A Single RecyclerView Row

这个问题我不是很清楚怎么问,就用图来说明吧

在我的应用程序中,下载图标的onClick 调用行中视频的下载方法。下载完成后,图标从黑色变为绿色。布尔标志用于在 SharedPreference 中保存此状态。这个保存的状态在我的 RecyclerView Adapter 中再次调用,所以下载的状态可以反映应用程序重新启动的时间。

挑战是...

当应用程序重新启动时,只有下载的行图标显示为绿色,即使未下载,每一行的图标也会变为绿色。下面是我的代码。

class DownloadReceiver extends ResultReceiver { //DownloadReceiver class
                public DownloadReceiver(Handler handler) {
                    super(handler);
                }

                @Override
                protected void onReceiveResult(int resultCode, Bundle resultData) {
                    super.onReceiveResult(resultCode, resultData);
                    if (resultCode == TichaDownloadService.UPDATE_PROGRESS) {
                        int progress = resultData.getInt("progress"); //get the progress
                        //Set the progress to progressBarCir
                        progressBarCir.setProgress(progress);
                        icon_download.setVisibility(View.GONE);
                        progressBarCir.setVisibility(View.VISIBLE);
                        Log.i("STATUS", "DOWNLOADING>>>");
                        if (progress == 100) { // Downloade process is completed
                            isNotDownloaded = false; // Flagging that the video has been downloaded
                            progressBarCir.setVisibility(View.GONE);
                            // Setting the Download Icon to reflect the New color state
                            icon_download.setColorFilter(itemView.getContext().getResources().getColor(R.color.funnygreen));
                            icon_download.setVisibility(View.VISIBLE);
                            // Saving the boolean flag in SharedPreferece
                            SharedPreferences sharedPreferences = getSharedPreferences("com.example.instagramclone",MODE_PRIVATE);
                            sharedPreferences.edit().putBoolean("isDownloadedState",isNotDownloaded).commit();
                            
                            // Logging of the save state to confirm state is saved.
                            boolean newState= sharedPreferences.getBoolean("isDownloadedState",true);
                            Log.i("STATE XCHANGE", "DOWNLOADED HENCE, "+String.valueOf(newState));

                        }
                    } else {
                        Log.i("STATUS", " NOT DOWNLOADING,BOSS");
                    }
                }
            }

下面是我的适配器的 Holder 部分的片段 Class

  public LectureClassesHolder(@NonNull final View itemView) {
        super(itemView);
        // Now we ref each custom layout view item using the itemView
        textViewTitle = itemView.findViewById(R.id.subject_topic);
        textViewDescription = itemView.findViewById(R.id.subject_description);
        textViewDuration = itemView.findViewById(R.id.subject_duration);
        imageViewDownload = itemView.findViewById(R.id.download);
        textViewUrl = itemView.findViewById(R.id.url_link);

        SharedPreferences sharedPreferences = itemView.getContext().getSharedPreferences("com.example.instagramclone",Context.MODE_PRIVATE);
        isNotDownloaded = sharedPreferences.getBoolean("isDownloadedState",true);

        if (isNotDownloaded){
            imageViewDownload.setColorFilter(itemView.getContext().getResources().getColor(R.color.black));
            Log.i("DOWNLOAD STATE ","NOT Downloaded State is "+ isNotDownloaded);
        }else {
            imageViewDownload.setColorFilter(itemView.getContext().getResources().getColor(R.color.funnygreen));
            Log.i("DOWNLOAD STATE ","NOT Downloaded State is "+ isNotDownloaded);
        }

        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = getAdapterPosition();
                if (position != RecyclerView.NO_POSITION && listener != null) {
                    listener.onItemClick(getSnapshots().getSnapshot(position), position, itemView);
                }
            }
        });

        // Incase e no work
        imageViewDownload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = getAdapterPosition();
                if (position != RecyclerView.NO_POSITION && listener != null) {
                    listener.onViewItemClick(getSnapshots().getSnapshot(position), position, itemView);
                }
            }
        });
    }

我需要一种方法使更改仅对相关行有效。非常感谢有关如何实现这一目标的任何帮助。

问题当然是个人记录和存储的内容之间没有关联。最好的解决方案是使用一个数据库 table 至少包含一个用于下载的列 url 和另一个状态,存储具有所需状态的记录(例如,使用 0 表示未下载状态,使用 1 表示下载状态)并在下载后使用 url 更新行的状态列,然后您可以查询检查记录的状态。然而,在仍然使用共享首选项的同时快速修复您的解决方案是将链接存储在一个数组中并检查该数组,请参阅:

 public static String all_records(Context act)
{

    SharedPreferences prefs = act.getSharedPreferences("SHARED_PREFS_NAME", act.MODE_PRIVATE);

    return prefs.getString("all_records", "[]");
 
 
    
}
 public static void add_download(Activity act,String url)
{
    JSONArray ja=new JSONArray();
    try {
    ja=new JSONArray(all_records(act));
        JSONObject jo=new JSONObject();
        jo.put("url",url);
        ja.put(jo);
    } catch (JSONException e) {
        e.printStackTrace();
    }
    SharedPreferences.Editor saver =act.getSharedPreferences("SHARED_PREFS_NAME", act.MODE_PRIVATE).edit();



    saver.putString("all_records",ja.toString());
    saver.commit();


}
public static boolean is_downloaded(Activity act,String url)
{
    JSONArray ja=new JSONArray();
    try {
    ja=new JSONArray(all_records(act));
        for (int i=0;i<ja.length();i++)
        {
            try {
                Log.e("Check ", "" + ja.getJSONObject(i).getString("url") + " Against " + url);
                if (ja.getJSONObject(i).getString("url").equalsIgnoreCase(url)) {
                    return true;
                }
            }catch (Exception ex){}
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }



    return false;
}

在这种情况下,您可以像这样在下载时使用它

protected void onReceiveResult(int resultCode, Bundle resultData) {
                super.onReceiveResult(resultCode, resultData);
                if (resultCode == TichaDownloadService.UPDATE_PROGRESS) {
                    int progress = resultData.getInt("progress"); //get the progress
                    //Set the progress to progressBarCir
                    progressBarCir.setProgress(progress);
                    icon_download.setVisibility(View.GONE);
                    progressBarCir.setVisibility(View.VISIBLE);
                    Log.i("STATUS", "DOWNLOADING>>>");
                    if (progress == 100) { // Downloade process is completed
                        isNotDownloaded = false; // Flagging that the video has been downloaded
                        progressBarCir.setVisibility(View.GONE);
                        // Setting the Download Icon to reflect the New color state
                        icon_download.setColorFilter(itemView.getContext().getResources().getColor(R.color.funnygreen));
                        icon_download.setVisibility(View.VISIBLE);
                        // Saving the boolean flag in SharedPreferece
                        add_download(/*your context*/,/*your url*/);

                    }
                } else {
                    Log.i("STATUS", " NOT DOWNLOADING,BOSS");
                }
            }

在您的回收视图适配器上的 onbindview 或任何您想要检索是否已下载状态的地方 call is_downloaded(/*YOUR CONTEXT*/,"YOUR URL");