[英]Item Position in RecyclerView and SharedPreferences
我有一個 RecyclerView,當用戶點擊任何項目時,它會打開一個用戶必須完成一些任務的活動,如果用戶完成了任務,它將被記錄在 SharePrefrences(到這里沒有問題)然后如果用戶返回到 recyclerView 一個我已經在布局中定義的圖像將是可見的,這意味着這一步已經完成。 但是當用戶在完成任務后返回到 recyclerView 時,問題就出現了,除了所選項目,其他一些項目也在圖像中可見。
比如我點擊item 1,完成所有任務返回recyclerView,除了item 1,item 8, 16, 24 也可以看到圖片。 這是查看所需的代碼:
在任務活動中:
private void trophyGiftRecycler() {
if ((Integer)img_test1_p1.getTag()==R.drawable.alph1_a) {
SharedPreferences prefs1 = getSharedPreferences("prefs1", MODE_PRIVATE);
boolean letter1 = prefs1.getBoolean("letter1", true);
if (letter1) {
trophyShared();
}
}
}
private void trophyShared() {
if ((Integer)img_test1_p1.getTag()==R.drawable.alph1_a) {
SharedPreferences prefs1 = getSharedPreferences("prefs1", MODE_PRIVATE);
SharedPreferences.Editor editor = prefs1.edit();
editor.putBoolean("letter1", true);
editor.apply();
}
}
In the MainAdapter
@Override
public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {
holder.itemLetter.setImageResource(mainModelAlphabets.get(position).getItemImages());
holder.itemTxt.setImageResource(mainModelAlphabets.get(position).getItemTxt());
holder.itemLetter.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(canStart) {
canStart = false; // do canStart false
// Do what you don't want to run twice due to double tap
if (position==0){
Activity activity = (Activity) context;
Intent intent = new Intent(context,Alphabet2.class);
intent.putExtra("alphabet_word1",R.drawable.alphabet_a1);
intent.putExtra("alphabet_word2",R.drawable.alphabet_a2);
intent.putExtra("alphabet_word3",R.drawable.alphabet_a3);
activity.startActivity(intent);
activity.overridePendingTransition(R.anim.activity_slide_in_left,R.anim.activity_slide_out_right);
Runtime.getRuntime().gc();
activity.onBackPressed();
}
if (position==1){
Activity activity = (Activity) context;
Intent intent = new Intent(context,Alphabet2.class);
intent.putExtra("alphabet_word1",R.drawable.alphabet_b1);
intent.putExtra("alphabet_word2",R.drawable.alphabet_b2);
intent.putExtra("alphabet_word3",R.drawable.alphabet_b3);
activity.startActivity(intent);
activity.overridePendingTransition(R.anim.activity_slide_in_left,R.anim.activity_slide_out_right);
Runtime.getRuntime().gc();
activity.onBackPressed();
}
});
setAnimation(holder.itemView,position);
SharedPreferences prefs1 = context.getSharedPreferences("prefs1", MODE_PRIVATE);
SharedPreferences prefs2 = context.getSharedPreferences("prefs2", MODE_PRIVATE);
boolean letter1 = prefs1.getBoolean("letter1", false);
boolean letter2 = prefs2.getBoolean("letter2", false);
if (letter1){
if (position==0){
holder.itemTrophy.setVisibility(View.VISIBLE);
}
}
if (letter2){
if (position==1){
holder.itemTrophy.setVisibility(View.VISIBLE);
}
}
}
如您所見,它應該可以在位置 1 或 2 中看到圖像,但在其他位置(並非 all_ 也是如此)可見。
看看這個片段,添加到onBindViewHolder
的底部
boolean letter1 = prefs1.getBoolean("letter1", false);
boolean letter2 = prefs2.getBoolean("letter2", false);
if ((letter1 || letter2) && position==1) {
holder.itemTrophy.setVisibility(View.VISIBLE);
}
else holder.itemTrophy.setVisibility(View.GONE);
查看RecyclerView
是如何工作的,以及在 Android 方面什么是回收。 簡而言之:當某些項目滾動到屏幕外時, View
/列表項目被重用,然后相同的View
從另一側進入(滾動到頂部將作為下一個列表項目顯示在底部)。 你沒有設置View.GONE
所以回收的列表項仍然有View.VISIBLE
作為你的獎杯圖標
編輯:
簡單的方法
if (position==1){
holder.itemTrophy.setVisibility(letter1 ? View.VISIBLE : View.GONE);
}
更精致
SharedPreferences prefs = context.getSharedPreferences("prefs" + (position+1), MODE_PRIVATE);
boolean letterSet = prefs.getBoolean("letter" + (position+1), false);
holder.itemTrophy.setVisibility(letterSet ? View.VISIBLE : View.GONE);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.