基本的原理和我的上一篇随笔“”差不多,代码里面有注释,这里就不废话了,直接上代码。
自定义的分页显示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 @Override32 public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {33 this.lastVisibleItemIndex = firstVisibleItem + visibleItemCount;34 this.totalItemCount = totalItemCount;35 }36 37 @Override38 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的代码如下:
14 5 6 15 16 28 2920 21 27
主页面布局activity_main.xml代码如下:
15 6 10 11
主界面MainActivity.java代码如下:
1 public class MainActivity extends Activity implements ListViewLoadListener { 2 private PagedListView listView; 3 private ArrayAdapterlistAdapter; 4 private List 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 ();17 for (int i = 0; i < 10; i++) {18 listData.add("This is an initial data.");19 }20 listAdapter = new ArrayAdapter (MainActivity.this, android.R.layout.simple_expandable_list_item_1, listData);21 listView.setAdapter(listAdapter);22 }23 24 @Override25 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 }