我正在使用android jetpack的分页库。我可以获取到第2页的数据,但是即使我从Datasource中的loadAfter()方法在api调用中传递params.key,也无法获取其他页面的数据。我很简单,无法理解问题出在哪里。
这里是我的数据源:
public class ApprovedBillsDataSource extends PageKeyedDataSource<Integer, BillsModel.Bills> {
static int FIRST_PAGE = 1;
String sortOrder;
String searchParam;
EmptyHandler emptyHandler;
ApprovedBillsDataSource(String sortOrder, String searchParam, EmptyHandler emptyHandler) {
this.sortOrder = sortOrder;
this.searchParam = searchParam;
this.emptyHandler = emptyHandler;
}
@Override
public void loadInitial(@NonNull LoadInitialParams<Integer> params, @NonNull final LoadInitialCallback<Integer, BillsModel.Bills> callback) {
RestClient.webServices().getCompletePartnerBills(FIRST_PAGE, 1, sortOrder, searchParam).enqueue(new Callback<ApiResponse<BillsModel>>() {
@Override
public void onResponse(Call<ApiResponse<BillsModel>> call, Response<ApiResponse<BillsModel>> response) {
if (response.code() == 200) {
callback.onResult(response.body().getData().getBills(), null, FIRST_PAGE + 1);
if (response.body().getData().getBills() == null || response.body().getData().getBills().size() == 0) {
emptyHandler.onListEmpty();
}
}
}
@Override
public void onFailure(Call<ApiResponse<BillsModel>> call, Throwable t) {
}
});
}
@Override
public void loadBefore(@NonNull LoadParams<Integer> params, @NonNull LoadCallback<Integer, BillsModel.Bills> callback) {
}
@Override
public void loadAfter(@NonNull LoadParams<Integer> params, @NonNull final LoadCallback<Integer, BillsModel.Bills> callback) {
RestClient.webServices().getCompletePartnerBills(params.key, 1, sortOrder, searchParam).enqueue(new Callback<ApiResponse<BillsModel>>() {
@Override
public void onResponse(Call<ApiResponse<BillsModel>> call, Response<ApiResponse<BillsModel>> response) {
if (response.code() == 200) {
callback.onResult(response.body().getData().getBills(), null);
}
}
@Override
public void onFailure(Call<ApiResponse<BillsModel>> call, Throwable t) {
}
});
}
这是我的适配器:
public class ApprovedBillsAdapter extends PagedListAdapter<BillsModel.Bills, ApprovedBillsAdapter.ItemViewHolder> {
Context context;
ApprovedBillsAdapter(Context context) {
super(DIFF_CALLBACK);
this.context = context;
}
@NonNull
@Override
public ApprovedBillsAdapter.ItemViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
return new ApprovedBillsAdapter.ItemViewHolder(LayoutInflater.from(context).inflate(R.layout.row_approved_bills, viewGroup, false));
}
@Override
public void onBindViewHolder(@NonNull ApprovedBillsAdapter.ItemViewHolder holder, int i) {
try {
final BillsModel.Bills bill = getItem(i);
holder.tv_bill_id.setText(bill.getBillId());
Picasso.with(context).load(bill.getCustomer_image()).placeholder(R.drawable.ic_avatar).into(holder.ivUserImage);
holder.tvCustomerName.setText(bill.getCustomer_name());
holder.tvAmount.setText("Rs." + bill.getBillAmount());
holder.tvMobileNumber.setText(bill.getCustomer_mobile());
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(context, BillDetailsActivity.class);
intent.putExtra("id", bill.getId());
context.startActivity(intent);
}
});
holder.tvPendingSinceValue.setText(bill.getUpdated_at());
} catch (Exception e) {
}
}
private static DiffUtil.ItemCallback<BillsModel.Bills> DIFF_CALLBACK =
new DiffUtil.ItemCallback<BillsModel.Bills>() {
@Override
public boolean areItemsTheSame(BillsModel.Bills oldItem, BillsModel.Bills newItem) {
return oldItem.getBillId().equals(newItem.getBillId());
}
@Override
public boolean areContentsTheSame(BillsModel.Bills oldItem, BillsModel.Bills newItem) {
return oldItem.equals(newItem);
}
};
class ItemViewHolder extends RecyclerView.ViewHolder {
TextView tv_bill_id;
@BindView(R.id.iv_user_image)
ImageView ivUserImage;
@BindView(R.id.tv_customer_name)
TextView tvCustomerName;
@BindView(R.id.tv_mobile_number)
TextView tvMobileNumber;
@BindView(R.id.tv_bill_id)
TextView tvBillId;
@BindView(R.id.tv_amount)
TextView tvAmount;
@BindView(R.id.tv_pending_since_value)
TextView tvPendingSinceValue;
public ItemViewHolder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
tv_bill_id = itemView.findViewById(R.id.tv_bill_id);
}
}
}
答案 0 :(得分:0)
解决了,我需要添加
Integer key = params.key + 1;
callback.onResult(response.body().getData().getOffers(), key);
到我的代码。在每个loadAfter调用上递增params.key是关键。然后将其作为相邻键传递给onResult函数。