Android自带刷新控件SwipeRefreshLayout扩展,支持ListView下拉刷新

先看图,图片可能有点小,大概看个样子吧。

这里写图片描述

  • 扩展类
public class SwipeRefreshListView extends SwipeRefreshLayout implements AbsListView.OnScrollListener {

    private ListView mListView;
    private View swipeFooterView;
    private TextView tv;
    private ProgressBar pb;
    private onLoadMoreListener mOnLoadMoreListener;

    public SwipeRefreshListView(Context context) {
        this(context, null);
    }

    public SwipeRefreshListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        if (mListView == null) {
            getListView();
        }
    }

    /**
     * 初始化ListView的操作必须放在onLayout中,这样才找得到ListView这个子VIew
     */
    private void getListView() {
        int childCount = getChildCount();
        if (childCount > 0) {
            //这里取第一位是因为系统已经自带了一个circleImageView在第0位,就是下拉出来的那个刷新按钮
            View chileView = this.getChildAt(1);
            if (chileView instanceof ListView) {
                mListView = (ListView) chileView;
                mListView.setOnScrollListener(this);
                initFooterView();
            } else {
                throw new RuntimeException("child view not ListView");
            }
        }
    }

    /**
     * 初始化上拉刷新布局
     */
    private void initFooterView() {
        swipeFooterView = View.inflate(getContext(), R.layout.swipe_footerview, null);
        tv = (TextView) swipeFooterView.findViewById(R.id.tv);
        pb = (ProgressBar) swipeFooterView.findViewById(R.id.pb);
        mListView.addFooterView(swipeFooterView);
    }


    @Override
    public void onScrollStateChanged(AbsListView absListView, int scrollState) {
        if (scrollState == SCROLL_STATE_IDLE && mListView.getLastVisiblePosition() + 1 == mListView.getCount() && null != mOnLoadMoreListener) {
            tv.setVisibility(View.VISIBLE);
            tv.setText("正在加载...");
            pb.setVisibility(View.VISIBLE);
            mOnLoadMoreListener.loadMore();
        }
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
                         int totalItemCount) {

    }

    /**
     * 接口回调
     *
     * @param loadMoreListener
     */
    public void setOnLoadMoreListener(onLoadMoreListener loadMoreListener) {
        this.mOnLoadMoreListener = loadMoreListener;
    }

    /**
     * 加载更多接口
     */
    public interface onLoadMoreListener {
        void loadMore();
    }

    /**
     * 完成加载数据
     */
    public void loadMoreComplete() {
        tv.setText("上拉加载更多...");
        pb.setVisibility(View.GONE);
    }
}
  • 布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.simple.swiperefreshlistview.SwipeRefreshListView
        android:id="@+id/sr_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ListView
            android:id="@+id/lv"
            android:layout_width="match_parent"
            android:layout_height="match_parent"></ListView>
    </com.simple.swiperefreshlistview.SwipeRefreshListView>
</RelativeLayout>
  • MainActivity
public class MainActivity extends AppCompatActivity {

    private ListView lv;
    private List<String> list;
    private SwipeRefreshListView sr_layout;
    private ArrayAdapter adapter;
    private int addHead = 0;
    private int addFooter = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        sr_layout = (SwipeRefreshListView) findViewById(R.id.sr_layout);
        lv = (ListView) findViewById(R.id.lv);
        list = new ArrayList<>();
        for (int i = 0; i < 15; i++) {
            list.add("simple" + i);
        }
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, list);
        lv.setAdapter(adapter);
        sr_layout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                list.add(0, "refresh add item" + addHead);
                addHead++;
                sr_layout.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        adapter.notifyDataSetChanged();
                        sr_layout.setRefreshing(false);
                    }
                }, 3000);
            }
        });
        sr_layout.setOnLoadMoreListener(new SwipeRefreshListView.onLoadMoreListener() {
            @Override
            public void loadMore() {
                sr_layout.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        list.add("loadMore add item" + addFooter);
                        list.add("loadMore add item" + addFooter);
                        list.add("loadMore add item" + addFooter);
                        list.add("loadMore add item" + addFooter);
                        list.add("loadMore add item" + addFooter);
                        addFooter++;
                        adapter.notifyDataSetChanged();
                        sr_layout.loadMoreComplete();
                    }
                }, 3000);
            }
        });
    }

}
  • 底部布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="60dp">

        <TextView
            android:id="@+id/tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="上拉加载更多..."
            android:visibility="visible"
            android:textSize="17sp" />

        <ProgressBar
            android:id="@+id/pb"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:visibility="gone"
            android:layout_toLeftOf="@id/tv" />
    </RelativeLayout>
</RelativeLayout>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值