zl程序教程

您现在的位置是:首页 >  移动开发

当前栏目

Android 加了一个上拉刷新,下拉加载的布局

Android 一个 加载 布局 刷新 上拉
2023-09-14 09:04:29 时间

在这里插入图片描述
记得是在gridview里面放的

链接在这里

1.在 build.gradle 中添加依赖
可能加载不成功,就去搜索依赖,搜索时不要带版本号,我是将版本号改成2.0.5就可以了的

implementation 'androidx.appcompat:appcompat:1.0.0'                 //必须 1.0.0 以上

implementation  'io.github.scwang90:refresh-layout-kernel:2.0.3'      //核心必须依赖
implementation  'io.github.scwang90:refresh-header-classics:2.0.3'    //经典刷新头
implementation  'io.github.scwang90:refresh-header-radar:2.0.3'       //雷达刷新头
implementation  'io.github.scwang90:refresh-header-falsify:2.0.3'     //虚拟刷新头
implementation  'io.github.scwang90:refresh-header-material:2.0.3'    //谷歌刷新头
implementation  'io.github.scwang90:refresh-header-two-level:2.0.3'   //二级刷新头
implementation  'io.github.scwang90:refresh-footer-ball:2.0.3'        //球脉冲加载
implementation  'io.github.scwang90:refresh-footer-classics:2.0.3'    //经典加载


如果使用 AndroidX 先在 gradle.properties 中添加,两行都不能少噢~

android.useAndroidX=true
android.enableJetifier=true

2.在XML布局文件中添加 SmartRefreshLayout

<?xml version="1.0" encoding="utf-8"?>
<com.scwang.smart.refresh.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">
    <com.scwang.smart.refresh.header.ClassicsHeader
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:overScrollMode="never"
        android:background="#fff" />
    <com.scwang.smart.refresh.footer.ClassicsFooter
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</com.scwang.smart.refresh.layout.SmartRefreshLayout>

3.在 Activity 或者 Fragment 中添加代码

RefreshLayout refreshLayout = (RefreshLayout)findViewById(R.id.refreshLayout);
refreshLayout.setRefreshHeader(new ClassicsHeader(this));
refreshLayout.setRefreshFooter(new ClassicsFooter(this));
refreshLayout.setOnRefreshListener(new OnRefreshListener() {
    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        refreshlayout.finishRefresh(2000/*,false*/);//传入false表示刷新失败
    }
});
refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
    @Override
    public void onLoadMore(RefreshLayout refreshlayout) {
        refreshlayout.finishLoadMore(2000/*,false*/);//传入false表示加载失败
    }
});

如果是在fragment,就将this改成requireContext()

V p n用弄子里,不然有点难加载github

下面是首页的代码

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

    <com.example.app5.view.ActionBarView
        android:id="@+id/action_bar"
        android:layout_width="match_parent"
        android:title="@string/recommend"
        android:layout_height="wrap_content" />

    <ImageView
        android:id="@+id/iv_firstpage"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scaleType="fitXY"
        android:background="@color/transparent"
        android:adjustViewBounds="true"
        android:src="@mipmap/firstpage" />

    <GridView
        android:id="@+id/gview"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:horizontalSpacing="10dp"
        android:scrollbars="none"
        android:verticalSpacing="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:numColumns="2" />

</LinearLayout>

在这里插入图片描述