setVisibility 在 RecyclerView 中未能对所有项目生效

huangapple go评论74阅读模式
英文:

setVisibility not working on all items in recyclerview

问题

以下是您提供的代码部分的翻译:

如果 (datassss[position] == "初学者") {
    holder.hpim1.setVisibility(View.VISIBLE);
    如果 (learnprogresdatacat1[position] == biglimits[cat]) {
        holder.hpim1.setImageDrawable(getResources().getDrawable(R.drawable.ic_win5));
    }
}
如果 (datassss[position] == "初级") {
    如果 (learnprogresdatacat1[getexacposition] != biglimits[cat]) {
        holder.miimlock.setVisibility(View.VISIBLE);
    }
    如果 (learnprogresdatacat1[getexacposition] == biglimits[cat]) {
        holder.miimlock.setVisibility(View.GONE);
    }
    holder.hpim1.setVisibility(View.VISIBLE);
    holder.hpim2.setVisibility(View.VISIBLE);
    holder.Main_lessons_num_text.setText("4课程");
    holder.main_big_imagex.setImageDrawable(getResources().getDrawable(R.drawable.h_sb_ele_tree));
    如果 (learnprogresdatacat1[position] == elelimits[cat]) {
        holder.hpim1.setImageDrawable(getResources().getDrawable(R.drawable.ic_win5));
        holder.hpim2.setImageDrawable(getResources().getDrawable(R.drawable.ic_win2));
    }
}
如果 (datassss[position] == "中级") {
    Log.e("找到限制", learnprogresdatacat1[getexacposition] + " fl");
    Log.e("限制", elelimits[cat] + " l");
    如果 (learnprogresdatacat1[getexacposition] != elelimits[cat]) {
        Log.e("限制", "解锁");
        holder.miimlock.setVisibility(View.VISIBLE);
    } else 如果 (learnprogresdatacat1[getexacposition] == elelimits[cat]) {
        holder.miimlock.setVisibility(View.GONE);
    }
    holder.hpim1.setVisibility(View.VISIBLE);
    holder.hpim2.setVisibility(View.VISIBLE);
    holder.hpim3.setVisibility(View.VISIBLE);
    holder.Main_lessons_num_text.setText("5课程");
    holder.main_big_imagex.setImageDrawable(getResources().getDrawable(R.drawable.tree2));
    如果 (learnprogresdatacat1[position] == interlimits[cat]) {
        holder.hpim1.setImageDrawable(getResources().getDrawable(R.drawable.ic_win5));
        holder.hpim2.setImageDrawable(getResources().getDrawable(R.drawable.ic_win2));
        holder.hpim3.setImageDrawable(getResources().getDrawable(R.drawable.ic_win6));
    }
}

