SwipeRefreshLayout 是 Android 官方 v4 支持包中的一个组件,该组件当中只接受 1 个子组件也就是需要刷新内容的组件,如果有多个子组件时将只展示第一个子组件中所更新的内容,后面的子组件将不予展示。这个子组件必须是允许滚动的,如 ListView、GridView 或者是 RecyclerView 等。
在使用SwipeRefreshLayout 前,我们需要在build.gradle(Module:app)导入v4支持库。
SwipeRefreshLayout 常用方法见下表:
下面我们通过一个简单的实例来简单使用一些SwipeRefreshLayout 组件,并加深其印象。
从示例我们可以看到下拉一些会加载新的数据。
//设置下拉监听事件
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
//创建线程,在子线程更新数据
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
data.add("刷新数据");
adapter.notifyDataSetChanged();
//数据更新完成,swipeRefreshLayout处于为刷新状态
swipeRefreshLayout.setRefreshing(false);
}
},1000);
}
});
可以看到我们在onRefresh()方法中创建了一个子线程,用于更新数据,这样就简单实现了数据刷新。
下面贴出源码:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".MainActivity">
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/swipe_refresh_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.example.mydemo;
import androidx.appcompat.app.AppCompatActivity;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private SwipeRefreshLayout swipeRefreshLayout;
private ListView listView;
private ArrayAdapter adapter;
private List<String> data = new ArrayList<>(Arrays.asList("初始数据1","初始数据2","初始数据3"));
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initListView();
initSwipeRefreshLayout();
}
@SuppressLint("ResourceAsColor")
private void initSwipeRefreshLayout() {
swipeRefreshLayout = findViewById(R.id.swipe_refresh_layout);
//设置刷新颜色资源
swipeRefreshLayout.setColorSchemeResources(
android.R.color.holo_red_dark,
android.R.color.holo_green_dark);
//设置刷新背景颜色
swipeRefreshLayout.setBackgroundColor(android.R.color.background_light);
//设置手指下拉多少触发监听
swipeRefreshLayout.setDistanceToTriggerSync(30);
//设置下拉监听事件
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
//创建线程,在子线程更新数据
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
data.add("刷新数据");
adapter.notifyDataSetChanged();
//数据更新完成,swipeRefreshLayout处于为刷新状态
swipeRefreshLayout.setRefreshing(false);
}
},1000);
}
});
}
private void initListView() {
listView = findViewById(R.id.list_view);
adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,data);
listView.setAdapter(adapter);
}
}