This is the 14th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

Android drop down refresh control SwipeRefreshLayout parsing

Before using it we need to update our Android-support-V4.jar version before 19.1 so that we can use it. So the following we through the source code analysis of SwipeRefreshLayout. Take a look at this type of introduction:

/** * The SwipeRefreshLayout should be used whenever the user can refresh the * contents of a view via a vertical swipe gesture. The activity that * instantiates this view should add an OnRefreshListener to be notified * whenever the swipe to refresh gesture is completed. * ... * This layout should be made the parent of the view that will be refreshed as a * result of the gesture and can only support one direct child. This view will * also be made the target of the gesture and will be forced to match both the *  width and the height supplied in this layout. The SwipeRefreshLayout does not * provide accessibility events; instead, a menu item must be provided to allow * refresh of the content wherever this gesture is used. * </p> */ public class SwipeRefreshLayout extends ViewGroup{ ... }Copy the code

When users swipe gesture, SwipeRefreshLayout can be used to swipe data. At the same time? We should add the OnRefreshListener interface to the activity that presents the control to control when the refresh is complete. SwipeRefreshLayout inherits from a ViewGroup, not a ListView, so its implementation logic is independent of ListView. This frees us up from using ListView and does not require complex logic processing. SwipeRefreshLayout should be the parent control of the View to be refreshed. It can only have one child View. And its direct child View has to have a sliding function.

SwipeRefreshLayout: SwipeRefreshLayout: SwipeRefreshLayout: SwipeRefreshLayout 1, Public void setProgressViewOffset(Boolean scale, int start, int end); The following is an explanation of scale’s property values.

Scale: Set to true if there is no view at a higher Z-order than where the progress spinner is Set to appearCopy the code

Public void setProgressViewEndTarget(Boolean scale, int end);

3, public void setSize (int size), set the refresh controls the size of the system DEFAULT to provide us with two options: SwipeRefreshLayout. LARGE (LARGE) and SwipeRefreshLayout. DEFAULT (inset).

Public void setOnRefreshListener(OnRefreshListener Listener), which is used to set our pull-down refresh callback event.

5. Public void setRefreshing(Boolean refreshing), set this parameter to false, and the refreshing control disappears.

6, public void setProgressBackgroundColor (int colorRes), set the refresh controls the background color.

param colorRes Resource id of the color.
Copy the code

7, public void setColorSchemeResources(int… ColorResIds), sets the color in the refresh control animation. The parameter is the resource ID

8, public void setColorSchemeColors(int… Colors), sets the color in the refresh control animation. The parameter is the color value.

9, public Boolean isRefreshing() check whether the refreshing status is ongoing.

10, public void setRefreshing(Boolean refreshing), set whether to refresh the control

The above are several common methods, among which methods 5-10 are commonly used. Now let’s use it.

We are using it on API21, so it is the new refresh effect. To test this, we created a Module named SwiperefreshDemo in Android Studio. 1. Look at the layout file:

<android.support.v4.widget.SwipeRefreshLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" 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/swipeRefresh"
    tools:context=".MainActivity">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <TextView
            android:id="@+id/tv_state"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/app_name"
            android:textColor="#ff0000"
            android:padding="20dp"/>
        </ScrollView>

	</android.support.v4.widget.SwipeRefreshLayout >
Copy the code

2, MainActivity code:

public class MainActivity extends ActionBarActivity { private SwipeRefreshLayout swipeRefreshLayout; private TextView tv_state; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv_state = (TextView) findViewById(R.id.tv_state); swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefresh); swipeRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_light, android.R.color.holo_red_light, android.R.color.holo_orange_light, android.R.color.holo_green_light); swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { / / refresh 5 seconds mHandler. SendEmptyMessageDelayed (0500); }}); } private Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { Tv_state.settext (" refreshing "); . / / refresh after stop the refresh swipeRefreshLayout setRefreshing (false); }}; }Copy the code

Very simple to use, directly look at the effect:

The effect is not very nice, but also a popular drop-down refresh in many apps. Now let’s look at the use of nested ListViews.

ListView dropdown refresh

1. Layout files

<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" 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/swipeRefresh"
    tools:context="test.dsw.com.swiperefreshdemo.ListViewActivity">

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

	</android.support.v4.widget.SwipeRefreshLayout>
Copy the code

2. Process the code in ListViewActivity:

public class ListViewActivity extends ActionBarActivity { private SwipeRefreshLayout mSwipeRefreshLayout; private ListView listView; private MyAdapter adapter; List<ImageInfor> list = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_list_view); initData(); mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefresh); listView = (ListView) findViewById(R.id.listView); adapter = new MyAdapter(list); listView.setAdapter(adapter); mSwipeRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_light, android.R.color.holo_red_light, android.R.color.holo_orange_light, android.R.color.holo_green_light); mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override public void onRefresh() { adapter.addList(list); MHandler. SendEmptyMessageDelayed (0500); }}); } private Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { Toast.maketext (getApplication()," data updated ", toast.length_short).show(); . / / refresh after stop the refresh mSwipeRefreshLayout setRefreshing (false); }}; . }Copy the code

And look at the renderings:

SwipeRefreshLayout is a ViewGroup that can be wrapped around any control you want. This reduces the coupling between the refresh and the control and increases flexibility. So let’s go through these two cases in their entirety.The above is the use effect in API21, for the effect of 4.0 system we will not demonstrate, directly for your reference!