ListView 實現像Android Market那樣 分頁載入

來源:互聯網
上載者:User
 最近幾天研究ListView實現分頁載入和滾動式載入,發現可以用listView的OnScroll方法來實現,直接上代碼ListViewScroll.javaview plaincopy to clipboardprint?
package zy.lucifer.ListViewScroll; 
import android.app.Activity; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.Gravity; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.AbsListView; 
import android.widget.BaseAdapter; 
import android.widget.LinearLayout; 
import android.widget.ListView; 
import android.widget.ProgressBar; 
import android.widget.TextView; 
import android.widget.Toast; 
import android.widget.AbsListView.OnScrollListener; 
import android.widget.LinearLayout.LayoutParams; 
public class ListViewScroll extends Activity { 
    /** Called when the activity is first created. */ 
    private LayoutParams mLayoutParams = new LinearLayout.LayoutParams( 
            LinearLayout.LayoutParams.WRAP_CONTENT, 
            LinearLayout.LayoutParams.WRAP_CONTENT); 
    /**
     * 設定布局顯示目標最大化屬性
     */ 
    private LayoutParams FFlayoutParams = new LinearLayout.LayoutParams( 
            LinearLayout.LayoutParams.FILL_PARENT, 
            LinearLayout.LayoutParams.FILL_PARENT); 
    ListView listView ; 
    private int lastItem = 0; 
    LinearLayout loadingLayout; 
    private listViewAdapter adapter; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
        listView = (ListView) findViewById(R.id.myList); 
         
        Log.i("test", "onCreate(Bundle savedInstanceState)>>>>>>>>>>>>>>>"); 
        // 線性布局 
        LinearLayout layout = new LinearLayout(this); 
        // 設定布局 水平方向 
        layout.setOrientation(LinearLayout.HORIZONTAL); 
        // 進度條 
        ProgressBar progressBar = new ProgressBar(this); 
        // 進度條顯示位置 
        progressBar.setPadding(0, 0, 15, 0); 
        // 把進度條加入到layout中 
        layout.addView(progressBar, mLayoutParams); 
        // 常值內容 
        TextView textView = new TextView(this); 
        textView.setText("載入中..."); 
        textView.setGravity(Gravity.CENTER_VERTICAL); 
        // 把文本加入到layout中 
        layout.addView(textView, FFlayoutParams); 
        // 設定layout的重力方向,即對齊是 
        layout.setGravity(Gravity.CENTER); 
        // 設定ListView的頁尾layout 
        loadingLayout = new LinearLayout(this); 
        loadingLayout.addView(layout, mLayoutParams); 
        loadingLayout.setGravity(Gravity.CENTER); 
        listView.addFooterView(loadingLayout); 
        adapter = new listViewAdapter(); 
        listView.setAdapter(adapter); 
        listView.setOnScrollListener(new OnScrollListener() { 
  
            @Override 
            public void onScroll(AbsListView view, int firstVisibleItem, 
                    int visibleItemCount, int totalItemCount) { 
                // TODO Auto-generated method stub 
                Log.i("test" , "Scroll>>>first: " + firstVisibleItem + ", visible: " + visibleItemCount + ", total: " + totalItemCount); 
                lastItem = firstVisibleItem + visibleItemCount - 1; 
                Log.i("test" , "Scroll>>>lastItem:" + lastItem); 
                //顯示50條ListItem,即0-49,因為onScroll是在“滑動”執行過之後才觸發,所以用adapter.count<=41作條件 
                int scrolllength=101; 
                if (adapter.count<=scrolllength) { 
                    if (firstVisibleItem+visibleItemCount==totalItemCount) { 
                        adapter.count += 10; 
                        adapter.notifyDataSetChanged(); 
                        listView.setSelection(lastItem); 
                        int currentPage=adapter.count/10; 
                        Toast.makeText(getApplicationContext(), "第"+currentPage+"頁", Toast.LENGTH_LONG).show(); 
                    } 
                } 
                else { 
                     listView.removeFooterView(loadingLayout);  
                } 
            } 
            @Override 
            public void onScrollStateChanged(AbsListView view, int scrollState) { 
                // TODO Auto-generated method stub 
            } 
        }); 
    } 
    class listViewAdapter extends BaseAdapter { 
        int count = 10; /* starting amount */ 
        public int getCount() { 
            return count; 
        } 
        public Object getItem(int pos) { 
            return pos; 
        } 
        public long getItemId(int pos) { 
            return pos; 
        } 
        public View getView(int pos, View v, ViewGroup p) { 
            Log.i("test", "getView>>>pos:" + pos); 
            TextView view; 
            if (v == null) { 
                view = new TextView(ListViewScroll.this); 
            } else { 
                view = (TextView) v; 
            } 
            view.setText("ListItem " + pos); 
            view.setTextSize(20f); 
            view.setGravity(Gravity.CENTER); 
            view.setHeight(60); 
            return view; 
             
        } 
    } 

main.xml

view plaincopy to clipboardprint?
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
<ListView android:cacheColorHint="#00000000" android:id="@+id/myList" 
android:layout_width="fill_parent" android:layout_height="fill_parent" 
        > 
    </ListView> 
</LinearLayout> 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.