一、ListView分頁:
(一)、目的:
Android 應(yīng)用開發(fā)中,采用ListView組件來展示數(shù)據(jù)是很常用的功能,當(dāng)一個應(yīng)用要展現(xiàn)很多的數(shù)據(jù)時,一般情況下都不會把所有的數(shù)據(jù)一次就展示出來,而是通過 分頁的形式來展示數(shù)據(jù),這樣會有更好的用戶體驗。因此,很多應(yīng)用都是采用分批次加載的形式來獲取用戶所需的數(shù)據(jù)。例如:微博客戶端可能會在用戶滑 動至列表底端時自動加載下一頁數(shù)據(jù),也可能在底部放置一個"查看更多"按鈕,用戶點擊后,加載下一頁數(shù)據(jù)。
(二)、核心技術(shù)點:
1. 借助 ListView組件的OnScrollListener監(jiān)聽事件,去判斷何時該加載新數(shù)據(jù);
2. 往服務(wù)器get傳遞表示頁碼的參數(shù):page。而該page會每加載一屏數(shù)據(jù)后自動加一;
3. 利用addAll()方法不斷往list集合末端添加新數(shù)據(jù),使得適配器的數(shù)據(jù)源每新加載一屏數(shù)據(jù)就發(fā)生變化;
4. 利用適配器對象的notifyDataSetChanged()方法。該方法的作用是通知適配器自己及與該數(shù)據(jù)有關(guān)的view,數(shù)據(jù)已經(jīng)發(fā)生變動,要刷新自己、更新數(shù)據(jù)。
(三)、OnScrollListener監(jiān)聽事件 :
1、該監(jiān)聽器中有兩個需要實現(xiàn)的方法:
· onScrollStateChanged(AbsListView view, int scrollState):監(jiān)聽屏幕的滾動狀態(tài)的變動情況
· onScroll(AbsListView view, int firstVisibleItem,int visibleItemCount, int totalItemCount):監(jiān)聽屏幕滾動的item的數(shù)量
2、scrollState 回調(diào)順序如下:
· 第1次:scrollState = SCROLL_STATE_TOUCH_SCROLL(1):表示正在滾動。當(dāng)屏幕滾動且用戶使用的觸碰或手指還在屏幕上時為1
· 第2次:scrollState =SCROLL_STATE_FLING(2) :表示手指做了拋的動作(手指離開屏幕前,用力滑了一下,屏幕產(chǎn)生慣性滑動)。
· 第3次:scrollState =SCROLL_STATE_IDLE(0) :表示屏幕已停止。屏幕停止?jié)L動時為0。
3、onScroll中參數(shù)講解:
· firstVisibleItem:當(dāng)前窗口中能看見的第一個列表項ID(從0開始)
· visibleItemCount:當(dāng)前窗口中能看見的列表項的個數(shù)(小半個也算)
· totalItemCount:列表項的總數(shù)
4、思路:
· 當(dāng)滾到最后一條,加載新數(shù)據(jù);
· 適配器的數(shù)據(jù)源要進(jìn)行累加:totalList.addAll(list);
· 數(shù)據(jù)發(fā)生變化,適配器通知:adapter.notifyDataSetChanged();【牢記】
· 判斷是否滾到最后一行。
示例代碼:
if (firstVisibleItem + visibleItemCount == totalItemCount ) {
isBottom = true;
}
(四)、頁面效果:
【要求:】
當(dāng)屏幕滑動到最后一條時,顯示“點擊加載數(shù)據(jù)”的提示。點擊后可以加載新的數(shù)據(jù)。當(dāng)向上滑動或者沒有到最后一條時,不顯示“點擊加載數(shù)據(jù)”的提示。
(五)、核心代碼:
1、布局文件的核心代碼:
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ListView
android:id="@+id/listView_main"
android:layout_below="@+id/button_main_init"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/layout_main_nextpage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#000"
android:visibility="invisible"
android:gravity="center"
android:onClick="clickButton"
android:padding="5dp">
<ProgressBar
android:id="@+id/progressBar_main"
style="?android:attr/progressBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/text_main_nextpage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:textSize="18sp"
android:onClick="clickButton"
android:textColor="#fff"
android:text="點擊加載更多數(shù)據(jù)" />
2、Activity頁面核心代碼:
public class MainActivity extends Activity {
private String TAG = "MainActivity";
private ListView listView_main;
private LinearLayout layout_main_nextpage;
private MySQLiteDatabaseHelper dbHelper = null;
// 用于分頁顯示數(shù)據(jù)的屬性
private int pageSize = 30;// 每頁顯示的條數(shù)
private int curPage = 1;
private int rowCount = 0;
private int pageCount = 0;// 總頁數(shù)
private boolean isBottom = false;// 判斷是否滾動到數(shù)據(jù)最后一條
private List<Map<String, Object>> totalList = null;// 加載到適配器中的數(shù)據(jù)源
private SimpleAdapter adapter = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView_main = (ListView) findViewById(R.id.listView_main);
layout_main_nextpage = (LinearLayout) findViewById(R.id.layout_main_nextpage);
// 實例化訪問數(shù)據(jù)庫幫助類
dbHelper = new MySQLiteDatabaseHelper();
// 獲取數(shù)據(jù)表一共有多少條,從而計算共有多少頁
rowCount = dbHelper.selectCount("select id from android_basic", null);
// 計算總頁碼數(shù)
pageCount = (int) Math.ceil(rowCount / (float) pageSize);
// 如果當(dāng)前頁為第一頁,則數(shù)據(jù)源集合中就是第一頁的內(nèi)容
if (curPage == 1) {
totalList = getCurpageList(1);
}
adapter = new SimpleAdapter(this, totalList,
R.layout.item_listview_main, new String[] { "_id", "title" },
new int[] { R.id.text_item_listview_id,
R.id.text_item_listview_title });
listView_main.setAdapter(adapter);
// 給ListView對象設(shè)置滾動監(jiān)聽器,以此來判斷是否已經(jīng)滾動到最后一條,從而決定是否加載新數(shù)據(jù)
listView_main.setOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
if (isBottom) {
// 如果滾到最后一條數(shù)據(jù)(即:屏幕最底端),則顯示:“加載更多新數(shù)據(jù)”
if (curPage < pageCount) {
layout_main_nextpage.setVisibility(View.VISIBLE);
}
} else {
layout_main_nextpage.setVisibility(View.GONE);
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
// Log.i(TAG, "==" + firstVisibleItem + ":::" + visibleItemCount
// + ":::" + totalItemCount);
// 判斷是否已經(jīng)滾動到了最后一條,從而決定是否提示加載新數(shù)據(jù)
isBottom = (firstVisibleItem + visibleItemCount == totalItemCount);
}
});
}
public void clickButton(View view) {
switch (view.getId()) {
case R.id.layout_main_nextpage:
// Log.i(TAG, "==" + curPage + ":::" + pageCount);
// 如果不是最后一頁,則讓當(dāng)前頁碼累加,讓數(shù)據(jù)源累加新數(shù)據(jù),并通知適配器信息發(fā)生變化
if (curPage < pageCount) {
curPage++;
totalList.addAll(getCurpageList(curPage));
adapter.notifyDataSetChanged();
}
// 只要點擊了提示“加載新數(shù)據(jù)”的信息,就讓其隱藏
layout_main_nextpage.setVisibility(View.GONE);
break;
default:
break;
}
}
// 獲取每一頁的數(shù)據(jù),返回List集合
private List<Map<String, Object>> getCurpageList(int currentPage) {
int offset = (currentPage - 1) * pageSize;
String sql = "select id _id ,title from android_basic limit ? , ?";
return dbHelper.selectData(sql, new String[] { offset + "",
pageSize + "" });
}
}
以上便是千鋒軟件測試培訓(xùn)老師分享的ListView分頁知識,學(xué)習(xí)軟件測試,可以參考千鋒提供的軟件測試學(xué)習(xí)路線,內(nèi)容包含軟件測試環(huán)境配置與管理,數(shù)據(jù)庫測試技術(shù),軟件測試編程技術(shù),應(yīng)用程序測試技術(shù),互聯(lián)網(wǎng)/移動互聯(lián)網(wǎng)測試技術(shù)等,根據(jù)千鋒提供的軟件測試學(xué)習(xí)路線圖,可以讓你對學(xué)好軟件測試需要掌握的知識有個清晰的了解,并能快速入門軟件測試。