【发布时间】:2021-08-22 08:58:54
【问题描述】:
当我使用 SwipeRefreshLayout 时,它会在我尝试在列表顶部向上滚动时刷新。这次我想应用同样的东西,但刷新的触发器是在列表底部但仍想向下滚动时。有谁知道我怎样才能做到这一点?非常感谢您的帮助。先感谢您。但是在java中,请。
【问题讨论】:
标签: java android list refresh swiperefreshlayout
当我使用 SwipeRefreshLayout 时,它会在我尝试在列表顶部向上滚动时刷新。这次我想应用同样的东西,但刷新的触发器是在列表底部但仍想向下滚动时。有谁知道我怎样才能做到这一点?非常感谢您的帮助。先感谢您。但是在java中,请。
【问题讨论】:
标签: java android list refresh swiperefreshlayout
第 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!
【讨论】: