Android SwipeRefreshLayout:滑动进度动画

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24099755/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:47:41  来源:igfitidea点击:

SwipeRefreshLayout: Swipe progress animation

androidlistviewandroid-animationswiperefreshlayout

提问by Simon

I'm quite new to android and I'm exploring the sample code on google website. The code I'm on currently is the SwipeRefreshLayout: http://developer.android.com/samples/SwipeRefreshLayoutBasic/index.html

我对 android 很陌生,我正在探索谷歌网站上的示例代码。我目前使用的代码是 SwipeRefreshLayout:http: //developer.android.com/samples/SwipeRefreshLayoutBasic/index.html

In the code, we see the SwipeRefreshLayout being executed for a listview, so in other words, if you drag down the list view, the method is triggered and the listview refreshes itself.

在代码中,我们看到 SwipeRefreshLayout 正在为一个列表视图执行,所以换句话说,如果您向下拖动列表视图,该方法会被触发并且列表视图会自行刷新。

<android.support.v4.widget.SwipeRefreshLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/swiperefresh"
      android:layout_width="match_parent"
      android:layout_height="match_parent">

    <ListView
          android:id="@android:id/list"
          android:layout_width="match_parent"
          android:layout_height="match_parent" />

</android.support.v4.widget.SwipeRefreshLayout>

What is confusing me is that there is no layout in the code for the progress animation bar that gets displayed at the top of the listview when you drag it down. Do you think that the SwipeRefreshLayout automatically generates a progress animation bar when you drag it down without requiring the user to specify any layout in xml for it?

让我感到困惑的是,当您向下拖动列表视图时,进度动画栏的代码中没有显示在列表视图顶部的布局。你认为 SwipeRefreshLayout 向下拖动时会自动生成进度动画条,而不需要用户为其指定 xml 中的任何布局?

For example, if one wants to put padding at both ends of the progress animation bar, so that the animation doesn't touch the ends of the screen when you refresh, how would one do it?

例如,如果想在进度动画条的两端放置padding,这样刷新时动画就不会碰到屏幕的两端,那怎么做呢?

The https://developer.android.com/reference/android/support/v4/widget/SwipeRefreshLayout.htmldoesn't really say anything about it.

https://developer.android.com/reference/android/support/v4/widget/SwipeRefreshLayout.html并没有真正说什么。

回答by Gil Vegliach

You can'tchange the animation.

不能改变动画。

In SwipeRefreshLayoutyou can't customize much; I believe this is an attempt of Google to get developers stick to common patterns. What you can style are the four colors of the animation, specified with setColorScheme().

SwipeRefreshLayout你不能自定义太多;我相信这是谷歌试图让开发人员坚持通用模式的尝试。您可以设置样式的是动画的四种颜色,用setColorScheme().

SwipeRefreshLayouttakes care to show its predefined animation at its top for you. This can be when canChildScrollUp()returns false and user 'pulls down', or when you set setRefreshing(true). Turn off the animation with setRefreshing(false). Put your logic in the method onRefreshing()of the listener. Override canChildScrollUp()if you put something that is not a ListViewin your SwipeRefreshLayout. You should be good to go from here.

SwipeRefreshLayout注意在其顶部为您显示其预定义的动画。这可能是当canChildScrollUp()返回 false 并且用户“下拉”时,或者当您设置setRefreshing(true). 用 关闭动画setRefreshing(false)。将您的逻辑放在onRefreshing()侦听器的方法中。重写canChildScrollUp(),如果你把东西是不是ListView在你的SwipeRefreshLayout。你应该很高兴从这里走。

回答by SteelBytes

although you can't change it, you can still hide it and then display your own once triggered :-)

虽然你不能改变它,但你仍然可以隐藏它,然后在触发后显示你自己的 :-)

SwipeRefreshLayout swiperefresh = (SwipeRefreshLayout)root.findViewById(R.id.swiperefresh);
swiperefresh.setOnRefreshListener(this);
swiperefresh.setColorSchemeColors(0,0,0,0);
swiperefresh.setProgressBackgroundColor(android.R.color.transparent); 

Notes:

笔记:

  • in eclipse you have to add @SuppressLint("ResourceAsColor") to compile this.

  • you still need to call setRefreshing(false) at an appropriate point in time else onRefresh doesn't trigger again.

  • 在 Eclipse 中,你必须添加 @SuppressLint("ResourceAsColor") 来编译它。

  • 您仍然需要在适当的时间点调用 setRefreshing(false) 否则 onRefresh 不会再次触发。