RecyclerView的上拉刷新与下拉加载

1.布局(用SwipeRefreshLayout实现下拉刷新)

<android.support.v4.widget.SwipeRefreshLayout
    android:id="@+id/swipeRefreshLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</android.support.v4.widget.SwipeRefreshLayout>

2.RecyclerView的itemView布局

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:layout_marginTop="10dp">

    <ImageView
        android:id="@+id/item_img"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:scaleType="fitXY"
        android:src="@mipmap/panda" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@id/item_img"
        android:layout_margin="1dp"
        android:background="#8c282828"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/item_tv"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:layout_weight="1"
            android:text="大熊猫"
            android:textColor="#ffffff"
            android:textSize="15sp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:text="国宝"
            android:textColor="#de3636"
            android:textSize="15sp" />
    </LinearLayout>

</RelativeLayout>

这里写图片描述

3.RecyclerView数据展示,见RecyclerView的简单使用

4.下拉刷新要借助SwipeRefreshLayout

// 下拉刷新(SwipeRefreshLayou设置刷新的监听)
refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
    @Override
    public void onRefresh() {
        // 下拉刷新,清空数据源,重新获取最新数据

    }
});

注意:刷新完毕要关闭刷新动画

// 数据已经更新,关闭刷新动画
// 如果不关闭会一直转圈
if (refreshLayout.isRefreshing()) {
    refreshLayout.setRefreshing(false);
}

这里写图片描述

5.上拉加载需要添加OnScrollListener监听事件

// 上拉加载需要添加滚动监听事件
// 这里实现LinearLayoutManager并且是VERTAL方向的监听
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
    @Override
    public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
        super.onScrollStateChanged(recyclerView, newState);
        // 满足条件 执行上拉加载数据
        if (newState == RecyclerView.SCROLL_STATE_IDLE && isBottom) {
        // 填充下一页数据...           
        // 注意:加载完 要置为false
        isBottom = false;
    }
}

    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        // dy>0 代表向下滚动
        super.onScrolled(recyclerView, dx, dy);
        int lastVisibleItemPosition = manager.findLastVisibleItemPosition();
        int itemCount = manager.getItemCount();
        // 如果最后一个可见的View的position 等于 itemCount-1 代表滚动到底部
        isBottom = itemCount-1 == lastVisibleItemPosition && itemCount>0;
    }
});

这里写图片描述 这里写图片描述

代码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值