<meter id="pryje"><nav id="pryje"><delect id="pryje"></delect></nav></meter>
          <label id="pryje"></label>

          新聞中心

          EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計應(yīng)用 > Android 中 ListView 分頁加載數(shù)據(jù)

          Android 中 ListView 分頁加載數(shù)據(jù)

          作者: 時間:2016-10-08 來源:網(wǎng)絡(luò) 收藏

          熟悉Android的朋友們都知道,不管是微博客戶端還是新聞客戶端,都離不開列表組件,可以說列表組件是Android數(shù)據(jù)展現(xiàn)方面最重要的組 件,我們今天就要講一講列表組件ListView加載數(shù)據(jù)的相關(guān)內(nèi)容。通常來說,一個應(yīng)用在展現(xiàn)大量數(shù)據(jù)時,不會將全部的可用數(shù)據(jù)都呈現(xiàn)給用戶,因為這不 管對于服務(wù)端還是客戶端來說都是不小的壓力,因此,很多應(yīng)用都是采用分批次加載的形式來獲取用戶所需的數(shù)據(jù)。比如:微博客戶端可能會在用戶滑動至列表底端 時自動加載下一頁數(shù)據(jù),也可能在底部放置一個“加載更多”按鈕,用戶點擊后,加載下一頁數(shù)據(jù)。

          本文引用地址:http://www.ex-cimer.com/article/201610/305462.htm

          我們今天就結(jié)合實例來演示一下使用ListView獲取數(shù)據(jù)的過程。

          新建一個loadmore項目,我們來看一下結(jié)構(gòu)圖和最終效果圖:

          左圖中包含了三個布局文件、一個Adapter和一個Activity,右圖是我們運行后的主界面。

          其中,main.xml是主界面的布局文件,它包含一個ListView組件,代碼如下:

          01

          02

          03 android:orientation=vertical

          04 android:layout_width=fill_parent

          05 android:layout_height=fill_parent

          06 android:paddingLeft=3dp

          07 android:paddingRight=3dp>

          08

          09 android:id=@id/android:list

          10 android:layout_width=fill_parent

          11 android:layout_height=wrap_content/>

          12

          這里我們引用了Android內(nèi)置的名為list的id,因為我們后面要使用到ListActivity,我們的MainActivity繼承于它。

          然后就是list_item.xml,它是ListView中單個列表項的布局文件,從效果圖中可以看到,這里只使用到了一個TextView組件,list_item.xml代碼如下:

          01

          02

          03 android:orientation=vertical

          04 android:layout_width=fill_parent

          05 android:layout_height=fill_parent>

          06

          07 android:id=@+id/list_item_text

          08 android:layout_width=fill_parent

          09 android:layout_height=fill_parent

          10 android:gravity=center

          11 android:textSize=20sp

          12 android:paddingTop=10dp

          13 android:paddingBottom=10dp/>

          14

          我們注意到在右圖中列表底部有一個按鈕不同于其他的列表項,這是什么情況?事實上這個按鈕是我們在ListView底部添加的一個視圖。ListView 組件提供了兩個很實用的功能,那就是可以在頂部和底部添加自定義的視圖。我們在此處ListView的底部添加了一個視圖用來加載更多數(shù)據(jù),這個視圖對應(yīng) 著load_more.xml布局文件,代碼如下:

          01

          02

          03 xmlns:android=http://schemas.android.com/apk/res/android

          04 android:orientation=vertical

          05 android:layout_width=fill_parent

          06 android:layout_height=wrap_content>

          07

          08 android:id=@+id/loadMoreButton

          09 android:layout_width=fill_parent

          10 android:layout_height=wrap_content

          11 android:text=load more

          12 android:onClick=loadMore/>

          13

          接下來我們來了解一下我們的Adapter,ListViewAdapter代碼如下:

          01package com.scott.loadmore;

          02

          03import java.util.List;

          04

          05import android.content.Context;

          06import android.view.LayoutInflater;

          07import android.view.View;

          08import android.view.ViewGroup;

          09import android.widget.BaseAdapter;

          10import android.widget.TextView;

          11

          12public class ListViewAdapter extends BaseAdapter {

          13 private List items;

          14 private LayoutInflater inflater;

          15

          16 public ListViewAdapter(Context context, List items) {

          17 this.items = items;

          18 inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

          19 }

          20

          21 @Override

          22 public int getCount() {

          23 return items.size();

          24 }

          25

          26 @Override

          27 public Object getItem(int position) {

          28 return items.get(position);

          29 }

          30

          31 @Override

          32 public long getItemId(int position) {

          33 return position;

          34 }

          35

          36 @Override

          37 public View getView(int position, View view, ViewGroup parent) {

          38 if (view == null) {

          39 view = inflater.inflate(R.layout.list_item, null);


          上一頁 1 2 3 下一頁

          關(guān)鍵詞:

          評論


          相關(guān)推薦

          技術(shù)專區(qū)

          關(guān)閉
          看屁屁www成人影院,亚洲人妻成人图片,亚洲精品成人午夜在线,日韩在线 欧美成人 (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })();