Android-PullToRefresh自动加载更多

Android-PullToRefresh(github地址)是个十分方便的下拉刷新库(也有上拉加载的功能),各个app中常见到滑到底部自动加载的功能,这里提供一个简单的实现思路:

    <com.handmark.pulltorefresh.library.PullToRefreshListView    
        xmlns:ptr="http://schemas.android.com/apk/res-auto"
        android:id="@+id/id_pull_to_refresh_listview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:cacheColorHint="@color/gray_light"
        android:divider="@color/gray_light"
        android:dividerHeight="@dimen/spacing_normal"
        android:fadingEdge="none"
        android:fadingEdgeLength="0dp"
        android:scrollingCache="false"
        ptr:ptrAnimationStyle="flip"
        ptr:ptrHeaderTextAppearance="?android:attr/textAppearanceMedium" />
mRefreshListView=(PullToRefreshListView)findViewById(R.id.id_pull_to_refresh_listview);
mRefreshListView.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
footer = LayoutInflater.from(getActivity()).inflate(R.layout.footer,null,false);
mRefreshListView.getRefreshableView().addFooterView(footer);
//该回调会在滑到最后一个可见view的时候会被调用,此时我们让添加的footer可见
mRefreshListView.setOnLastItemVisibleListener(new PullToRefreshBase.OnLastItemVisibleListener() {
            @Override
            public void onLastItemVisible() {
                footer.setVisibility(View.VISIBLE);
                //后面执行加载数据的请求,完成后调用footer.setVisibility(View.GONE)
            }
        });
<?xml version="1.0" encoding="utf-8"?>

<!--R.layout.footer -->
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:padding="5dp"
    android:text="正在加载中...."
    android:textSize="@dimen/common_text_size" />

注意:如果footer.setVisibility(View.GONE),扔占据空间的话给他设置一个父view

思路比较简单,希望对家有帮助

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值