如何随机int []图像而不在bindviewholder中重复

时间:2019-07-04 20:15:05

标签: android android-recyclerview

我遇到一个问题,我想在Recyclerview中的onBindViewHolder上随机分配一个图像数组,一切工作正常,但是它正在重复该数组中的随机图像,例如2张图像重复2次。我在垂直recyclerview中使用它来将文本和图像显示到它们的确切位置,例如,如果我有2个数组,其中一个是字符串,另一个是int用于图像,那么我希望两个数组的位置将同时在同一位置随机排列,但不能重复代码可以完美地完成所有工作,但可以重复图像。

这是我的完整代码

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewwHolder>  {
     private int [] arraylist;
    private Context ctx;
   private ImageView name;
    private TextView textView;
   private   String [] text ;



    public MyAdapter(int[] arraylist, String []text,Context ctx) {
        this.arraylist = arraylist;
        this.text = text;
        this.ctx = ctx;
    }
    @NonNull
    @Override
    public ViewwHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view= LayoutInflater.from(ctx.getApplicationContext()).inflate(R.layout.row,  viewGroup, false) ;
        return new ViewwHolder(view);    }

    @Override
    public void onBindViewHolder(@NonNull ViewwHolder viewwHolder, final int i) {



        Random random=new Random();
      final int po=random.nextInt(arraylist.length);
        name.setImageResource(arraylist[po]);
        textView.setText(text[po]);

        name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                    Intent intent= new Intent(ctx, second.class);
                           intent.putExtra("imageposition", arraylist[po]);
                           intent.putExtra("textposition",text[po]);
                           ctx.startActivity(intent);


            }
        });

    }

    @Override
    public int getItemCount() {
        return arraylist.length;
    }


    public class ViewwHolder extends RecyclerView.ViewHolder {
        public ViewwHolder(@NonNull View itemView) {
            super(itemView);
            name=itemView.findViewById(R.id.itemoneimageone);
            textView=itemView.findViewById(R.id.text);
        }
    }



}

2 个答案:

答案 0 :(得分:0)

使用完图像后,您可以将其从列表中删除。

private ArrayList<Integer> arraylist; // use a dynamic array.


final int po = random.nextInt(arraylist.size());
int imageResource = arraylist.get(po);
arraylist.remove(po); // if you don't want to lose it, you can store it in another ArrayList before removal
name.setImageResource(imageResource);

答案 1 :(得分:0)

如果您不想重复这些图像,则不想在每次调用onBindViewHolder时随机选择一个图像。取而代之的是,您只希望将图像(和相关文本)随机播放一次,并按顺序遍历随机播放的数组。由于您同时具有图像数组和文本数组(可能需要使其保持同步),因此我建议创建一个从0到arraylist.length-1的整数数组,然后将其改组。 (您可以在this question的答案中找到用于改编int[]的代码。)您可以在onBindViewHolder之外执行此操作,并将改组后的索引存储在成员字段中(我们将其称为{{ 1}})。然后代替

shuffledArray

使用

final int po=random.nextInt(arraylist.length);

一种替代方法(在我看来,更好)是创建图像/文本对的数组,并将其存储在适当类型的final int po = shuffledArray[i]; 中。然后,您可以使用ArrayList随机排列列表。再一次,将在Collections.shuffle之外执行一次。

由于某种原因,Java有一个onBindViewHolder方法,但没有一个Collections.shuffle方法。

编辑:现在,我看到了您的整个课堂,这就是我将其更改的方式:

Arrays.shuffle

然后,像以前一样,在public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewwHolder> { ... private int[] shuffled; public MyAdapter(int[] arraylist, String []text,Context ctx) { this.arraylist = arraylist; this.text = text; this.ctx = ctx; shuffled = new int[arraylist.length]; for (int i = 0; i < arraylist.length; i++) shuffled[i] = i; shuffle(shuffled); } private static void shuffle(int[] array) { Random rnd = ThreadLocalRandom.current(); for (int i = array.length - 1; i > 0; i--) { int index = rnd.nextInt(i + 1); int tmp = array[index]; array[index] = array[i]; array[i] = tmp; } } } 内,将onBindViewHolder的设置方式更改为:

po

其他所有内容都可以保持不变。