安卓:使用PagedList时如何添加项目和刷新列表?



我已经使用PagedList加载项目页面并在RecyclerView中显示它们,并且它成功地显示了数字列表(0,1,2,3....,99(,列表始终可以上下滚动而没有任何问题。 当我尝试单击按钮来调用 appendItems(( 来附加新数据时,它仍然很好。但是,当我滚动列表时,该应用程序崩溃了。

java.util.ConcurrentModificationException
at java.util.AbstractList$SubAbstractList.size(AbstractList.java:360)
at androidx.paging.PagedStorage.get(PagedStorage.java:153)
at androidx.paging.PagedList.get(PagedList.java:384)
at androidx.paging.AsyncPagedListDiffer.getItem(AsyncPagedListDiffer.java:206)
at androidx.paging.PagedListAdapter.getItem(PagedListAdapter.java:156)
at com.mysample.pagedlist.MyPagedListAdapter.onBindViewHolder(MyPagedListAdapter.java:38)
at com.mysample.pagedlist.MyPagedListAdapter.onBindViewHolder(MyPagedListAdapter.java:19)

似乎我不知道附加更多数据和刷新显示的正确方法。谁能告诉我如何解决?

Api 作为数据库和 aao

public class Api {
private static List<String> list = null;
private static void initListIfNeeded() {
if (list == null) {
list = new ArrayList<>();
for (int i = 0; i < 100; i++) {
list.add("" + i);
}
}
}
public static List<String> pageItems(int page, int pageSize) {
initListIfNeeded();
int start = page * pageSize;
int end = (page + 1) * pageSize;
if (end > list.size()) {
end = list.size();
}
if (page < 0 || start >= list.size()) {
return new ArrayList<>();
} else {
return list.subList(start, end);
}
}
public static void appendItems() {
initListIfNeeded();
int size = list.size();
for (int i = 0; i < 10; i++) {
list.add("" + (size + i));
}
}
}

用于处理数据分页的数据源

public class MyDataSource extends PageKeyedDataSource<Integer, String> {
public MyDataSource() {
}
@Override
public void loadInitial(@NonNull LoadInitialParams<Integer> params, @NonNull LoadInitialCallback<Integer, String> callback) {
callback.onResult(Api.pageItems(0, params.requestedLoadSize), -1, 1);
}
@Override
public void loadBefore(@NonNull LoadParams<Integer> params, @NonNull LoadCallback<Integer, String> callback) {
callback.onResult(Api.pageItems(params.key, params.requestedLoadSize), params.key - 1);
}
@Override
public void loadAfter(@NonNull LoadParams<Integer> params, @NonNull LoadCallback<Integer, String> callback) {
callback.onResult(Api.pageItems(params.key, params.requestedLoadSize), params.key + 1);
}
}

相应的视图模型类

public class MyViewModel extends ViewModel {
public LiveData<PagedList<String>> list;
public MyViewModel() {
list = new LivePagedListBuilder<>(new DataSource.Factory<Integer, String>() {
@Override
public DataSource<Integer, String> create() {
return new MyDataSource();
}
},  new PagedList.Config.Builder().setPageSize(20).setInitialLoadSizeHint(20).build()).build();
}
public void appendItems() {
Api.appendItems();
}
}

活动

public class PagedListActivity extends AppCompatActivity {
private MyPagedListAdapter adapter;
private MyViewModel viewModel;
private RecyclerView recyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_paged_list);
adapter = new MyPagedListAdapter(LayoutInflater.from(this));
recyclerView = findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);
viewModel = ViewModelProviders.of(this).get(MyViewModel.class);
viewModel.list.observe(this, new Observer<PagedList<String>>() {
@DebugLog
@Override
public void onChanged(PagedList<String> strings) {
adapter.submitList(strings);
}
});
}
@DebugLog
public void onClick(View view) {
viewModel.appendItems();
}
}

适配器

public class MyPagedListAdapter extends PagedListAdapter<String, MyPagedListAdapter.VH> {
private LayoutInflater inflater;
protected MyPagedListAdapter(LayoutInflater inflater) {
super(DIFF_CALLBACK);
this.inflater = inflater;
}
@NonNull
@Override
public VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.item_text, parent, false);
return new VH(view);
}
@Override
public void onBindViewHolder(@NonNull VH holder, int position) {
holder.setData(getItem(position));
}
public class VH extends RecyclerView.ViewHolder {
private final TextView txt;
public VH(@NonNull View itemView) {
super(itemView);
txt = itemView.findViewById(R.id.txt);
}
public void setData(String text) {
txt.setText(text);
}
}
private static DiffUtil.ItemCallback<String> DIFF_CALLBACK = new DiffUtil.ItemCallback<String>() {
@Override
public boolean areItemsTheSame(@NonNull String oldItem, @NonNull String newItem) {
return oldItem.equalsIgnoreCase(newItem);
}
@Override
public boolean areContentsTheSame(@NonNull String oldItem, @NonNull String newItem) {
return oldItem.equalsIgnoreCase(newItem);
}
};
}

从这个堆栈跟踪:

java.util.ConcurrentModificationException
at java.util.AbstractList$SubAbstractList.size(AbstractList.java:360)

我们可以说

a( 当有人尝试修改列表时应用程序崩溃,而迭代列表正在进行中
b( 这发生在 SubAbstractList 实例(在您调用时创建(list.subList(start, end);

应用崩溃,因为在原始列表修改之前创建的子列表在您修改(将项目追加到(原始列表后不再有效

与其从Api#pageItems返回list.subList(start, end);,不如尝试
返回new ArrayList<>(list.subList(start, end));

要使回收器视图显示更改,应调用Adapter#notifyItemRangeInserted或 RW 适配器

相关内容

  • 没有找到相关文章

最新更新