focus on the setLoadMoreData (String json) method.
Class Code:
public class WatchHistoryActivity extends BaseActivity implements View.OnClickListener, BaseQuickAdapter.RequestLoadMoreListener {
@BindView(R.id.rv_watch_history)
RecyclerView mRecyclerView;
@BindView(R.id.rl_watch_history_empty_view)
RelativeLayout mEmptyView;
@BindView(R.id.rl_watch_history_loading)
RelativeLayout mLoading;
private static final String TAG = "WatchHistoryActivity";
private static final int INIT_DATA_SUCCESS = 0;
private static final int INIT_DATA_FAIL = 1;
private static final int NETWORK_PROBLEM = 2;
private static final int LOAD_MORE_FAIL = 3;
private List<WatchHistoryBean> mDatas;
private WatchHistoryAdapter mAdapter;
private String mToken;
private WatchHistoryHandler mHandler = new WatchHistoryHandler();
private int mCurrentCounter;
private boolean isErr = true;
private int mSize = 1;
@Override
protected View initView() {
View view = View.inflate(this, R.layout.activity_watch_history, null);
ButterKnife.bind(this, view);
mTvTitle.setText("");
mIvBack.setImageResource(R.mipmap.back);
// mTvMore.setText("");
frameLayout.addView(view);
return view;
}
@Override
protected void initData() {
mLoading.setVisibility(View.VISIBLE);
mToken = PreferenceUtils.getString(this, "token");
getInitData();
}
@Override
protected void initEvent() {
mIvBack.setOnClickListener(this);
}
/**
*
*/
private void getInitData() {
new Thread(new Runnable() {
@Override
public void run() {
try {
Response response = NetWorkRequestUtils.postRequest(ConstantsUtils.HEAD_URL + "/usercenter/favorite", new InitWatchHistoryObject());
if (response.isSuccessful()) {
String json = response.body().string();
Log.d(TAG, "= " + json);
setInitData(json);
} else {
Log.d(TAG, "1111111111");
Message obtain = Message.obtain();
obtain.what = INIT_DATA_FAIL;
mHandler.sendMessage(obtain);
}
} catch (Exception e) {
Log.d(TAG, "222222");
Message obtain = Message.obtain();
obtain.what = NETWORK_PROBLEM;
mHandler.sendMessage(obtain);
e.printStackTrace();
}
}
}).start();
}
/**
*
*/
private void setInitData(String json) {
//
Gson gson = new Gson();
WatchHistoryBean watchHistoryBean = gson.fromJson(json, WatchHistoryBean.class);
List<WatchHistoryBean.DataBean> data = watchHistoryBean.getData();
mDatas = new ArrayList<>();
for (int i = 0; i < data.size(); iPP) {
WatchHistoryBean.DataBean dataBean = data.get(i);
WatchHistoryBean bean = new WatchHistoryBean();
String title = dataBean.getName();
String date = dataBean.getUpdateTime();
int watchCount = dataBean.getClick();
String img = dataBean.getImg();
String length = dataBean.getLenght();
bean.setTitle(title);//
bean.setWatchCont(watchCount + "");//
bean.setDate(date);//
bean.setImgUrl(img);//
bean.setTotalTime(length);//
bean.setData(data);
mDatas.add(bean);
}
Message obtain = Message.obtain();
obtain.what = INIT_DATA_SUCCESS;
mHandler.sendMessage(obtain);
}
@Override
public void onLoadMoreRequested() {
mRecyclerView.postDelayed(new Runnable() {
@Override
public void run() {
getLoadMoreData();//
}
}, 1000);
}
private void getLoadMoreData() {
mSize = mSize + 1;
new Thread(new Runnable() {
@Override
public void run() {
try {
Response response = NetWorkRequestUtils.postRequest(ConstantsUtils.HEAD_URL + "/usercenter/favorite", new MoreWatchHistoryObject());
if (response.isSuccessful()) {
String json = response.body().string();
Log.d(TAG, "= " + json);
setLoadMoreData(json);
} else {
Log.d(TAG, "111111111");
Message obtain = Message.obtain();
obtain.what = LOAD_MORE_FAIL;
mHandler.sendMessage(obtain);
}
} catch (Exception e) {
Log.d(TAG, "2222222222");
Message obtain = Message.obtain();
obtain.what = LOAD_MORE_FAIL;
mHandler.sendMessage(obtain);
e.printStackTrace();
}
}
}).start();
}
/**
*
*/
private void setLoadMoreData(String json) {
Gson gson = new Gson();
WatchHistoryBean watchHistoryBean = gson.fromJson(json, WatchHistoryBean.class);
final List<WatchHistoryBean.DataBean> data = watchHistoryBean.getData();
mDatas = new ArrayList<>();
final int totalCount = mCurrentCounter + data.size();//
runOnUiThread(new Runnable() {
@Override
public void run() {
if (mCurrentCounter >= totalCount) {
//
mAdapter.loadMoreEnd();
} else {
if (isErr) {
for (int i = 0; i < data.size(); iPP) {
WatchHistoryBean.DataBean dataBean = data.get(i);
WatchHistoryBean bean = new WatchHistoryBean();
String title = dataBean.getName();
String date = dataBean.getUpdateTime();
int watchCount = dataBean.getClick();
String img = dataBean.getImg();
String length = dataBean.getLenght();
bean.setTitle(title);//
bean.setWatchCont(watchCount + "");//
bean.setDate(date);//
bean.setImgUrl(img);//
bean.setTotalTime(length);//
bean.setData(data);//!data
mAdapter.addData(bean);
mDatas.add(bean);
}
//positiontotalCount,
for (int i = 0; i < totalCount; iPP) {
WatchHistoryBean.DataBean dataBean = data.get(i);
data.add(dataBean);
}
mCurrentCounter = mAdapter.getData().size();
//
mAdapter.loadMoreComplete();
} else {
//
isErr = true;
//
mAdapter.loadMoreFail();
}
}
}
});
}
private class WatchHistoryHandler extends Handler {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case INIT_DATA_SUCCESS://
mLoading.setVisibility(View.GONE);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(WatchHistoryActivity.this);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
mRecyclerView.setLayoutManager(linearLayoutManager);
mRecyclerView.addItemDecoration(new DividerItemDecoration(WatchHistoryActivity.this, 1));
mAdapter = new WatchHistoryAdapter(R.layout.item_watch_history, mDatas, WatchHistoryActivity.this);
mRecyclerView.setAdapter(mAdapter);
mAdapter.setOnLoadMoreListener(WatchHistoryActivity.this);
break;
case INIT_DATA_FAIL://
mLoading.setVisibility(View.GONE);
Toast.makeText(WatchHistoryActivity.this, "", Toast.LENGTH_SHORT).show();
break;
case NETWORK_PROBLEM://
mLoading.setVisibility(View.GONE);
Toast.makeText(WatchHistoryActivity.this, "", Toast.LENGTH_SHORT).show();
break;
case LOAD_MORE_FAIL://
mLoading.setVisibility(View.GONE);
Toast.makeText(WatchHistoryActivity.this, "", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.iv_back:
finish();
break;
default:
break;
}
}
/**
*
*/
private class InitWatchHistoryObject {
String token = mToken;
String type = "2";
int p = 1;
}
private class MoreWatchHistoryObject {
String token = mToken;
String type = "2";
int p = mSize;
}
}