对PullToRefresh 和viewpaget有了认识后,在项目中也进行了实战,以下是对它们的综合使用:
先上效果:
首先定义view 并且初始化:
private Button findproblem_problemlisting_all;
private Button findproblem_problemlisting_accept;
private Button findproblem_problemlisting_answer;
private Button findproblem_problemlisting_unanswer;
private int page_all;
private int page_unanswer;
private int page_answer;
private int page_accept;
private int totalpage_all;
private int totalpage_unanswer;
private int totalpage_answer;
private int totalpage_accept;
private List<Problem> list_all;
private List<Problem> list_unanswer;
private List<Problem> list_answer;
private List<Problem> list_accept;
private PullToRefreshListView findproblem_problemlisting_listview_all;
private PullToRefreshListView findproblem_problemlisting_listview_unanswer;
private PullToRefreshListView findproblem_problemlisting_listview_answer;
private PullToRefreshListView findproblem_problemlisting_listview_accept;
private ProblemByFindAdapter allAdapter;
private ProblemByFindAdapter unanswerAdapter;
private ProblemByFindAdapter answerAdapter;
private ProblemByFindAdapter acceptAdapter;
private int listType;
private int tag;
private ViewPager findproblem_problemlisting_listview_vp;
private ArrayList<View> viewList;
实例化的过程:加载同一个XML文件
@Override
void init() {
findproblem_problemlisting_listview_all = (PullToRefreshListView) LayoutInflater.from(this).inflate(R.layout.findproblem_problemlisting_listview, null);
findproblem_problemlisting_listview_unanswer = (PullToRefreshListView) LayoutInflater.from(this).inflate(R.layout.findproblem_problemlisting_listview, null);
findproblem_problemlisting_listview_answer = (PullToRefreshListView) LayoutInflater.from(this).inflate(R.layout.findproblem_problemlisting_listview, null);
findproblem_problemlisting_listview_accept = (PullToRefreshListView) LayoutInflater.from(this).inflate(R.layout.findproblem_problemlisting_listview, null);
底部下拉刷新(共有三种形式:上拉、下拉、两者都可)
findproblem_problemlisting_listview_all.setMode(Mode.PULL_FROM_END);
findproblem_problemlisting_listview_unanswer.setMode(Mode.PULL_FROM_END);
findproblem_problemlisting_listview_answer.setMode(Mode.PULL_FROM_END);
findproblem_problemlisting_listview_accept.setMode(Mode.PULL_FROM_END);
把view添加到数组中(方便view的添加删除和数量的计算)
viewList.add(findproblem_problemlisting_listview_all);
viewList.add(findproblem_problemlisting_listview_unanswer);
viewList.add(findproblem_problemlisting_listview_answer);
viewList.add(findproblem_problemlisting_listview_accept);
ViewPager添加适配器
findproblem_problemlisting_listview_vp.setAdapter(new ListViewPagerAdapter());
ViewPager设置页数滚动的监听
findproblem_problemlisting_listview_vp.setOnPageChangeListener(new OnPageChangeListener() {
public void onPageSelected(int arg0) {
LogUtil.i(TAG, "滑动:" + arg0);
netHttp();
switch (arg0) {
case 0:
chooseButton(findproblem_problemlisting_all, false);
break;
case 1:
chooseButton(findproblem_problemlisting_unanswer, false);
break;
case 2:
chooseButton(findproblem_problemlisting_answer, false);
break;
case 3:
chooseButton(findproblem_problemlisting_accept, false);
break;
}
}
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
public void onPageScrollStateChanged(int arg0) {
}
});
}
ViewPager 滑动效果必须实现的四个方法;
private class ListViewPagerAdapter extends PagerAdapter {
@Override
public View instantiateItem(ViewGroup container, int position) {
LogUtil.i(TAG, "ViewPager.position:" + position);
container.addView(viewList.get(position));
return viewList.get(position);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView(viewList.remove(position));
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public int getCount() {
return viewList.size();
}
}
页面初始化时对数据开启异步任务,在view进行切换时显示相应的数据;
private void LoadData(int rangetype) {
if (!NetUtil.hasNetwork(this)) {
Toast.makeText(this, "网络不给力,请稍后重试", 0).show();
findproblem_data_loading.setVisibility(View.GONE);
findproblem_wifi.setVisibility(View.VISIBLE);
return;
}
findproblem_data_loading.setVisibility(View.VISIBLE);
new GetDataTask(0, rangetype).execute();
new GetDataTask(1, rangetype).execute();
new GetDataTask(2, rangetype).execute();
new GetDataTask(3, rangetype).execute();
对viewPager的每一个view进行刷新的监听
findproblem_problemlisting_listview_all.setOnRefreshListener(new OnRefreshListener2<ListView>() {
public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
}
public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
http(findproblem_problemlisting_listview_all);
new GetDataTask(0, 0).execute();
}
});
findproblem_problemlisting_listview_unanswer.setOnRefreshListener(new OnRefreshListener2<ListView>() {
下拉刷新
public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
}
上拉刷新
public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
http(findproblem_problemlisting_listview_unanswer);
new GetDataTask(1, 0).execute();
}
});
findproblem_problemlisting_listview_answer.setOnRefreshListener(new OnRefreshListener2<ListView>() {
public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
}
public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
http(findproblem_problemlisting_listview_answer);
new GetDataTask(2, 0).execute();
}
});
findproblem_problemlisting_listview_accept.setOnRefreshListener(new OnRefreshListener2<ListView>() {
public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
}
public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
http(findproblem_problemlisting_listview_accept);
new GetDataTask(3, 0).execute();
}
});
}
private class GetDataTask extends AsyncTask<Void, Void, List<Problem>> {
private int type;
private int rangetype;
private int page;
public GetDataTask(int type, int rangetype) {
super();
this.type = type;
this.rangetype = rangetype;
}
必须实现的一个方法:从服务器获取数据
@Override
protected List<Problem> doInBackground(Void... params) {
switch (type) {
case 0:
page_all++;
page = page_all;
break;
case 1:
page_unanswer++;
page = page_unanswer;
break;
case 2:
page_answer++;
page = page_answer;
break;
case 3:
page_accept++;
page = page_accept;
break;
}
RequestVo requestVo = new RequestVo();
requestVo.context = DisabuseActivity.this;
requestVo.requestUrl = Constants.FINDPROBLEM_URL;
requestVo.requestDataMap = new HashMap<String, String>();
requestVo.requestDataMap.put("keywords", keywords);
requestVo.requestDataMap.put("type", String.valueOf(type));
requestVo.requestDataMap.put("rangetype", String.valueOf(rangetype));
requestVo.requestDataMap.put("page", String.valueOf(page));
requestVo.requestDataMap.put("num", String.valueOf(10));
String data = (String) NetUtil.post(requestVo);
List<Problem> problemList = null;
try {
JSONArray jsonArray = new JSONArray(data);
JSONObject lastpJsonObject = jsonArray.getJSONObject(jsonArray.length() - 2);
int lastp = lastpJsonObject.getInt("lastp");
switch (type) {
case 0:
totalpage_all = lastp;
break;
case 1:
totalpage_unanswer = lastp;
break;
case 2:
totalpage_answer = lastp;
break;
case 3:
totalpage_accept = lastp;
break;
}
problemList = new ArrayList<Problem>();
for (int i = 0; i < jsonArray.length() - 2; i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
Problem problem = new Problem();
String pid = jsonObject.getString("pid");
String userid = jsonObject.getString("uid");
String click = jsonObject.getString("click");
String name = jsonObject.getString("name");
String headimg = jsonObject.getString("photo");
int type = jsonObject.getInt("type");
int status = jsonObject.getInt("status");
problem.setPid(pid);
problem.setUserid(userid);
problem.setType(type);
problem.setStatus(status);
problem.setClick(click);
problem.setHeadimg(headimg);
problem.setName(name);
switch (status) {
case 0:
String unanswer_problem = jsonObject.getString("problem");
String unanswer_createtime = jsonObject.getString("createtime");
problem.setUnanswer_createtime(unanswer_createtime);
problem.setUnanswer_problem(unanswer_problem);
break;
case 1:
case 3:
String answered_problem = jsonObject.getString("problem");
String answered_createtime = jsonObject.getString("createtime");
String answered_answer = jsonObject.getString("answer");
problem.setAnswered_answer(answered_answer);
problem.setAnswered_problem(answered_problem);
problem.setAnswered_createtime(answered_createtime);
break;
case 2:
String accept_problem = jsonObject.getString("problem");
String accept_createtime = jsonObject.getString("createtime");
String accept_answer = jsonObject.getString("answer");
problem.setAccept_answer(accept_answer);
problem.setAccept_createtime(accept_createtime);
problem.setAccept_problem(accept_problem);
break;
}
problemList.add(problem);
}
} catch (JSONException e) {
listViewHandler.sendEmptyMessage(0);
}
return problemList;
}
将获取的数据添加到list中对View notifyDatasetchange();
@Override
protected void onPostExecute(List<Problem> result) {
super.onPostExecute(result);
if (result == null) {
return;
}
switch (type) {
case 0:
LogUtil.i(TAG, "result.size():" + result.size() + ",type:" + type + ",rangetype:" + rangetype + ",page_unanswer:" + page_unanswer);
list_all.addAll(result);
allAdapter.notifyDataSetChanged();
findproblem_problemlisting_listview_all.onRefreshComplete();
下拉刷新请求的页数大于总页数时需要对view进行setLastItemVisableListener的监听
if (page_all >= totalpage_all) {
findproblem_problemlisting_listview_all.setMode(Mode.DISABLED);
findproblem_problemlisting_listview_all.setOnLastItemVisibleListener(new OnLastItemVisibleListener() {
public void onLastItemVisible() {
Toast.makeText(DisabuseActivity.this, "已到达底部!", Toast.LENGTH_SHORT).show();
}
});
}
break;
case 1:
LogUtil.i(TAG, "result.size():" + result.size() + ",type:" + type + ",rangetype:" + rangetype + ",page_answer:" + page_answer);
list_unanswer.addAll(result);
unanswerAdapter.notifyDataSetChanged();
findproblem_problemlisting_listview_unanswer.onRefreshComplete();
if (page_unanswer >= totalpage_unanswer) {
findproblem_problemlisting_listview_unanswer.setMode(Mode.DISABLED);
findproblem_problemlisting_listview_unanswer.setOnLastItemVisibleListener(new OnLastItemVisibleListener() {
public void onLastItemVisible() {
Toast.makeText(DisabuseActivity.this, "已到达底部!", Toast.LENGTH_SHORT).show();
}
});
}
break;
case 2:
LogUtil.i(TAG, "result.size():" + result.size() + ",type:" + type + ",rangetype:" + rangetype + ",page_accept:" + page_accept);
list_answer.addAll(result);
answerAdapter.notifyDataSetChanged();
findproblem_problemlisting_listview_answer.onRefreshComplete();
if (page_answer >= totalpage_answer) {
findproblem_problemlisting_listview_answer.setMode(Mode.DISABLED);
findproblem_problemlisting_listview_answer.setOnLastItemVisibleListener(new OnLastItemVisibleListener() {
public void onLastItemVisible() {
Toast.makeText(DisabuseActivity.this, "已到达底部!", Toast.LENGTH_SHORT).show();
}
});
}
break;
case 3:
LogUtil.i(TAG, "result.size():" + result.size() + ",type:" + type + ",rangetype:" + rangetype + ",page_accept:" + page_accept);
list_accept.addAll(result);
acceptAdapter.notifyDataSetChanged();
findproblem_problemlisting_listview_accept.onRefreshComplete();
if (page_accept >= totalpage_accept) {
findproblem_problemlisting_listview_accept.setMode(Mode.DISABLED);
findproblem_problemlisting_listview_accept.setOnLastItemVisibleListener(new OnLastItemVisibleListener() {
public void onLastItemVisible() {
Toast.makeText(DisabuseActivity.this, "已到达底部!", Toast.LENGTH_SHORT).show();
}
});
}
break;
}
findproblem_data_loading.setVisibility(View.GONE);
}
}