如何解决当用户尝试向下滚动时,如何在列表底部实现刷新?
当我使用 SwipeRefreshLayout 时,它会在我尝试在列表顶部向上滚动时刷新。这次我想应用相同的东西,但刷新的触发器是在列表底部但仍想向下滚动时。有谁知道我怎样才能做到这一点?非常感谢您的帮助。先感谢您。但是,请使用 Java。
解决方法
第一步:编写代码到 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 作为父布局,当用户滑动布局时,它可以刷新子视图。
step3:代码到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);
}
});
}
}
让我们把你好世界改成 shafri
swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
i++;
textView.setText("shafri"+i);
swipeRefresh.setRefreshing(false);
}
});
快乐代码!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。