An overview of the

Google has launched SwipeRefreshLayout for a drop-down refresh. Compared with the commonly used pull-to-refesh, this scheme is more simple and convenient.

Associated project references (management dependencies)

Add the following to your app-level build.gradle:

The compile 'com. Android. Support: appcompat - v7:23.0.0' compile 'com. Android. Support: support - v4:23.0.0'Copy the code

Write a Layout.

<LinearLayout 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:orientation="vertical"
    tools:context=".MainActivity">


    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipeRefreshLayout1"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ListView
            android:id="@+id/listview1"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </android.support.v4.widget.SwipeRefreshLayout>
</LinearLayout>
Copy the code

Add listeners to your code to handle events

public void setOnRefreshListener(OnRefreshListener listener)

The complete code

package demo.vir56k.swiperefreshlayoutdemo; import android.os.Handler; import android.support.v4.widget.SwipeRefreshLayout; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.widget.ArrayAdapter; import android.widget.ListView; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class MainActivity extends AppCompatActivity { private static final int REFRESH_COMPLETE = 0X110; private SwipeRefreshLayout mSwipeLayout; private ListView mListView; private ArrayAdapter<String> mAdapter; private List<String> mDatas; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mListView = (ListView) findViewById(R.id.listview1); mSwipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout1); mDatas = creatDataSource(); mSwipeLayout.setOnRefreshListener(mSwipeRefreshLayoutOnRefreshListener); mSwipeLayout.setColorSchemeResources(android.R.color.holo_blue_bright, android.R.color.holo_green_light, android.R.color.holo_orange_light, android.R.color.holo_red_light); mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mDatas); mListView.setAdapter(mAdapter); } private List<String> creatDataSource() { mDatas = new ArrayList<String>(); for (int i = 0; i < 5; i++) { mDatas.add("item" + i); } return mDatas; } SwipeRefreshLayout.OnRefreshListener mSwipeRefreshLayoutOnRefreshListener = new SwipeRefreshLayout.OnRefreshListener()  { @Override public void onRefresh() { mHandler.sendEmptyMessageDelayed(REFRESH_COMPLETE, 2000); }}; private Handler mHandler = new Handler() { public void handleMessage(android.os.Message msg) { switch (msg.what) { case REFRESH_COMPLETE: List<String> lst = new ArrayList<String>(); for (int i = mDatas.size() + 3; i > mDatas.size(); i--) { lst.add("item -" + i); } mDatas.addAll(0, lst); mAdapter.notifyDataSetChanged(); mSwipeLayout.setRefreshing(false); break; }}; }; }Copy the code

This is done.

Supplement:

SwipeRefreshLayout’s child controls are not directly a ListView, etc., but a normal RelativeWLayout.

Solutions:

Rewrite the canChildScrollUp method with SwipeRefreshLayout

Code:

/* * Copyright 2016, The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the  License. */ package com.example.android.architecture.blueprints.todoapp.tasks; import android.content.Context; import android.support.v4.view.ViewCompat; import android.support.v4.widget.SwipeRefreshLayout; import android.util.AttributeSet; import android.view.View; /** * Extends {@link SwipeRefreshLayout} to support non-direct descendant scrolling views. * <p> * {@link SwipeRefreshLayout} works as expected when a scroll view is a direct child: it triggers * the refresh only when the view is on top. This class adds a way (@link #setScrollUpChild} to * define which view controls this behavior. */ public class ScrollChildSwipeRefreshLayout extends SwipeRefreshLayout { private View mScrollUpChild; public ScrollChildSwipeRefreshLayout(Context context) { super(context); } public ScrollChildSwipeRefreshLayout(Context context, AttributeSet attrs) { super(context, attrs); } @Override public boolean canChildScrollUp() { if (mScrollUpChild ! = null) { return ViewCompat.canScrollVertically(mScrollUpChild, -1); } return super.canChildScrollUp(); } public void setScrollUpChild(View view) { mScrollUpChild = view; }}Copy the code