无序排列列表



我有两个RecyclerView和一个ArrayList,称为集合,我正在尝试对这个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);
}
}

我的问题:

当我运行应用程序时,我看到RecyclerView1和RecyclerView2都是随机的(顺序相同(。

我想要什么:

我想看看随机项目在RecyclerView1和正常顺序RecyclerView2 中的顺序

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

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

然后对对象进行混洗(在使用相同对象的两种方法中,混洗将反映到RecyclerView1RecyclerView2

创建新的List对象并在搅乱后返回,这样您将在RecyclerView1RecyclerView2中看到两个不同的顺序

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

相关内容

  • 没有找到相关文章

最新更新