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 }