.Net程式員玩轉Android開發---(16)ListView分頁事件,androidlistview

來源:互聯網
上載者:User

.Net程式員玩轉Android開發---(16)ListView分頁事件,androidlistview

        ListView在載入是資料的時候,如果一次性把所有資料都載入出來,這樣如果資料量大的話,效率低,效能差,通常情況下採取的措施是分頁載入,只載入當前頁數量的資料。這一節我們示範下ListView怎麼分頁載入資料。首先看下

            


         1. 建立載入進度列

                        ListVIew每次載入的時候,會在底部有一個載入進度條欄,顯示載入中,我們建立一個這樣的布局檔案,代碼如下

                      

<?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" >        <LinearLayout     android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"     android:gravity="center"     android:paddingTop="10dip"     >        <ProgressBar            android:id="@+id/progressBar1"            style="?android:attr/progressBarStyleLarge"            android:layout_width="wrap_content"            android:layout_height="wrap_content" />        <TextView            android:id="@+id/textView1"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="正在載入..." />        </LinearLayout>    </LinearLayout>

       

         2. 自訂ListView

                   上面我們建立了一個載入進度布局檔案,接著我們要把這個布局檔案添加到ListView中,現在我們建立一個自訂的ListViewPager,自訂的ListViewPager繼承ListViiew,通過addFooterView方法,將負載檔案添加到listview中, 同時要監聽ListView的滾動事件,來載入資料,所以要繼承OnScrollListener介面。

            

package com.example.helloword;import android.R.bool;import android.content.Context;import android.os.Handler;import android.util.AttributeSet;import android.view.LayoutInflater;import android.view.View;import android.widget.AbsListView;import android.widget.ListView;//自訂listviewpublic class ListViewPager extends ListView implements android.widget.AbsListView.OnScrollListener  {View footer;//定義底部viewint lastItem;//最後一個可見的數量int totalItem;//總的數量boolean isload;//是否正在載入IDataInterface datainter;public ListViewPager(Context context) {super(context);InitFooter(context);// TODO Auto-generated constructor stub}     public ListViewPager(Context context, AttributeSet attrs) {        super(context, attrs);        InitFooter(context);    } public ListViewPager(Context context, AttributeSet attrs, int defStyle) {        super(context, attrs, defStyle);        InitFooter(context);    }    //初始化底部布局欄private void InitFooter(Context oontext){LayoutInflater  inflater=LayoutInflater.from(oontext);//添加底部欄footer=inflater.inflate(R.layout.listviewfooter, null);footer.setVisibility(View.GONE);//初始化的時候設定底部不可見this.addFooterView(footer);this.setOnScrollListener(this);}@Overridepublic void onScroll(AbsListView arg0, int arg1, int arg2, int arg3) {// TODO Auto-generated method stub//arg1可見的第一個數     arg2可見的數量      arg3總的數量this.lastItem=arg1+arg2;//最後一個可見的數等於當前第一個可見的數加上可見的數量this.totalItem=arg3;}//載入完成@Overridepublic void onScrollStateChanged(AbsListView arg0, int arg1) {// TODO Auto-generated method stub  滾動狀態等於滾動停止,參數arg1表示滾動狀態if(lastItem==totalItem&&arg1==SCROLL_STATE_IDLE){//最後一個和總的相等說明已經到listview底部if(!isload){isload=true;footer.setVisibility(View.VISIBLE);//設定可見Handler  handler=new Handler();handler.postDelayed(new Runnable(){public void run(){}}, 5000);datainter.LoadData();//載入資料isload=false;//footer.setVisibility(View.GONE);//設定可見}}}public void setInterface(IDataInterface data){datainter=data;}}

         onScroll(AbsListView arg0, int arg1, int arg2, int arg3)有三個重要的參數

             arg1表示當前可見的第一個數

            arg2表示可見的數量

            arg3表示總的數量

       onScrollStateChanged(AbsListView arg0, int arg1)表示滾動狀態變化

       如果滾動狀態結束,載入資料。

   3.自訂ListView綁定資料

                  自訂listview建立後,我們建立一個DEMO來綁定資料,首先建立一個布局檔案和一個activity檔案

                 

<?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" >             <com.example.helloword.ListViewPager        android:id="@+id/lvpager"        android:layout_width="match_parent"        android:layout_height="wrap_content" >    </com.example.helloword.ListViewPager>         </LinearLayout>

 
package com.example.helloword;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import android.app.Activity;import android.os.Bundle;import android.widget.ListView;import android.widget.SimpleAdapter;public class PageListView extends Activity implements  IDataInterface {private ListViewPager lv;      SimpleAdapter adp;//定義適配器     private List<Map<String,Object>> mapList;//定義資料來源        protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.pagerlistview);lv=(ListViewPager)findViewById(R.id.lvpager);lv.setInterface(this);mapList=new ArrayList<Map<String,Object>>();      for(int i=0;i<6;i++)    {        Map<String,Object> map=new HashMap<String,Object>();      map.put("code","編碼:1000"+i);      map.put("name","名稱:Ipad"+i);     map.put("price","價格:"+i);     map.put("model","單位:"+i);      mapList.add(map);      }         adp=new SimpleAdapter(PageListView.this, mapList,R.layout.listdetail, new String[]{"code","name","price","model"}, new int[]{R.id.tvcode,R.id.tvname,R.id.tvprice,R.id.tvmodel});      lv.setAdapter(adp);              }@Overridepublic void LoadData() {// TODO Auto-generated method stubfor(int i=10;i<16;i++)    {        Map<String,Object> map=new HashMap<String,Object>();      map.put("code","編碼:1000"+i);      map.put("name","名稱:Ipad"+i);     map.put("price","價格:"+i);     map.put("model","單位:"+i);      mapList.add(map);      }adp.notifyDataSetChanged();}}


         PageListView中通過介面向自訂LISTVIEW傳遞資料,所以我們要建立一個介面檔案  
package com.example.helloword;public interface IDataInterface { public void LoadData();//載入資料介面}

   demo下載:http://download.csdn.net/detail/zx13525079024/8316547下載

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.