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

来源:互联网 发布:国际市场占有率数据 编辑:程序博客网 时间:2024/06/15 11:45

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

这里写图片描述

  • 扩展类
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>
0 0
原创粉丝点击