listview實現分頁載入資料的注意點
阿新 • • 發佈:2019-01-03
/**
* 未讀資訊list
*/
public class MessageSecondLayerActivity extends Activity implements View.OnClickListener {
private ListView listView;
private List<MessageItem> list ;
//分頁的時候得重新建立一個list,不能同時建立一個list還把他傳遞給adapter。
// adapter的建構函式必須傳遞list,且在建構函式中使用this.list=list
// 否則可能出現,1,點選第二屏後的抱越界2,載入到最後一頁卡死
private List<MessageItem> appendlist ;
private ActionBar title;
private int currentPage;
private int totalpages;
private MessageAdapter adapter;
private TextView footertv,msgNullTv;
private LinearLayout footer;
private int MESSAGE=5;
private String msgType;
private static String urlString = Urls.base_url + SystemParams.getAppChannel() + "/" + Urls.GETMSGLIST;
private static String actUrlString=Urls.base_url + SystemParams.getAppChannel() + "/"+Urls.GETACTIVITYLIST;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_message);
}
@Override
protected void onResume() {
super.onResume();
initParams();
initTitle();
initlistview();
}
private void initParams() {
currentPage=1;
totalpages=1;
list = new ArrayList<>();
appendlist=new ArrayList<>();
.....
}
private void initTitle() {
........
}
private void initlistview() {
footer= (LinearLayout) getLayoutInflater().inflate(R.layout.message_page_end_text, null);
footertv= (TextView) footer.findViewById(R.id.righttv);
footertv.setText(getResources().getString(R.string.message_page_end));
footertv.setGravity(Gravity.CENTER);
footer.setVisibility(View.INVISIBLE);
listView = (ListView) findViewById(R.id.smslistview);
listView.addFooterView(footer,null,false);//必須在setAdapter之前呼叫
adapter = new MessageAdapter(list);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {//點選item處理
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
.........
}
});
listView.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {//滾動停止
if (view.getLastVisiblePosition() == view.getCount() - 1) {
footer.setVisibility(View.VISIBLE);
visitSysReplyService(currentPage);
if (currentPage==totalpages+1){//分頁到底
ToastDisplayUtils.ToastCenterDisplay(0,0,"只能顯示最近三個月的訊息,更多訊息請訪問底部的網站",MessageSecondLayerActivity.this);
footertv.setText(getResources().getString(R.string.message_end));
}
}
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
}
});
}
/**系統訊息、問題回覆
* 訪問伺服器
* @param page
*/
private void visitSysReplyService(int page) {
if (page<=totalpages){
CommonUtil.showProgressDialog(this, getString(R.string.doing));
Map<String, String> params = new HashMap<String, String>();
JSONObject jsonObj = new JSONObject();
try {
jsonObj.put("token", LoginAccountEntityManager.getInstance().getModelData().getToken());
jsonObj.put("page", page);//要有類似的引數
jsonObj.put("size", 10);
...
params.put("params", jsonObj.toString());
HttpCaller.getInstance().servicePost(urlString, params, new HttpHandler() {
public void handleResult(Object result) {
try {
JSONObject jsonObject = new JSONObject(result.toString());
System.out.println("---->"+result.toString());
System.out.println(((JSONObject) result).getJSONArray("list").length()>0);
if (jsonObject.optBoolean("success")) {
if (((JSONObject) result).getJSONArray("list").length()>0) {//防止為空
msgNullTv.setVisibility(View.GONE);
listView.setVisibility(View.VISIBLE);
++currentPage;
jsonServerData(result);
}else{
listView.setVisibility(View.GONE);
msgNullTv.setVisibility(View.VISIBLE);
}
if (!jsonObject.optString("page").equals("")) {//防止為空
JSONObject pageobject = new JSONObject(jsonObject.optString("page"));
totalpages = pageobject.optInt("totalPages");
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
public void onAfterHandle(boolean success) {
CommonUtil.closeProgressDialog();
}
});
} catch (JSONException e) {
e.printStackTrace();
}
}
}
.......
* 未讀資訊list
*/
public class MessageSecondLayerActivity extends Activity implements View.OnClickListener {
private ListView listView;
private List<MessageItem> list ;
//分頁的時候得重新建立一個list,不能同時建立一個list還把他傳遞給adapter。
// adapter的建構函式必須傳遞list,且在建構函式中使用this.list=list
// 否則可能出現,1,點選第二屏後的抱越界2,載入到最後一頁卡死
private List<MessageItem> appendlist ;
private ActionBar title;
private int currentPage;
private int totalpages;
private MessageAdapter adapter;
private TextView footertv,msgNullTv;
private LinearLayout footer;
private int MESSAGE=5;
private String msgType;
private static String urlString = Urls.base_url + SystemParams.getAppChannel() + "/" + Urls.GETMSGLIST;
private static String actUrlString=Urls.base_url + SystemParams.getAppChannel() + "/"+Urls.GETACTIVITYLIST;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_message);
}
@Override
protected void onResume() {
super.onResume();
initParams();
initTitle();
initlistview();
}
private void initParams() {
currentPage=1;
totalpages=1;
list = new ArrayList<>();
appendlist=new ArrayList<>();
.....
}
private void initTitle() {
........
}
private void initlistview() {
footer= (LinearLayout) getLayoutInflater().inflate(R.layout.message_page_end_text, null);
footertv= (TextView) footer.findViewById(R.id.righttv);
footertv.setText(getResources().getString(R.string.message_page_end));
footertv.setGravity(Gravity.CENTER);
footer.setVisibility(View.INVISIBLE);
listView = (ListView) findViewById(R.id.smslistview);
listView.addFooterView(footer,null,false);//必須在setAdapter之前呼叫
adapter = new MessageAdapter(list);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {//點選item處理
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
.........
}
});
listView.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {//滾動停止
if (view.getLastVisiblePosition() == view.getCount() - 1) {
footer.setVisibility(View.VISIBLE);
visitSysReplyService(currentPage);
if (currentPage==totalpages+1){//分頁到底
ToastDisplayUtils.ToastCenterDisplay(0,0,"只能顯示最近三個月的訊息,更多訊息請訪問底部的網站",MessageSecondLayerActivity.this);
footertv.setText(getResources().getString(R.string.message_end));
}
}
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
}
});
}
/**系統訊息、問題回覆
* 訪問伺服器
* @param page
*/
private void visitSysReplyService(int page) {
if (page<=totalpages){
CommonUtil.showProgressDialog(this, getString(R.string.doing));
Map<String, String> params = new HashMap<String, String>();
JSONObject jsonObj = new JSONObject();
try {
jsonObj.put("token", LoginAccountEntityManager.getInstance().getModelData().getToken());
jsonObj.put("page", page);//要有類似的引數
jsonObj.put("size", 10);
...
params.put("params", jsonObj.toString());
HttpCaller.getInstance().servicePost(urlString, params, new HttpHandler() {
public void handleResult(Object result) {
try {
JSONObject jsonObject = new JSONObject(result.toString());
System.out.println("---->"+result.toString());
System.out.println(((JSONObject) result).getJSONArray("list").length()>0);
if (jsonObject.optBoolean("success")) {
if (((JSONObject) result).getJSONArray("list").length()>0) {//防止為空
msgNullTv.setVisibility(View.GONE);
listView.setVisibility(View.VISIBLE);
++currentPage;
jsonServerData(result);
}else{
listView.setVisibility(View.GONE);
msgNullTv.setVisibility(View.VISIBLE);
}
if (!jsonObject.optString("page").equals("")) {//防止為空
JSONObject pageobject = new JSONObject(jsonObject.optString("page"));
totalpages = pageobject.optInt("totalPages");
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
public void onAfterHandle(boolean success) {
CommonUtil.closeProgressDialog();
}
});
} catch (JSONException e) {
e.printStackTrace();
}
}
}
.......