Java setVisibility无法处理recyclerview中的所有项目

Java setVisibility无法处理recyclerview中的所有项目,java,android,android-recyclerview,Java,Android,Android Recyclerview,因此,我有回收器视图显示4个项目,我有代码解锁卡并移除锁定视图,所以问题是代码在前2个可见项目中工作,但当我滚动到项目3时,它在其中有相同的代码,但设置可见性消失了,即使我在调用日志中看到它也不工作 下面是java代码: if (datassss[position] == "Beginner") { holder.hpim1.setVisibility(View.VISIBLE); if (learnprog

因此,我有回收器视图显示4个项目,我有代码解锁卡并移除锁定视图,所以问题是代码在前2个可见项目中工作,但当我滚动到项目3时,它在其中有相同的代码,但设置可见性消失了,即使我在调用日志中看到它也不工作

下面是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));
                }
            }
这是我的日志,我对可视性呼叫说:

2020-10-16 09:28:04.441 21853-21853/E/限制:解锁 2020-10-16 09:28:04.441 21853-21853/E/发现限值:4层 2020-10-16 09:28:04.441 21853-21853/E/极限:4升

但正如您所能看到的那样,emulator视图的图像并没有消失

它们都有相同的代码,但一开始只适用于可见项 请帮帮我怎么了
谢谢

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

将此添加到所有if语句的开头和前面

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

更新

尝试将“中间”if语句中的
elelimits
替换为
biglimites
。就像这样:

            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));
                }
            }

您必须处理这两种情况if和else部分,因为
Viewholder
正在被重用。@ADM它们都在同一时间调用,它在前2项中起作用,但当我滚动时,相同的代码在列表的3或4项上不起作用,我尝试使用else con它相同的仍然锁定视图显示,我的意思是如果{}else{}他们注意到锁的问题,他们工作正常我的问题与持有人。miimlock开关是锁的黑色视图,如您在xml视图中的图像所示,默认情况下,在“中间”中将
elelimits
替换为
bigflimits
如果statementelelimits是int数组,显示每个类别中有多少课程,所以我无法设置biggner,但我也尝试了如果你看到我登录qus它不起作用,说如果con调用但设置Visiable不起作用logcal显示的
log.e(“找到的限制”,learnprogresdatacat1[getexacposition]+“fl”)限制:解锁2020-10-16 09:28:04.441 21853-21853/E/找到的限制:4 fl 2020-10-16 09:28:04.441 21853-21853/E/limit:4 l这是我的日志,两个数字都是4,所以它是正确的,它在日志中说它是解锁的,但在模拟器中可以看到它仍然是锁定的