请注意,由于文本中包含HTML转义字符(如" ),我已经将它们翻译为对应的双引号和空格。另外,图片的描述以及链接没有被翻译,您可以在原文中查看它们。

英文:

so I have recycler view show 4 items, I have code that unlocks the card and remove lock view so the problem is code work in the first 2 visible items but when I scroll to item 3 it has same code in it but set visibility GONE don't work even when I see it call-in log

here is code java:

  if (datassss[position] == "Beginner") {
holder.hpim1.setVisibility(View.VISIBLE);
if (learnprogresdatacat1[position] == biglimits[cat]) {
holder.hpim1.setImageDrawable(getResources().getDrawable(R.drawable.ic_win5));
}
}
if (datassss[position] == "Elementary") {
if (learnprogresdatacat1[getexacposition] != biglimits[cat]) {
holder.miimlock.setVisibility(View.VISIBLE);
}
if (learnprogresdatacat1[getexacposition] == biglimits[cat]) {
holder.miimlock.setVisibility(View.GONE);
}
holder.hpim1.setVisibility(View.VISIBLE);
holder.hpim2.setVisibility(View.VISIBLE);
holder.Main_lessons_num_text.setText("4lessons");
holder.main_big_imagex.setImageDrawable(getResources().getDrawable(R.drawable.h_sb_ele_tree));
if (learnprogresdatacat1[position] == elelimits[cat]) {
holder.hpim1.setImageDrawable(getResources().getDrawable(R.drawable.ic_win5));
holder.hpim2.setImageDrawable(getResources().getDrawable(R.drawable.ic_win2));
}
}
if (datassss[position] == "Intermediate") {
Log.e("found limit", learnprogresdatacat1[getexacposition] + " fl");
Log.e("limit", elelimits[cat] + " l");
if (learnprogresdatacat1[getexacposition] != elelimits[cat]) {
Log.e("limit",  "unlock");
holder.miimlock.setVisibility(View.VISIBLE);
} else if (learnprogresdatacat1[getexacposition] == elelimits[cat]) {
holder.miimlock.setVisibility(View.GONE);
}
holder.hpim1.setVisibility(View.VISIBLE);
holder.hpim2.setVisibility(View.VISIBLE);
holder.hpim3.setVisibility(View.VISIBLE);
holder.Main_lessons_num_text.setText("5lessons");
holder.main_big_imagex.setImageDrawable(getResources().getDrawable(R.drawable.tree2));
if (learnprogresdatacat1[position] == interlimits[cat]) {
holder.hpim1.setImageDrawable(getResources().getDrawable(R.drawable.ic_win5));
holder.hpim2.setImageDrawable(getResources().getDrawable(R.drawable.ic_win2));
holder.hpim3.setImageDrawable(getResources().getDrawable(R.drawable.ic_win6));
}
}

here is my log i debug witch show say to visibility call :

> 2020-10-16 09:28:04.441 21853-21853/ E/limit: unlock
2020-10-16 09:28:04.441 21853-21853/ E/found limit: 4 fl
2020-10-16 09:28:04.441 21853-21853/ E/limit: 4 l

but as you can you in the image of the emulator view is not gonesetVisibility 在 RecyclerView 中未能对所有项目生效

setVisibility 在 RecyclerView 中未能对所有项目生效

all have the same code but it only works with visible items at first
plz help me what's wrong
thanks

答案1

得分: 1

在所有if语句之前和开头添加以下内容:

holder.hpim1.setVisibility(View.GONE);
holder.hpim2.setVisibility(View.GONE);
holder.miimlock.setVisibility(View.GONE);
holder.hpim3.setVisibility(View.GONE);

更新

尝试在"Intermediate"的if语句中将elelimits替换为biglimits,如下所示:

if (datassss[position] == "Intermediate") {
    Log.e("found limit", learnprogresdatacat1[getexacposition] + " fl");
    Log.e("limit", biglimits[cat] + " l");

    if (learnprogresdatacat1[getexacposition] != biglimits[cat]) {
        Log.e("limit", "unlock");
        holder.miimlock.setVisibility(View.VISIBLE);
    } else if (learnprogresdatacat1[getexacposition] == biglimits[cat]) {
        holder.miimlock.setVisibility(View.GONE);
    }
    holder.hpim1.setVisibility(View.VISIBLE);
    holder.hpim2.setVisibility(View.VISIBLE);
    holder.hpim3.setVisibility(View.VISIBLE);
    holder.Main_lessons_num_text.setText("5lessons");
    holder.main_big_imagex.setImageDrawable(getResources().getDrawable(R.drawable.tree2));
    if (learnprogresdatacat1[position] == interlimits[cat]) {
        holder.hpim1.setImageDrawable(getResources().getDrawable(R.drawable.ic_win5));
        holder.hpim2.setImageDrawable(getResources().getDrawable(R.drawable.ic_win2));
        holder.hpim3.setImageDrawable(getResources().getDrawable(R.drawable.ic_win6));
    }
}
英文:

Add this at the beginning and before all if statements

holder.hpim1.setVisibility(View.GONE);
holder.hpim2.setVisibility(View.GONE);
holder.miimlock.setVisibility(View.GONE);
holder.hpim3.setVisibility(View.GONE);

Update

Try to replace elelimits with biglimits in "Intermediate" if statement. Like that:

            if (datassss[position] == "Intermediate") {
Log.e("found limit", learnprogresdatacat1[getexacposition] + " fl");
Log.e("limit", elelimits[cat] + " l");
if (learnprogresdatacat1[getexacposition] != biglimits[cat]) {
Log.e("limit",  "unlock");
holder.miimlock.setVisibility(View.VISIBLE);
} else if (learnprogresdatacat1[getexacposition] == biglimits[cat]) {
holder.miimlock.setVisibility(View.GONE);
}
holder.hpim1.setVisibility(View.VISIBLE);
holder.hpim2.setVisibility(View.VISIBLE);
holder.hpim3.setVisibility(View.VISIBLE);
holder.Main_lessons_num_text.setText("5lessons");
holder.main_big_imagex.setImageDrawable(getResources().getDrawable(R.drawable.tree2));
if (learnprogresdatacat1[position] == interlimits[cat]) {
holder.hpim1.setImageDrawable(getResources().getDrawable(R.drawable.ic_win5));
holder.hpim2.setImageDrawable(getResources().getDrawable(R.drawable.ic_win2));
holder.hpim3.setImageDrawable(getResources().getDrawable(R.drawable.ic_win6));
}
}

答案2

得分: 0

问题在于你没有在所有的if语句的else部分处理可见性。如果你在if语句中设置一个组件为可见,请确保在else语句中将其设置为不可见。否则,这个问题将持续存在。
这个问题的根本原因是视图被回收,如果你没有正确处理可见性,旧的可见性状态将被保留。

英文:

The problem is you are not handling the visibility in all else part of your if's. You you are setting a component to be visible in a if statement, make sure to make it invisible in the else statement.Otherwise this issue will persist.
The root cause of this issue is the view gets recycled and the old visibility is retained if you are not handling the visibility correctly.

huangapple
  • 本文由 发表于 2020年10月16日 14:22:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/64383907.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定