【问题标题】:How to implement refresh at the bottom of the list when user try to scroll down?当用户尝试向下滚动时,如何在列表底部实现刷新?
【发布时间】:2021-08-22 08:58:54
【问题描述】:

当我使用 SwipeRefreshLayout 时,它会在我尝试在列表顶部向上滚动时刷新。这次我想应用同样的东西,但刷新的触发器是在列表底部但仍想向下滚动时。有谁知道我怎样才能做到这一点?非常感谢您的帮助。先感谢您。但是在java中,请。

【问题讨论】:

    标签: java android list refresh swiperefreshlayout


    【解决方案1】:

    第 1 步:编码到 res/layout/activity_main.xml

    <?xml version = "1.0" encoding = "utf-8"?>
    <android.support.v4.widget.SwipeRefreshLayout 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:id = "@+id/swipeRefresh"
       android:layout_width = "match_parent"
       android:layout_height = "match_parent"
       tools:context = ".MainActivity">
    <LinearLayout
       android:layout_width = "wrap_content"
       android:gravity = "center"
       android:layout_height = "wrap_content">
       <TextView
          android:id = "@+id/text"
          android:textSize = "30sp"
          android:layout_width = "wrap_content"
          android:layout_height = "wrap_content"
          android:text = "Hello World!"/>
    </LinearLayout>
    </android.support.v4.widget.SwipeRefreshLayout>
    

    在上面的代码中我们给了 swipeRefreshLayout 作为父布局,当用户滑动布局时,它可以刷新子视图。

    第三步:代码到 src/MainActivity.java

    package com.example.aji.myapplication;
    
    import android.support.v4.widget.SwipeRefreshLayout;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.widget.TextView;
    
    public class MainActivity extends AppCompatActivity {
       SwipeRefreshLayout swipeRefresh;
       
       @Override
       protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          final TextView textView = findViewById(R.id.text);
          swipeRefresh = findViewById(R.id.swipeRefresh);
          swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
             @Override
             public void onRefresh() {
              //your stuff
                swipeRefresh.setRefreshing(false);
             }
          });
       }
    }
    

    让我们把 hello world 改成 shafri

    swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
       @Override
       public void onRefresh() {
          i++;
          textView.setText("shafri"+i);
          swipeRefresh.setRefreshing(false);
       }
    });
    

    HappyCode!

    【讨论】:

    • 嗨。谢谢您的回答。对不起,也许我对自己的问题感到困惑哈哈。您显示的代码是向下滚动时可以刷新吗?但是这一次,当用户尝试向下滚动时,即使在列表的末尾,我该如何刷新?
    • 我只是做了你所做的。正常的滑动刷新。所以这次我想要倒刷刷新。 emm 你明白我的意思吗?
    猜你喜欢
    • 2012-10-13
    • 2020-07-06
    • 1970-01-01
    • 1970-01-01
    • 2021-08-15
    • 1970-01-01
    • 1970-01-01
    • 2021-08-22
    • 2013-04-22
    相关资源
    最近更新 更多