我有两个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);
}
}
我的问题:
当我运行应用程序时,我看到RecyclerView
1和RecyclerView
2都是随机的(顺序相同(。
我想要什么:
我想看看随机项目在RecyclerView
1和正常顺序RecyclerView
2 中的顺序
首先将列表对象传递给setAdapterForRecyclerView(collections);
之后,您将相同的列表对象传递给setAdapterForRecyclerViewBestCollections(shuffleCollection(collections));
然后对对象进行混洗(在使用相同对象的两种方法中,混洗将反映到RecyclerView1
和RecyclerView2
创建新的List
对象并在搅乱后返回,这样您将在RecyclerView1
和RecyclerView2
中看到两个不同的顺序
public List<CollectionsModel> shuffleCollection(List<CollectionsModel> collectionsModelList) {
List<CollectionsModel> shuff = new ArrayList<>(collectionsModelList);
java.util.Collections.shuffle(shuff);
return shuff;
}