SwipeRefreshLayout: Swipe progress animation

2019-01-22 21:39发布

问题:

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

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.

<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?

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?

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

回答1:

You can't change the animation.

In SwipeRefreshLayout you 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 takes 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 ListView in your SwipeRefreshLayout. You should be good to go from here.



回答2:

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.