SwipeRefreshLayout窗口小部件用于实现从刷新到刷新的用户界面设计模式。它使用垂直滑动手势来刷新视图的内容。 SwipeRefreshLayout小部件检测垂直滑动并显示不同的进度条,并在应用程序中触发回调方法。若要使用此行为,SwipeRefreshLayout小部件必须是ListView或GridView的父级。 SwipeRefreshLayout小部件的这种行为使用户可以手动刷新布局。 SwipeRefreshLayout类包含一个名为OnRefreshListener的侦听器。想要使用此侦听器的类应实现SwipeRefreshLayout.OnRefreshListener接口。在垂直向下滑动手势时,将触发此侦听器,并调用onRefresh()方法,并且可以根据需要重写此方法。
Swipe-to-Refresh ListView is very much similar to Swipe-to-Refresh RecyclerView. In place of ListView, we use RecyclerView. Please refer to Pull to Refresh with RecyclerView in Android with Example.
例子
在此示例中,我们将字符串类型的数据存储到用于填充ListView的ArrayList中。每当调用OnRefresh()方法时,就会对ArrayList数据进行混洗。下面给出了一个示例GIF,以使我们对本文中要做的事情有一个了解。注意,我们将使用Java语言实现该项目。
分步实施
步骤1:创建一个新项目
要在Android Studio中创建新项目,请参阅如何在Android Studio中创建/启动新项目。请注意,选择Java作为编程语言。
步骤2:添加依赖项
我们正在使用SwipeRefreshLayout。因此,我们需要为其添加依赖项。要添加依赖项,请转到Gradle脚本> build.gradle(Module:app)并添加以下依赖项。添加依赖项后,您需要单击立即同步。
dependencies {
implementation ‘androidx.swiperefreshlayout:swiperefreshlayout:1.1.0’
}
在继续之前,让我们添加一些颜色属性以增强应用程序栏。转到应用程序> res>值> colors.xml并添加以下颜色属性。
XML
#0F9D58
#16E37F
#03DAC5
XML
Java
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
SwipeRefreshLayout swipeRefreshLayout;
ListView listView;
ArrayList arrayList = new ArrayList<>(Arrays.asList("C-Language", "Java", "Data Structure",
"Networking", "Operating System", "Compiler Design", "Theory Of Computation",
"Software Engineering", "Web Engineering"));
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Getting the reference of SwipeRefreshLayout and ListView
swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);
listView = (ListView) findViewById(R.id.listView);
// simple_list_item_1 is a built in layout. It is part of Android OS, instead of creating our own
// xml layout we are using built-in layout
ArrayAdapter arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, arrayList);
listView.setAdapter(arrayAdapter);
// Implementing setOnRefreshListener on SwipeRefreshLayout
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
swipeRefreshLayout.setRefreshing(false);
// User defined method to shuffle the array list items
shuffleListItems();
}
});
}
public void shuffleListItems() {
// Shuffling the arraylist items on the basis of system time
Collections.shuffle(arrayList, new Random(System.currentTimeMillis()));
ArrayAdapter arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, arrayList);
listView.setAdapter(arrayAdapter);
}
}
步骤3:使用activity_main.xml文件
在此步骤中,我们将创建SwipeRefreshLayout并将ListView添加到其中。转到应用程序> res>布局> activity_main.xml并添加以下代码段。
XML格式
步骤4:使用MainActivity。 Java文件
在这一步中,我们获得SwipeRefreshLayout和ListView的引用,它们是在activiy_main.xml布局文件中定义的。我们将创建一个字符串元素的ArrayList并实现ArrayAdapter以便将数据设置为列表。然后,我们在SwipeRefreshLayout上实现setOnRefreshListener事件,并调用OnRefresh()方法对列表元素进行随机播放。
Java
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
SwipeRefreshLayout swipeRefreshLayout;
ListView listView;
ArrayList arrayList = new ArrayList<>(Arrays.asList("C-Language", "Java", "Data Structure",
"Networking", "Operating System", "Compiler Design", "Theory Of Computation",
"Software Engineering", "Web Engineering"));
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Getting the reference of SwipeRefreshLayout and ListView
swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);
listView = (ListView) findViewById(R.id.listView);
// simple_list_item_1 is a built in layout. It is part of Android OS, instead of creating our own
// xml layout we are using built-in layout
ArrayAdapter arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, arrayList);
listView.setAdapter(arrayAdapter);
// Implementing setOnRefreshListener on SwipeRefreshLayout
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
swipeRefreshLayout.setRefreshing(false);
// User defined method to shuffle the array list items
shuffleListItems();
}
});
}
public void shuffleListItems() {
// Shuffling the arraylist items on the basis of system time
Collections.shuffle(arrayList, new Random(System.currentTimeMillis()));
ArrayAdapter arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, arrayList);
listView.setAdapter(arrayAdapter);
}
}