gpt4 book ai didi

java - 网络分页与 arch 库中的 pagedList

转载 作者:太空宇宙 更新时间:2023-11-04 10:21:17 24 4
gpt4 key购买 nike

Android 添加分页库作为架构组件的一部分,以使用资源的分页。我用this做这件事的例子。我编写了这些代码,但在我的 DataSource.Factory 中,我无法从网络返回收到的 DataSource 。让我们看看我的代码:

PostDataSource:

public class PostDataSource extends PageKeyedDataSource {

private final APIUtils api = RetrofitHelper.getInstance().create(APIUtils.class);
private final String TOKEN = TokenUtil.getCachedAuthToken();
private int page = 1;
private GeneralWebResult<ArrayList<Post>> postList;

@Override
public void loadInitial(@NonNull LoadInitialParams params, @NonNull LoadInitialCallback callback) {

api.getMainPosts("all",10,"new",page,TOKEN).enqueue(new Callback<GeneralWebResult<ArrayList<Post>>>() {
@Override
public void onResponse(Call<GeneralWebResult<ArrayList<Post>>> call, Response<GeneralWebResult<ArrayList<Post>>> response) {
postList = response.body();
callback.onResult(/*return ArrayList<Post> */postList.getResult(),null, ++ page);
Log.e("RESULT",response.code()+" ");
}

@Override
public void onFailure(Call<GeneralWebResult<ArrayList<Post>>> call, Throwable t) {
Log.e("ERROR",t.getLocalizedMessage());
}
});
}

@Override
public void loadBefore(@NonNull LoadParams params, @NonNull LoadCallback callback) {

}

@Override
public void loadAfter(@NonNull LoadParams params, @NonNull LoadCallback callback) {
ArrayList<Post> postList = new ArrayList<>();
api.getMainPosts("all",10,"new",page,TOKEN).enqueue(new Callback<GeneralWebResult<ArrayList<Post>>>() {
@Override
public void onResponse(Call<GeneralWebResult<ArrayList<Post>>> call, Response<GeneralWebResult<ArrayList<Post>>> response) {
postList.addAll(response.body().getResult());
callback.onResult(postList,null);
Log.e("RESULT",response.code()+" ");
}

@Override
public void onFailure(Call<GeneralWebResult<ArrayList<Post>>> call, Throwable t) {
Log.e("ERROR",t.getLocalizedMessage());
}
});
}
}

PostDataSourceFactory:

public class PostDataSourceFactory extends DataSource.Factory {

private PostDataSource dataSource;

@Override
public DataSource create() {

return new PostDataSource();
}
}

PostViewModel:

public class PostViewModel extends ViewModel {
public LiveData<PagedList<Post>> liveDataSource;


public PostViewModel() {
Executor executor = Executors.newFixedThreadPool(5);
PostDataSourceFactory factory = new PostDataSourceFactory(executor);


PagedList.Config config = new PagedList.Config.Builder().setEnablePlaceholders(false)
.setInitialLoadSizeHint(10).setPageSize(10).setPrefetchDistance(4).build();

liveDataSource = (new LivePagedListBuilder(factory,config)).setFetchExecutor(executor).build();

//Log.e("LIVE DATA SOURCE", liveDataSource.getValue().get(0).getContent());


}
}

PostAdapter:

public class PostAdapter extends PagedListAdapter<Post, PostAdapter.Holder> {

private PagedList<Post> items;

public void setItems(PagedList<Post> items) {
this.items = items;
}

public class Holder extends RecyclerView.ViewHolder{

TextView content;
Holder(View itemView) {
super(itemView);

content = itemView.findViewById(R.id.item_post_test_content);
}

void bindTo(Post post){
content.setText(post.getContent());
}
}
PostAdapter() {
super(Post.DiffCAllback);
}

@NonNull
@Override
public PostAdapter.Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new Holder(LayoutInflater.from(parent.getContext()).inflate(R.layout.post_test_item,parent,false));
}

@Override
public void onBindViewHolder(@NonNull PostAdapter.Holder holder, int position) {
holder.bindTo(getItem(position));

}
}

主要 Activity :

list = findViewById(R.id.home_list);
list.setLayoutManager(new LinearLayoutManager(this));
PostViewModel viewModel = ViewModelProviders.of(this).get(PostViewModel.class);


PostAdapter adapter = new PostAdapter();
viewModel.liveDataSource.observe(this,items ->{
Log.e("ITEMS SIZE",items.size()+" ");
adapter.setItems(items);
} );

list.setAdapter(adapter);

我的错误在哪里?为什么 PostDataSourceFactorycreate() 上返回 null?

最佳答案

使用 MutableLiveData 对象来包装数据源

MutableLiveData 用于在观察任何数据时通知您的 UI

使用 LivePagedListBuilder 时,建议使用 MutableLiveData 作为数据源的包装器

参见PagingWithNetworkSample

您还需要更新您的数据源 PagedKeyDataSource

 public class PostDataSourceFactory extends DataSource.Factory<Integer, Post> {
public MutableLiveData<PostDataSource> datasourceLiveData = new MutableLiveData<>();


public PostDataSourceFactory(
}

@Override
public DataSource<Integer, Post> create() {
PostDataSource dataSource = new PostDataSource();
datasourceLiveData.postValue(dataSource);
return dataSource;
}
}

关于java - 网络分页与 arch 库中的 pagedList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51136848/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com