Android 下拉刷新、上拉加载 SmartRefreshLayout

12 篇文章 0 订阅
11 篇文章 1 订阅

在开发过程中,不可避免的会使用到 RecyclerView 展示数据,很多时候数据很多,需要分页展示,这样,就需要用到下拉刷新、上拉加载功能,以达到友好交互的效果,下面我们就来看看如何简单的实现这个功能!

1、效果展示
1、添加使用的依赖
2、布局  activity_main.xml
3、适配布局   recycler_refresh.xml
3、数据适配器   RefreshAdapter.java
4、功能实现  MainActivity.java

具体实现都在代码中有详细注释。

1、效果展示

下拉刷新                                                                                                            上拉加载


1、添加使用的依赖

build.gradle 加入如下依赖

//recyclerview
implementation 'com.android.support:recyclerview-v7:28.0.0-alpha3'
//SmartRefreshLayout 下拉刷新 上拉加载
implementation 'com.scwang.smartrefresh:SmartRefreshLayout:1.0.4-7'
implementation 'com.scwang.smartrefresh:SmartRefreshHeader:1.0.4-7'
//View 注入框架 替代 findViewById
implementation 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
//gradle-retrolambda配置
implementation 'me.tatarka:gradle-retrolambda:3.7.0'


2、布局  activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <com.scwang.smartrefresh.layout.SmartRefreshLayout
        android:id="@+id/srl_control"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:srlAccentColor="#00000000"
        app:srlPrimaryColor="#00000000"
        app:srlEnablePreviewInEditMode="true">

        <com.scwang.smartrefresh.layout.header.ClassicsHeader
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

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

        <com.scwang.smartrefresh.layout.footer.ClassicsFooter
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

    </com.scwang.smartrefresh.layout.SmartRefreshLayout>

</LinearLayout>


3、适配布局   recycler_refresh.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/group"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:gravity="center"
        android:layout_margin="20dp"
        android:orientation="vertical"
        android:background="#00FFFF">

        <TextView
            android:id="@+id/tv_item"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="item1"
            android:textColor="#000000"
            android:textSize="16dp" />

    </LinearLayout>
    
</LinearLayout>


3、数据适配器   RefreshAdapter.java

package com.example.smartrefresh;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.constraint.Group;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

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

    private Context context;
    public List<String> strList;
    private RefreshInterface refreshInterface;

    public RefreshAdapter(Context context, List<String> strList, RefreshInterface refreshInterface) {
        this.context = context;
        this.strList = strList;
        this.refreshInterface = refreshInterface;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.recycler_refresh, null);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
        viewHolder.group.setOnClickListener(v -> refreshInterface.Refresh(i));
        viewHolder.tvItem.setText(strList.get(i));
    }

    @Override
    public int getItemCount() {
        return strList.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.group)
        LinearLayout group;
        @BindView(R.id.tv_item)
        TextView tvItem;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    //加载数据
    public void loadMore(List<String> strings) {
        strList.addAll(strings);
        notifyDataSetChanged();
    }

    //刷新数据
    public void refreshData(List<String> strings) {
        strList.addAll(0, strings);
        notifyDataSetChanged();
    }

    //点击的回掉,将点击的下标返回
    public interface RefreshInterface{
        void Refresh(int ints);
    }
}


4、功能实现  MainActivity.java

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.rv_refresh)
    RecyclerView rvRefresh;
    @BindView(R.id.srl_control)
    SmartRefreshLayout srlControl;

    //RecyclerView 数据适配器
    private RefreshAdapter refreshAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        recyclerRefresh(Data());
        smartRefresh();

    }

    //适配数据
    public void recyclerRefresh(List<String> Data){
        refreshAdapter = new RefreshAdapter(this, Data, ints -> Toast.makeText(MainActivity.this, Data.get(ints), Toast.LENGTH_SHORT).show());
        rvRefresh.setLayoutManager(new GridLayoutManager(this, 1));
        rvRefresh.setAdapter(refreshAdapter);
        rvRefresh.setNestedScrollingEnabled(false);
    }

    //监听下拉和上拉状态
    public void smartRefresh(){
        //下拉刷新
        srlControl.setOnRefreshListener(refreshlayout -> {
            srlControl.setEnableRefresh(true);//启用刷新
            /**
             * 正常来说,应该在这里加载网络数据
             * 这里我们就使用模拟数据 Data() 来模拟我们刷新出来的数据
             */
            refreshAdapter.strList.clear();
            refreshAdapter.refreshData(Data());
            srlControl.finishRefresh();//结束刷新
        });
        //上拉加载
        srlControl.setOnLoadmoreListener(refreshlayout -> {
            srlControl.setEnableLoadmore(true);//启用加载
            /**
             * 正常来说,应该在这里加载网络数据
             * 这里我们就使用模拟数据 MoreDatas() 来模拟我们加载出来的数据
             */
            refreshAdapter.loadMore(MoreDatas());
            srlControl.finishLoadmore();//结束加载
        });
    }

    //初始数据
    public List<String> Data(){
        List<String> data = new ArrayList<>();
        String temp = "item ";
        for(int i = 0; i < 10; i++) {
            data.add(temp + i);
        }
        return data;
    }

    //刷新得到的数据
    private List<String> MoreDatas() {
        List<String> data = new ArrayList<>();
        String temp = "新加数据";
        for(int i = 0; i < 10; i++) {
            data.add(temp + i);
        }
        return data;
    }
}

GitHub:https://github.com/iscopy/SmartRefresh

 

 

 

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值