Android 数据的上拉加载与下拉刷新?

导读:本篇文章讲解 Android 数据的上拉加载与下拉刷新?,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

效果图:

Android 数据的上拉加载与下拉刷新?    Android 数据的上拉加载与下拉刷新?

1.在项目build中添加引用

//数据加载与刷新
implementation 'com.scwang.smartrefresh:SmartRefreshLayout:1.1.0'
implementation 'com.scwang.smartrefresh:SmartRefreshHeader:1.0.5.1'//没有使用特殊Header,可以不加这行

2.xml布局

(1).布局activity_main:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:orientation="vertical">

    <com.scwang.smartrefresh.layout.SmartRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/refreshLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/tv_post"
        android:layout_marginTop="30px">

        <!--srlAccentColor 修改字体颜色 srlPrimaryColor 修改拉伸多余地方颜色的主题-->
        <!--srlEnablePreviewInEditMode 可以开启和关闭预览功能-->
        <com.scwang.smartrefresh.layout.header.ClassicsHeader
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#444444"
            app:srlAccentColor="@color/colorAccent"
            app:srlPrimaryColor="@color/spinner_text_color" />

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/rvRegisterList"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
            tools:itemCount="10"
            tools:listitem="@layout/item_recycleview" />

        <!--        <pl.droidsonroids.gif.GifImageView-->
        <!--            android:layout_width="match_parent"-->
        <!--            android:layout_height="wrap_content"-->
        <!--            android:scaleType="centerCrop"-->
        <!--            android:src="@mipmap/gif_header_repast"/>-->

        <!--srlAccentColor 修改字体颜色 srlPrimaryColor 修改拉伸多余地方颜色的主题-->
        <!--srlEnablePreviewInEditMode 可以开启和关闭预览功能-->
        <com.scwang.smartrefresh.layout.footer.ClassicsFooter
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#444444"
            app:srlAccentColor="@color/white"
            app:srlPrimaryColor="@color/colorPrimaryDark"/>



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

</LinearLayout>

(2).布局item_recycleview


<?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="wrap_content"
    android:orientation="vertical"
    android:layout_margin="20dp">

    <TextView
        android:id="@+id/tv_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minHeight="46dp"
        android:textSize="25sp"
        android:textColor="@color/black"
        android:gravity="center"
        android:text="伙食费对恢复is恢复is"/>

</LinearLayout>

3.Activity中调用

package com.ruidde.pictureselectordemo;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.RecyclerView;

import com.ruidde.pictureselectordemo.adapter.CourseListAdapter;
import com.ruidde.pictureselectordemo.adapter.CourseListRespone;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.footer.BallPulseFooter;
import com.scwang.smartrefresh.layout.header.BezierRadarHeader;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

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

/**
 * 方法一 :在Application中设置全局的Header 和 Footer设置的Header和Footer的优先级是最低的,如果同时还使用了方法二、三,将会被其他方法取代
 * 方法二 : XML设置的Header和Footer的优先级是中等的,会被方法三覆盖
 * 方法三 : Java代码设置
 * */
public class MainActivity extends FragmentActivity {
    private Context mContext;
    private List<CourseListRespone> mDatas = new ArrayList<>();
    private RecyclerView rvRegisterList;
    private SmartRefreshLayout refreshLayout;
    private CourseListAdapter courseListAdapter ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mContext = this;
        initView();
        initData();
    }

    public void initView(){
        rvRegisterList = findViewById(R.id.rvRegisterList);
        refreshLayout = findViewById(R.id.refreshLayout);

        courseListAdapter = new CourseListAdapter(mContext ,mDatas);
        rvRegisterList.setAdapter(courseListAdapter);
    }

    private void initData() {

        /**
         * 下拉刷新
         * */
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                Log.e("++++++","下拉刷新");
                refreshLayout.finishRefresh(3000);  //3秒取消刷新
//                currentPage = 1;
//                responseList.clear();
//                getBooksByBookType(currentPage ,bookId); //请求数据
            }
        });

        /**
         * 上拉加载
         * */
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {
                Log.e("++++++","上拉加载");
                refreshLayout.finishLoadMore(3000); //3秒取消加载
//                currentPage += 1;
//                if (currentPage <= totalPage) {
//                    getBooksByBookType(currentPage ,bookId);
//                } else {
//                    refreshlayout.finishLoadMore(1000);
//                    Toast.makeText(context,"加载完成",Toast.LENGTH_LONG).show();
//                }
            }
        });

        //设置 Header 为 贝塞尔雷达 样式
        refreshLayout.setRefreshHeader(new BezierRadarHeader(mContext).setEnableHorizontalDrag(true));
        //设置 Footer 为 球脉冲 样式
        refreshLayout.setRefreshFooter(new BallPulseFooter(mContext).setSpinnerStyle(SpinnerStyle.FixedBehind));

        /**
         *  if (currentPage == 1) { //结束下拉刷新
         *             refreshLayout.finishRefresh();
         *         } else { //结束上拉加载
         *             refreshLayout.finishLoadMore();
         *         }
         * */
    }

}

 

4.其他的一些关于上拉加载下拉刷新的文章:https://segmentfault.com/a/1190000010066071

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/119205.html

(0)
seven_的头像seven_bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!