[英]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.