jai*_*ait 2 android realm android-recyclerview
我有一个RealmResults对象,它保存.where.findAll()查询的输出.我在RecyclerView中显示此对象的内容,但由于RealmResults对象的内容是静态的,因此它会显示相同的记录集,并且打开RecyclerView.我想要随机化(随机化)RealmResults的内容,以便我可以在RecyclerView中显示RealmResults中的不同值.请提出一些可行的方法,以便我可以执行相同的操作.
你不应该随机化列表本身,你应该随机化你的访问(索引).
构建包含索引的列表 [0, n-1]
List<Integer> indices = new ArrayList<>(realmResults.size());
for(int i = 0; i < realmResults.size(); i++) {
indices.add(i);
}
Run Code Online (Sandbox Code Playgroud)
洗牌吧
Collections.shuffle(indices);
Run Code Online (Sandbox Code Playgroud)
然后,当您选择视图持有者数据时,使用索引随机位置索引领域结果
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if(holder instanceof YourHolder) {
YourHolder yourHolder = (YourHolder) holder;
RealmData realmData = realmResults.get(indices.get(position));
//init data for your holder
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
798 次 |
| 最近记录: |