參考資料:
首先新建一個(gè)layout文件,命名為moredata.xml,包含一個(gè)Button和一個(gè)ProgressBar,xml代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/bt_load"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="加載更多數(shù)據(jù)" />
<ProgressBar
android:id="@+id/pg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:visibility="gone"
/>
</LinearLayout>
然后定義一個(gè)item.xml,其作用是定義ListView的每個(gè)item的視圖,代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tv_title"
android:textSize="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
/>
<TextView
android:textSize="12sp"
android:id="@+id/tv_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
/>
</LinearLayout>
下面是主MainActivity.java代碼:
package cn.llbb.testlistview2;
import android.support.v7.app.ActionBarActivity;
import java.util.*;
import android.os.Bundle;
import android.os.Handler;
import android.view.*;
import android.view.View.*;
import android.widget.*;
import android.widget.AbsListView.OnScrollListener;
public class MainActivity extends ActionBarActivity implements OnScrollListener{
private ListView list = null;
private SimpleAdapter mSimpleAdapter;
private Button bt;
private ProgressBar pg;
private Handler handler;
private ArrayList<HashMap<String,String>> listitem;
private View moreView;
private int MaxDateNum;
private int lastVisibleIndex;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = (ListView) findViewById(R.id.listView1);
MaxDateNum = 65;
listitem = new ArrayList<HashMap<String,String>>();
moreView = getLayoutInflater().inflate(R.layout.moredata, null);
bt = (Button) moreView.findViewById(R.id.bt_load);
pg = (ProgressBar) moreView.findViewById(R.id.pg);
handler = new Handler();
listitem = new ArrayList<HashMap<String,String>>();
for(int i = 0;i < 10; i++){
HashMap<String, String> item = new HashMap<String, String>();
item.put("ItemTitle", "第" + i + "行標(biāo)題");
item.put("ItemText", "第" + i + "行內(nèi)容");
listitem.add(item);
}
mSimpleAdapter = new SimpleAdapter(this, listitem, R.layout.item,
new String[]{"ItemTitle","ItemText"},
new int[]{R.id.tv_title,R.id.tv_content});
list.addFooterView(moreView);
list.setAdapter(mSimpleAdapter);
bt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
pg.setVisibility(View.VISIBLE);
bt.setVisibility(View.GONE);
handler.postDelayed(new Runnable() {
@Override
public void run() {
loadMoreDate();// 加載更多數(shù)據(jù)
bt.setVisibility(View.VISIBLE);
pg.setVisibility(View.GONE);
mSimpleAdapter.notifyDataSetChanged();// 通知listView刷新數(shù)據(jù)
}
}, 2000);
}
});
}
private void loadMoreDate() {
int count = mSimpleAdapter.getCount();
if (count + 5 < MaxDateNum) {
// 每次加載5條
for (int i = count; i < count + 5; i++) {
HashMap<String, String> map = new HashMap<String, String>();
map.put("ItemTitle", "新增第" + i + "行標(biāo)題");
map.put("ItemText", "新增第" + i + "行內(nèi)容");
listitem.add(map);
}
} else {
// 數(shù)據(jù)已經(jīng)不足5條
for (int i = count; i < MaxDateNum; i++) {
HashMap<String, String> map = new HashMap<String, String>();
map.put("ItemTitle", "新增第" + i + "行標(biāo)題");
map.put("ItemText", "新增第" + i + "行內(nèi)容");
listitem.add(map);
}
}
}
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
lastVisibleIndex = firstVisibleItem + visibleItemCount - 1;
if (totalItemCount == MaxDateNum + 1) {
list.removeFooterView(moreView);
Toast.makeText(this, "數(shù)據(jù)全部加載完成,沒有更多數(shù)據(jù)!", Toast.LENGTH_LONG).show();
}
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
if (scrollState == OnScrollListener.SCROLL_STATE_IDLE
&& lastVisibleIndex == mSimpleAdapter.getCount()) {
}
}
}
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
網(wǎng)頁標(biāo)題:ListView的分頁加載功能-創(chuàng)新互聯(lián)
文章出自:http://chinadenli.net/article14/cogsge.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供做網(wǎng)站、Google、品牌網(wǎng)站設(shè)計(jì)、企業(yè)網(wǎng)站制作、全網(wǎng)營銷推廣、定制網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容