RecyclerView实现左滑加载

本文介绍如何在车载项目的横屏消息中心,利用RecyclerView实现用户左滑加载更多消息的功能。通过添加footview布局,创建新的LoadMoreAdapter,并设置滑动监听器,在倒数第二个item时触发加载更多操作。原有adapter保持不变,简化了实现过程。

背景

车载项目中,默认横屏,消息中心通过RecylerView展示各类消息,现在要求实现用户左滑获取更多消息并展示,实现方案是增加一个footerview布局,在原来的adpater上包裹一个新的adapter,监听用户滑动方向,这样做的好处是不需要修改原来的adapter,也不需要知道原来的adapter是怎么实现的。左滑和上滑只是方向的不同。

footview布局

定义一个footview.xml布局,里面是个loading的图片。

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    >


    <ImageView
        android:id="@+id/loading_img"
        android:layout_width="60dp"
        android:layout_height="60dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/ic_loading" />
</androidx.constraintlayout.widget.ConstraintLayout>

FootViewHolder

public class FootViewHolder extends BaseItemAdapter.BaseViewHolder {
   
   
    @Getter
    private ImageView loadingImg;

    public FootViewHolder(View itemView) {
   
   
        super(itemView);
        loadingImg = (ImageView) itemView.findViewById(R.id.loading_img);
    }
}

新的adapter

定义一个LoadMoreAdapter.java,构造方法中传入原来的adapter。

public class LoadMoreAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
   
   

    private RecyclerView.Adapter adapter;

    // 普通布局
    private static final int TYPE_ITEM = 1;
    // 脚布局
    private static final int TYPE_FOOTER = 2;
    // 当前加载状态
    private int loadState = 2;
    // 正在加载
    public static final int LOADING = 1;
    // 加载完成
    public static final int LOADING_COMPLETE = 2;
    // 左滑加载
    public static final int PRE_LOAD = 
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值