public class HomeCircleFragment extends BaseFragment {
public static final String SEARCH_CIRCLE_RESULTS = "search_circle_result";
private View mView;
private XRecyclerView mRecyclerView;
private CircleListAdapter adapter;
private List<HomeCircleBean> circleList = new ArrayList<>();
private HomeCircleBean circleBean;
private int mCurrentPageIndex = 1;
private HomeCircleAsyncTask asyncTask;
private JSONArray result;
private List<HomeCircleBean> circleBeanList;
private int count;
private Dialog dialog;
@Override
public int setResourceLayoutId() {
return R.layout.all_circle_layout;
}
@Override
public int setRecyclerViewId() {
return R.id.mRecyclerView;
}
@Override
public void initView() {
mView = getmView();
dialog = ToastUtils.createLoadingDialog(mView.getContext(),getString(R.string.common_loading));
adapter = new CircleListAdapter(circleList);
}
@Override
public void initListener() {
mRecyclerView = getmRecyclerView();
mRecyclerView.addItemDecoration(new XRecyclerViewDivider(mView.getContext(), LinearLayoutManager.HORIZONTAL));
mRecyclerView.setAdapter(adapter);
mRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
@Override
public void onRefresh() {
refreshData();
}
@Override
public void onLoadMore() {
loadmoreData();
}
});
}
@Override
public void refreshData() {
circleList.clear();
mCurrentPageIndex = 1;
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
new HomeCircleAsyncTask().execute(ApiUtils.CIRCLES+"?page="+mCurrentPageIndex);
mRecyclerView.refreshComplete();
}
},500);
}
private void loadmoreData() {
mCurrentPageIndex++;
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
if (adapter.getItemCount() >= count) {
mRecyclerView.setNoMore(true);
} else {
new HomeCircleAsyncTask().execute(ApiUtils.CIRCLES+"?page="+mCurrentPageIndex);
mRecyclerView.loadMoreComplete();
}
}
},500);
}
class HomeCircleAsyncTask extends AsyncTask<String, Void, List<HomeCircleBean>> {
@Override
protected void onPreExecute() {
super.onPreExecute();
dialog.show();
}
@Override
protected List<HomeCircleBean> doInBackground(String... strings) {
IRequest request = new BaseRequest(strings[0]);
IHttpClient mHttpClient = new OkHttpClientImpl();
IResponse response = mHttpClient.get(request);
String data = response.getData().toString();
try {
JSONObject jsonObject = new JSONObject(data);
count = jsonObject.getInt("count");
result = jsonObject.getJSONArray("results");
} catch (JSONException e) {
e.printStackTrace();
}
Gson gson = new Gson();
circleBeanList =
gson.fromJson(result.toString(), new TypeToken<List<HomeCircleBean>>() {
}.getType());
return circleBeanList;
}
@Override
protected void onPostExecute(List<HomeCircleBean> homeCircleBeans) {
super.onPostExecute(homeCircleBeans);
for (HomeCircleBean circle : homeCircleBeans) {
circleBean =
new HomeCircleBean(circle.getId(), circle.getImage(), circle.getName(), circle.getDesc(),
circle.getUser(), circle.getAddress(), circle.getAdd_time());
circleList.add(circleBean);
}
adapter.notifyDataSetChanged();
dialog.dismiss();
}
}
}