转自:http://blog.****.net/jdfkldjlkjdl/article/details/70229465
这次使用的是系统的SwipeRefreshLayout实现下拉刷新,和设置ListView的滑动监听判断是否滑动到最底部然后加载更多;如果想了解用另一种方式实现这个功能,请移步http://blog.****.net/jdfkldjlkjdl/article/details/51277941
其中,SwipeRefreshLayout的几个方法功能如下:
1、setOnRefreshListener():设置手势滑动监听器。
2、setProgressBackgroundColor():设置进度圈的背景色。
3、setColorSchemeResources():设置进度动画的颜色。
4、setRefreshing():设置组件的刷洗状态。
5、setSize():设置进度圈的大小
下面说一下实现步骤:
1.添加布局文件
- <android.support.v4.widget.SwipeRefreshLayout
- android:id="@+id/mSwipeRefreshLayout"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <ListView
- android:id="@+id/mListView"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:divider="#cccccc"
- android:dividerHeight="1px"/>
- </android.support.v4.widget.SwipeRefreshLayout>
2.添加加载更多的布局文件load_more.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/ll_load_more"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="vertical">
- <LinearLayout
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center_horizontal"
- android:layout_margin="10dip"
- android:gravity="center_vertical"
- android:orientation="horizontal">
- <ProgressBar
- android:layout_width="30dp"
- android:layout_height="30dp"
- android:layout_gravity="center"
- />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginLeft="10dip"
- android:text="加载更多"
- android:textColor="#bbb"
- android:textSize="12sp"/>
- </LinearLayout>
- </LinearLayout>
3.绑定下拉刷新事件
- //设置手势监听
- mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
- @Override
- public void onRefresh() {
- mHandler.sendEmptyMessageDelayed(REFRESH, 2000);
- }
- });
4.绑定上拉加载更多事件
- //给listview设置一个滑动的监听
- mListView.setOnScrollListener(new AbsListView.OnScrollListener() {
- int visibleLastIndex = 0; //最后的可视项索引
- int visibleItemCount; // 当前窗口可见项总数
- //当滑动状态发生改变的时候执行
- public void onScrollStateChanged(AbsListView view, int scrollState) {
- switch (scrollState) {
- //当不滚动的时候
- case AbsListView.OnScrollListener.SCROLL_STATE_IDLE:
- int itemsLastIndex = adapter.getCount() - 1; //数据集最后一项的索引
- int lastIndex = itemsLastIndex + 1; //加上底部的loadMoreView项
- //判断是否是最底部
- //if (view.getLastVisiblePosition() == (view.getCount()) - 1) { //或者
- if (visibleLastIndex == lastIndex) {
- loadMoreView.setVisibility(View.VISIBLE);
- mHandler.postDelayed(new Runnable() {
- @Override
- public void run() {
- //加载网络数据
- Message msg = new Message();
- msg.what = LOADMORE;
- msg.arg1 = visibleLastIndex - visibleItemCount + 1;
- mHandler.sendMessage(msg);
- }
- }, 2000);
- }
- break;
- }
- }
- //正在滑动的时候执行
- public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
- this.visibleItemCount = visibleItemCount;
- visibleLastIndex = firstVisibleItem + visibleItemCount - 1;
- }
- });
完整的activity代码如下:
- package demo.xzy.qh.com.listviewpulltorefreshandloadmore;
- import android.app.Activity;
- import android.graphics.Color;
- import android.os.Bundle;
- import android.os.Handler;
- import android.os.Message;
- import android.support.v4.widget.SwipeRefreshLayout;
- import android.view.View;
- import android.widget.AbsListView;
- import android.widget.ArrayAdapter;
- import android.widget.ListView;
- import java.util.ArrayList;
- import java.util.List;
- /**
- * ListView下拉刷新,上拉加载demo
- */
- public class MainActivity extends Activity {
- private SwipeRefreshLayout mSwipeRefreshLayout;
- private ListView mListView;
- private List<String> data = new ArrayList<>();
- private ArrayAdapter<String> adapter;
- private static final int REFRESH = 0x01;
- private static final int LOADMORE = 0x02;
- private View loadMoreView;
- private Handler mHandler = new Handler() {
- @Override
- public void handleMessage(Message msg) {
- super.handleMessage(msg);
- switch (msg.what) {
- case REFRESH:
- data.add(0, "刷新得到的数据");
- adapter.notifyDataSetChanged();
- mSwipeRefreshLayout.setRefreshing(false);
- break;
- case LOADMORE:
- for (int x = 0; x < 5; x++) {
- data.add(data.size(), "aaaaaa" + x);
- }
- adapter.notifyDataSetChanged(); //数据集变化后,通知adapter
- int position = msg.arg1;
- mListView.setSelection(position); //设置选中项
- loadMoreView.setVisibility(View.GONE);
- break;
- }
- }
- };
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- for (int i = 0; i < 6; i++) {
- data.add("测试数据" + i);
- }
- initView();
- }
- private void initView() {
- mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.mSwipeRefreshLayout);
- mListView = (ListView) findViewById(R.id.mListView);
- loadMoreView = getLayoutInflater().inflate(R.layout.load_more, null);
- loadMoreView.setVisibility(View.GONE);
- mListView.addFooterView(loadMoreView);
- mListView.setFooterDividersEnabled(false);
- //设置进度圈的大小;(这里面只有两个值SwipeRefreshLayout.LARGE和DEFAULT,后者是默认效果)
- mSwipeRefreshLayout.setSize(SwipeRefreshLayout.DEFAULT);
- //设置进度圈的背景色。这里随便给他设置了一个颜色:浅绿色
- mSwipeRefreshLayout.setProgressBackgroundColorSchemeColor(Color.CYAN);
- //设置进度动画的颜色。这里面最多可以指定四个颜色,我这也是随机设置的,大家知道怎么用就可以了
- mSwipeRefreshLayout.setColorSchemeResources(android.R.color.holo_orange_dark
- , android.R.color.holo_blue_dark
- , android.R.color.holo_red_dark
- , android.R.color.widget_edittext_dark);
- adapter = new ArrayAdapter<>(MainActivity.this, android.R.layout.simple_list_item_1, data);
- mListView.setAdapter(adapter);
- //设置手势监听
- mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
- @Override
- public void onRefresh() {
- mHandler.sendEmptyMessageDelayed(REFRESH, 2000);
- }
- });
- //给listview设置一个滑动的监听
- mListView.setOnScrollListener(new AbsListView.OnScrollListener() {
- int visibleLastIndex = 0; //最后的可视项索引
- int visibleItemCount; // 当前窗口可见项总数
- //当滑动状态发生改变的时候执行
- public void onScrollStateChanged(AbsListView view, int scrollState) {
- switch (scrollState) {
- //当不滚动的时候
- case AbsListView.OnScrollListener.SCROLL_STATE_IDLE:
- int itemsLastIndex = adapter.getCount() - 1; //数据集最后一项的索引
- int lastIndex = itemsLastIndex + 1; //加上底部的loadMoreView项
- //判断是否是最底部
- //if (view.getLastVisiblePosition() == (view.getCount()) - 1) { //或者
- if (visibleLastIndex == lastIndex) {
- loadMoreView.setVisibility(View.VISIBLE);
- mHandler.postDelayed(new Runnable() {
- @Override
- public void run() {
- //加载网络数据
- Message msg = new Message();
- msg.what = LOADMORE;
- msg.arg1 = visibleLastIndex - visibleItemCount + 1;
- mHandler.sendMessage(msg);
- }
- }, 2000);
- }
- break;
- }
- }
- //正在滑动的时候执行
- public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
- this.visibleItemCount = visibleItemCount;
- visibleLastIndex = firstVisibleItem + visibleItemCount - 1;
- }
- });
- }
- }
整个demo很简单,就两个布局文件和一个activity。
至此,标题中提到的功能已经实现。欢迎留言指正。