Android之分頁加載數據


基本的原理和我的上一篇隨筆“Android之下拉刷新ListView”差不多,代碼里面有注釋,這里就不廢話了,直接上代碼。

自定義的分頁顯示ListView——PagedListView.java代碼如下:

 1 public class PagedListView extends ListView implements OnScrollListener {
 2     private View footer; // 底部布局
 3     private LayoutInflater inflater;
 4     private int totalItemCount; // 總的Item的數量
 5     private int lastVisibleItemIndex; // 最后一個可見的Item的索引
 6     private boolean isLoading; // 是否正在加載
 7     private ListViewLoadListener listener; // 加載更多數據的回掉接口
 8 
 9     public PagedListView(Context context) {
10         this(context, null);
11     }
12 
13     public PagedListView(Context context, AttributeSet attrs) {
14         this(context, attrs, 0);
15     }
16 
17     public PagedListView(Context context, AttributeSet attrs, int defStyleAttr) {
18         super(context, attrs, defStyleAttr);
19         initView(context);
20     }
21 
22     // 加載底部布局到ListView中
23     private void initView(Context context) {
24         inflater = LayoutInflater.from(context);
25         footer = inflater.inflate(R.layout.sideworks_layout_footer, null);
26         footer.findViewById(R.id.control_layout_footer).setVisibility(View.GONE);
27         this.addFooterView(footer);
28         this.setOnScrollListener(this);
29     }
30 
31     @Override
32     public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
33         this.lastVisibleItemIndex = firstVisibleItem + visibleItemCount;
34         this.totalItemCount = totalItemCount;
35     }
36 
37     @Override
38     public void onScrollStateChanged(AbsListView view, int scrollState) {
39         if (totalItemCount == lastVisibleItemIndex && scrollState == SCROLL_STATE_IDLE) {
40             if (!isLoading) {
41                 isLoading = true;
42                 footer.findViewById(R.id.control_layout_footer).setVisibility(View.VISIBLE);
43                 listener.loadData(); // 調用接口中的回調方法進行數據更新
44             }
45         }
46     }
47 
48     // 加載更多數據的回掉接口
49     public interface ListViewLoadListener {
50         public void loadData();
51     }
52 
53     public void setListViewLoadInterface(ListViewLoadListener listener) {
54         this.listener = listener;
55     }
56 
57     // 數據加載完成之后,隱藏footer布局
58     public void onLoadComplete() {
59         isLoading = false;
60         footer.findViewById(R.id.control_layout_footer).setVisibility(View.GONE);
61     }
62 }

底部布局sideworks_layout_footer.xml的代碼如下:

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     android:layout_width="match_parent"
 3     android:layout_height="wrap_content" >
 4 
 5     <!-- 這里必須要套一層LinearLayout,不然在代碼中設置Visibility為Gone時底部總是會有一部分空白區域 -->
 6     <LinearLayout
 7         android:id="@+id/control_layout_footer"
 8         android:layout_width="match_parent"
 9         android:layout_height="wrap_content"
10         android:background="#e7e7e7"
11         android:gravity="center"
12         android:orientation="horizontal"
13         android:paddingBottom="10.0dip"
14         android:paddingTop="10.0dip" >
15 
16         <ProgressBar
17             style="?android:attr/progressBarStyleSmall"
18             android:layout_width="wrap_content"
19             android:layout_height="wrap_content" />
20 
21         <TextView
22             android:layout_width="wrap_content"
23             android:layout_height="wrap_content"
24             android:layout_marginLeft="15.0dip"
25             android:text="@string/str_footer_tip"
26             android:textColor="#777777" />
27     </LinearLayout>
28 
29 </LinearLayout>

主頁面布局activity_main.xml代碼如下:

 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent" >
 5 
 6     <com.view.PagedListView
 7         android:id="@+id/control_main_listview"
 8         android:layout_width="match_parent"
 9         android:layout_height="match_parent" />
10 
11 </RelativeLayout>

主界面MainActivity.java代碼如下:

 1 public class MainActivity extends Activity implements ListViewLoadListener {
 2     private PagedListView listView;
 3     private ArrayAdapter<String> listAdapter;
 4     private List<String> listData;
 5 
 6     @Override
 7     protected void onCreate(Bundle savedInstanceState) {
 8         super.onCreate(savedInstanceState);
 9         setContentView(R.layout.activity_main);
10         initView();
11     }
12 
13     private void initView() {
14         listView = (PagedListView) findViewById(R.id.control_main_listview);
15         listView.setListViewLoadInterface(this);
16         listData = new ArrayList<String>();
17         for (int i = 0; i < 10; i++) {
18             listData.add("This is an initial data.");
19         }
20         listAdapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_expandable_list_item_1, listData);
21         listView.setAdapter(listAdapter);
22     }
23 
24     @Override
25     public void loadData() {
26         new Handler().postDelayed(new Runnable() {
27             public void run() {
28                 // 增加兩條數據並通知ListView進行數據的更新
29                 for (int i = 0; i < 2; i++) {
30                     listData.add("This is a new data.");
31                 }
32                 listAdapter.notifyDataSetChanged();
33                 listView.onLoadComplete();
34             }
35         }, 2000);
36     }
37 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM