上拉刷新的实现

1.效果图

在这里插入图片描述

2.步骤

1.依赖

//刷新
    implementation 'com.lcodecorex:tkrefreshlayout:1.0.7'

2.xml

添加这个控件
在这里插入图片描述
全部xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView
    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"
    tools:context=".ui.fragment.BlankFragment">
    <com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/Trl_blank2_random">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:background="#FFF9F9F9"
            tools:context=".ui.fragment.BlankFragment">
            <LinearLayout
                android:visibility="gone"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
                <LinearLayout
                    android:layout_marginHorizontal="10dp"
                    app:layout_constraintEnd_toStartOf="@+id/linearLayout3"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@+id/cv_title"
                    style="@style/ll_fg_other_voice_title">

                    <com.bytedance.sdk.openadsdk.core.widget.TTRoundRectImageView
                        android:id="@+id/iv1"
                        android:layout_width="50dp"
                        android:layout_height="50dp"
                        android:src="@drawable/shape_blue" />

                    <TextView
                        android:text="排行版1"
                        style="@style/tv_fg_other_voice_title" />
                </LinearLayout>

                <LinearLayout
                    android:layout_marginHorizontal="10dp"
                    style="@style/ll_fg_other_voice_title"
                    app:layout_constraintBottom_toBottomOf="@+id/linearLayout2"
                    app:layout_constraintEnd_toStartOf="@+id/linearLayout4"
                    app:layout_constraintStart_toEndOf="@+id/linearLayout2"
                    app:layout_constraintTop_toTopOf="@+id/linearLayout2">

                    <com.bytedance.sdk.openadsdk.core.widget.TTRoundRectImageView
                        android:id="@+id/iv2"
                        android:layout_width="50dp"
                        android:layout_height="50dp"
                        android:src="@drawable/shape_blue" />

                    <TextView
                        style="@style/tv_fg_other_voice_title"
                        android:text="排行榜2" />
                </LinearLayout>

                <LinearLayout
                    android:layout_marginHorizontal="10dp"
                    style="@style/ll_fg_other_voice_title"
                    app:layout_constraintBottom_toBottomOf="@+id/linearLayout3"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toEndOf="@+id/linearLayout3"
                    app:layout_constraintTop_toTopOf="@+id/linearLayout3">

                    <com.bytedance.sdk.openadsdk.core.widget.TTRoundRectImageView
                        android:id="@+id/iv_3"
                        android:layout_width="50dp"
                        android:layout_height="50dp"
                        android:src="@drawable/shape_blue" />

                    <TextView
                        android:id="@+id/tv_3"
                        style="@style/tv_fg_other_voice_title"
                        android:text="排行榜3" />
                </LinearLayout>
            </LinearLayout>
            <TextView
                android:textSize="23sp"
                android:id="@+id/textView3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="16dp"
                android:layout_marginLeft="16dp"
                android:text="壁纸分类"
                android:textStyle="bold"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/lL_double" />

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/rv_blank2_content"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="16dp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/textView3" />
            <TextView
                android:textSize="23sp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                android:layout_marginLeft="16dp"
                android:text="随机壁纸"
                android:textStyle="bold"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/lL_double" />
            <!--随机-->
            <androidx.recyclerview.widget.RecyclerView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/rv_blank2_random"/>
            <!--<LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:gravity="center">

                <Button
                    android:layout_weight="1"
                    android:text="跳转到"
                    android:background="@drawable/shape_blue"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/bt_jump"/>
                <EditText
                    android:gravity="center"
                    android:hint="页"
                    android:inputType="number"
                    android:maxLength="2"
                    android:layout_width="50dp"
                    android:layout_height="50dp"
                    android:id="@+id/et_pager"/>
                &lt;!&ndash;上一页&ndash;&gt;
                <com.deadline.statebutton.StateButton
                    android:layout_weight="1"
                    android:text="上一页"
                    android:id="@+id/bt_previous"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintLeft_toLeftOf="parent"
                    app:layout_constraintRight_toRightOf="parent"
                    app:layout_constraintTop_toTopOf="parent"
                    android:layout_gravity="center_horizontal"

                    app:radius="5dp"
                    app:round="true"
                    app:animationDuration="200"

                    app:strokeDashGap="2dp"
                    app:strokeDashWidth="5dp"
                    app:normalStrokeWidth="2dp"
                    app:pressedStrokeWidth="2dp"
                    app:unableStrokeWidth="2dp"

                    app:normalStrokeColor="@android:color/white"
                    app:pressedStrokeColor="@android:color/white"
                    app:unableStrokeColor="@android:color/white"

                    app:normalTextColor="@android:color/white"
                    app:pressedTextColor="@android:color/white"
                    app:unableTextColor="@android:color/white"

                    app:normalBackgroundColor="@color/colorPrimaryDark"
                    app:pressedBackgroundColor="@color/colorPrimaryDark"
                    app:unableBackgroundColor="@color/colorPrimaryDark"/>
                <TextView
                    android:id="@+id/tv_current_page"
                    android:gravity="center"
                    android:layout_width="40dp"
                    android:layout_height="wrap_content"
                    android:text="1"/>
                <com.deadline.statebutton.StateButton
                    android:layout_weight="1"
                    android:text="下一页"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/bt_next"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintLeft_toLeftOf="parent"
                    app:layout_constraintRight_toRightOf="parent"
                    app:layout_constraintTop_toTopOf="parent"
                    android:layout_gravity="center_horizontal"

                    app:radius="5dp"
                    app:round="true"
                    app:animationDuration="200"

                    app:strokeDashGap="2dp"
                    app:strokeDashWidth="5dp"
                    app:normalStrokeWidth="2dp"
                    app:pressedStrokeWidth="2dp"
                    app:unableStrokeWidth="2dp"

                    app:normalStrokeColor="@android:color/white"
                    app:pressedStrokeColor="@android:color/white"
                    app:unableStrokeColor="@android:color/white"

                    app:normalTextColor="@android:color/white"
                    app:pressedTextColor="@android:color/white"
                    app:unableTextColor="@android:color/white"

                    app:normalBackgroundColor="@color/colorPrimaryDark"
                    app:pressedBackgroundColor="@color/colorPrimaryDark"
                    app:unableBackgroundColor="@color/colorPrimaryDark"/>
            </LinearLayout>-->

        </LinearLayout>
    </com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout>

</androidx.core.widget.NestedScrollView>

3.java代码

1.初始化与属性

//设置刷新基本属性
        mTrlBlank2Random.setEnableRefresh(false);//不可下拉刷新
        mTrlBlank2Random.setEnableLoadmore(true);//可上拉刷新

2.监听

/**
         * 刷新监听
         */
        mTrlBlank2Random.setOnRefreshListener(new RefreshListenerAdapter() {
            @Override
            public void onLoadMore(TwinklingRefreshLayout refreshLayout) {
                super.onLoadMore(refreshLayout);
                //通知presenter层更新数据
                if (mWallpaperClassificationPresenter!=null) {
                    page+=1;
                    mWallpaperClassificationPresenter.getWallpaperRandomDataMore(page);
                }
                //关闭刷新
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mTrlBlank2Random.finishLoadmore();
                    }
                }, 2000);//延迟两秒关闭
            }
        });

3.遇到的问题

1.在没有上拉的情况下会调用loadedMore方法

解决办法

不知道

2.Adapter步伐刷新失败

解决办法

不知道

猜你喜欢

转载自blog.csdn.net/sunweihao2019/article/details/108342937
今日推荐