洗牌数组列表

Shuffle ArrayList

我有两个 RecyclerView 和一个 ArrayList 叫做 collections,我正在尝试洗牌这个 ArrayList 并得到 12 个项目它。

@Override
protected void onPostExecute(List<CollectionsModel> collections) {
    super.onPostExecute(collections);
    if (isAdded() && getActivity() != null) {
        setAdapterForRecyclerView(collections);
         setAdapterForRecyclerViewBestCollections(shuffleCollection(collections));
    }
}

随机播放方法:

public List<CollectionsModel> shuffleCollection(List<CollectionsModel> collectionsModelList) {
    java.util.Collections.shuffle(collectionsModelList);
    return collectionsModelList;
}

RecyclerView 1 的适配器方法:

private void setAdapterForRecyclerViewBestCollections(List<CollectionsModel> collectionHelper) {
    for (int i = 0; i < 12; i++) {
        arrayListCollections.add(collectionHelper.get(i));
    }
    /*rest of code*/
}

RecyclerView 2 的适配器方法:

private void setAdapterForRecyclerView(final List<CollectionsModel> wlls) {
    if (myAdapter == null) {
        myAdapter = new MyAdapterCollection(wlls, getActivity(), new RecyclerViewClickListener() {
            @Override
            public void onClick(View view, Wallpaper wallpaper) {

            }

            @Override
            public void onClick(View view, CollectionsModel collectionsModel) {

            }
        }, R.layout.collection_item);
        recyclerView.setAdapter(myAdapter);
    } else {
        int position = myAdapter.getItemCount();
        myAdapter.getItems().addAll(wlls);
        myAdapter.notifyItemRangeInserted(position, position);
    }
}

我的问题:

当我 运行 应用程序时,我看到 RecyclerView 1 和 RecyclerView 2 它们都是随机的(顺序相同)。

我想要的:

我想查看 RecyclerView 1 中的随机物品顺序和正常顺序 RecyclerView 2

首先,您将列表对象传递给 setAdapterForRecyclerView(collections);

之后您将相同的列表对象传递给 setAdapterForRecyclerViewBestCollections(shuffleCollection(collections));

然后洗牌对象(在这两种方法中,您使用相同的对象和洗牌将反映到 RecyclerView1RecyclerView2

创建新的 List 对象和 return 洗牌后,这样你会在 RecyclerView1RecyclerView2

中看到两个不同的顺序
public List<CollectionsModel> shuffleCollection(List<CollectionsModel> collectionsModelList) {
    List<CollectionsModel> shuff = new ArrayList<>(collectionsModelList);
    java.util.Collections.shuffle(shuff);
    return shuff;
}