android下拉加载数据重复,显示不全
对于android下拉加载更多的功能实现
这也是我第一次使用下拉刷新功能,在使用的过程中也遇到了很多我意想不到的难题,不过最终还是将功能实现了,话不多说直接开始
列表框架的使用
首先,对于列表我是用的是RecycleView的BaseQuickAdapter,这个框架 地址是:link.,上面有关于框架的简单应用和详细的功能实现
遇到的难题:
其中链接中有关于上拉加载更多的简单使用,但是代码太少可能,按照上面的写我的列表显示的数据重复,还有一种情况就是数据不重复但是只能加载2次,以后一直转圈,不显示
效果:
话不多说,直接贴代码
1.对于数据重复的解决:
leder_recycler.setLayoutManager(new LinearLayoutManager(RepaireLedgerManager.this));//布局管理器
leder_recycler.setAdapter(adapter);//设置适配器
private void postRef(final String start) {
Retrofit retrofit = new Retrofit.Builder().baseUrl(address)
.addConverterFactory(GsonConverterFactory.create())//设置 Json 转换器
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())//RxJava 适配器
.build();
WorkPlaneService workPlaneService = retrofit.create(WorkPlaneService.class);
workPlaneService.getrepairedis(start)
.subscribeOn(Schedulers.io())//IO线程加载数据
.observeOn(AndroidSchedulers.mainThread())//主线程显示数据
.subscribe(new Subscriber<RepaireLedgerEntity>() {
@Override
public void onCompleted() {//数据加载完成
}
@Override
public void onError(Throwable e) {//数据加载失败
}
@Override
public void onNext(final RepaireLedgerEntity repaireLedgerEntity) {
if (repaireLedgerEntity.getState().equals("error")) {
Toast.makeText(RepaireLedgerManager.this, "网络错误,请重试", Toast.LENGTH_SHORT).show();
} else {
Log.i("zijia", "" + mCurrentCounter);
dateBeans = new ArrayList<>();
for (int i = 0; i < repaireLedgerEntity.getDate().size(); i++) {
dateBeans.add(new RepaireLedgerEntity.DateBean(repaireLedgerEntity.getDate().get(i).getId(), repaireLedgerEntity.getDate().get(i).getProduct(), repaireLedgerEntity.getDate().get(i).getBarcode(), repaireLedgerEntity.getDate().get(i).getCustomer(), repaireLedgerEntity.getDate().get(i).getPerson(), repaireLedgerEntity.getDate().get(i).getReceive_date()));
}
for (int i = 0; i < dateBeans.size(); i++) {
Log.i("qiqiqiq", dateBeans.get(i).getId());
}
adapter.addData(dateBeans);
adapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {//下拉加载功能实现
@Override
public void onLoadMoreRequested() {
leder_recycler.postDelayed(new Runnable() {
@Override
public void run() {
if (mCurrentCounter >= 1000) {
//数据全部加载完毕
adapter.loadMoreEnd();
} else {
boolean isErr = true;
if (isErr) {
//成功获取更多数据
Log.i("zahuishi","t"+mCurrentCounter);
postRef(String.valueOf(mCurrentCounter));
mCurrentCounter += 20;
adapter.loadMoreComplete();
} else {
//获取更多数据失败
isErr = true;
Toast.makeText(RepaireLedgerManager.this, "加载失败", Toast.LENGTH_LONG).show();
adapter.loadMoreFail();
}
}
}
}, 1000);
}
}, leder_recycler);
}
}
});
}
2.对于加载不全的解决方法:
Retrofit retrofit = new Retrofit.Builder().baseUrl(address)
.addConverterFactory(GsonConverterFactory.create())//设置 Json 转换器
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())//RxJava 适配器
.build();
WorkPlaneService workPlaneService = retrofit.create(WorkPlaneService.class);
workPlaneService.getrepairedis(start)
.subscribeOn(Schedulers.io())//IO线程加载数据
.observeOn(AndroidSchedulers.mainThread())//主线程显示数据
.subscribe(new Subscriber<RepaireLedgerEntity>() {
@Override
public void onCompleted() {
}
@Override
public void onError(Throwable e) {
}
@Override
public void onNext(final RepaireLedgerEntity repaireLedgerEntity) {
if (repaireLedgerEntity.getState().equals("error")) {
Toast.makeText(RepaireLedgerManager.this, "网络错误,请重试", Toast.LENGTH_SHORT).show();
} else {
Log.i("zijia", "" + mCurrentCounter);
dateBeans = new ArrayList<>();
for (int i = 0; i < repaireLedgerEntity.getDate().size(); i++) {
dateBeans.add(new RepaireLedgerEntity.DateBean(repaireLedgerEntity.getDate().get(i).getId(), repaireLedgerEntity.getDate().get(i).getProduct(), repaireLedgerEntity.getDate().get(i).getBarcode(), repaireLedgerEntity.getDate().get(i).getCustomer(), repaireLedgerEntity.getDate().get(i).getPerson(), repaireLedgerEntity.getDate().get(i).getReceive_date()));
}
for (int i = 0; i < dateBeans.size(); i++) {
Log.i("qiqiqiq", dateBeans.get(i).getId());
}
adapter.addData(dateBeans);
// adapter.disableLoadMoreIfNotFullPage();
adapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
@Override
public void onLoadMoreRequested() {
leder_recycler.postDelayed(new Runnable() {
@Override
public void run() {
if (mCurrentCounter >= 1000) {
//数据全部加载完毕
adapter.loadMoreEnd();
} else {
boolean isErr = true;
if (isErr) {
//成功获取更多数据
Log.i("zahuishi","t"+mCurrentCounter);
postRef(String.valueOf(mCurrentCounter));
mCurrentCounter += 20;
adapter.loadMoreComplete();
Retrofit retrofit = new Retrofit.Builder().baseUrl(address)
.addConverterFactory(GsonConverterFactory.create())//设置 Json 转换器
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())//RxJava 适配器
.build();
WorkPlaneService workPlaneService = retrofit.create(WorkPlaneService.class);
workPlaneService.getrepairedis(String.valueOf(mCurrentCounter))
.subscribeOn(Schedulers.io())//IO线程加载数据
.observeOn(AndroidSchedulers.mainThread())//主线程显示数据
.subscribe(new Subscriber<RepaireLedgerEntity>() {
@Override
public void onCompleted() {
Log.i("wwww","vos");
}
@Override
public void onError(Throwable e) {
Log.i("wwww",e.toString());
}
@Override
public void onNext(final RepaireLedgerEntity repaireLedgerEntity) {
if (repaireLedgerEntity.getState().equals("error")) {
Toast.makeText(RepaireLedgerManager.this, "网络错误,请重试", Toast.LENGTH_SHORT).show();
} else {
Log.i("zijia", "" + mCurrentCounter);
dateBeans = new ArrayList<>();
for (int i = 0; i < repaireLedgerEntity.getDate().size(); i++) {
dateBeans.add(new RepaireLedgerEntity.DateBean(repaireLedgerEntity.getDate().get(i).getId(), repaireLedgerEntity.getDate().get(i).getProduct(), repaireLedgerEntity.getDate().get(i).getBarcode(), repaireLedgerEntity.getDate().get(i).getCustomer(), repaireLedgerEntity.getDate().get(i).getPerson(), repaireLedgerEntity.getDate().get(i).getReceive_date()));
}
for (int i = 0; i < dateBeans.size(); i++) {
Log.i("qiqiqiq", dateBeans.get(i).getId());
}
adapter.addData(dateBeans);
mCurrentCounter += 20;
adapter.loadMoreComplete();
}
}
});
} else {
//获取更多数据失败
isErr = true;
Toast.makeText(RepaireLedgerManager.this, "加载失败", Toast.LENGTH_LONG).show();
adapter.loadMoreFail();
}
}
}
}, 1000);
}
}, leder_recycler);
}
}
});
}
总结:上拉加载数据重复原因是因为加载更多时设置了适配器,导致databeans中的数据还是上一次请求的数据,未发生改变
数据加载不全是因为我的for循环的i的变量设置的有问题