在本文中,我们将介绍SwipeRefreshLayout组件。 每当用户可以使用滑动手势刷新UI时,都应使用此组件。 在上一篇文章中,我们讨论了另一种刷新UI的方法,称为摇晃刷新 ,用户可以摇动智能手机,然后使用加速度传感器感应应用程序刷新UI。 我们谈过这个刷新模式在这个自定义实现后 ,我们实施了一些喜欢它。
SwipeRefreshLayout组件是SDK提供的标准实现,并且已在Android提供的某些应用程序(即Gmail)中使用。
介绍
该组件仅接受一个孩子:我们要刷新的孩子。 它使用侦听器机制来通知持有此组件的侦听器发生了刷新事件,因此换句话说,我们的Activity必须实现要通知的接口。 该活动负责处理刷新事件并刷新相应的View。 如果侦听器在接收到事件后确定应该进行刷新过程并想要显示“刷新动画”,则必须调用setRefrshing(true)
,否则可以取消调用setRefreshing(false)
的动画。
如何使用SwipeRefreshLayout
现在我们知道了该组件的工作原理,我们将创建一个简单的示例来演示如何使用它。 让我们假设我们想在用户使用垂直滑动手势时生成一个随机数:
通常,此组件是根组件:
<android.support.v4.widget.SwipeRefreshLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:id="@+id/swipe">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Random number:"
android:id="@+id/lbl"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/rndNum"
android:layout_toRightOf="@id/lbl"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/lbl"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:text="Swipe to Refresh"
style="@android:style/TextAppearance.Medium"/>
</RelativeLayout>
</ScrollView>
</android.support.v4.widget.SwipeRefreshLayout>
从上面的布局可以看到,SwipeRefreshLayout只有一个孩子。 现在我们可以编写活动代码:
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final SwipeRefreshLayout swipeView = (SwipeRefreshLayout) findViewById(R.id.swipe);
final TextView rndNum = (TextView) findViewById(R.id.rndNum);
swipeView.setColorScheme(android.R.color.holo_blue_dark, android.R.color.holo_blue_light, android.R.color.holo_green_light, android.R.color.holo_green_light);
swipeView.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
swipeView.setRefreshing(true);
Log.d("Swipe", "Refreshing Number");
( new Handler()).postDelayed(new Runnable() {
@Override
public void run() {
swipeView.setRefreshing(false);
double f = Math.random();
rndNum.setText(String.valueOf(f));
}
}, 3000);
}
});
}
....
如您所见,我们示例中的所有内容都发生在onCreate方法中。 在第6行,我们获得对SwipeRefreshLayout的引用,以便我们可以设置侦听器(第10、11、12行)。 在侦听器中,我们只需设置setRefreshing(true)即可刷新动画,然后生成随机数。 最后(我们模拟了一个很长的过程),我们停止了动画。
使用ListView的SwipeRefreshLayout
另一个有趣的示例是如何将SwipeRefreshLayout与ListView一起使用。 这是一个有趣的示例,因为在实际应用中,我们经常遇到这种情况,即我们的ListView包含一些项目,我们想刷新它们。 如果ListView是SwipeRefreshLayout的唯一一个子元素,那么我们就不会有任何问题,因为一切都会顺利进行。 在某些情况下,我们不仅具有ListView,而且还具有其他元素,让我们假设我们具有如下所示的UI:
这种情况稍微复杂一点,因为如果我们向上滚动ListView中的项目,那么一切都会按预期进行,但是如果向下滚动,刷新过程就会开始,并且列表项目不会按照我们的期望滚动。 在这种情况下,我们可以使用技巧,我们可以使用setEnabled(false)
禁用刷新通知,并在ListView中的第一项可见后立即再次启用它:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final SwipeRefreshLayout swipeView = (SwipeRefreshLayout) findViewById(R.id.swipe);
swipeView.setEnabled(false);
ListView lView = (ListView) findViewById(R.id.list);
ArrayAdapter<String> adp = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, createItems(40,0 ));
lView.setAdapter(adp);
swipeView.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
swipeView.setRefreshing(true);
( new Handler()).postDelayed(new Runnable() {
@Override
public void run() {
swipeView.setRefreshing(false);
}
}, 3000);
}
});
lView.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView absListView, int i) {
}
@Override
public void onScroll(AbsListView absListView, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
if (firstVisibleItem == 0)
swipeView.setEnabled(true);
else
swipeView.setEnabled(false);
}
});
}
如您在第33行所看到的,我们重写了ListView的onScrollListener来处理启用/禁用机制。
翻译自: https://www.javacodegeeks.com/2014/05/android-swiperefreshlayout-tutorial.html