SoFunction
Updated on 2025-04-11

Android SwipereFreshLayout drop-down refresh

Android SwipereFreshLayout drop-down refresh

We all know that after Android 5.0, we have advocated the use of Material Design design. There is a very good design in Material Design. Let’s take a look at its use. Since it comes from Material Design, our first step is to add its library.

1. We are adding the library:

 compile ':support-v4:22.1.1'

2. Then we use it directly in the res/layouts/activity_main.xml layout:

<.
  xmlns:andro
  android:
  android:layout_width="match_parent"
  android:layout_height="wrap_content">
  <ListView
    android:
    android:layout_width="match_parent"
    android:layout_height="match_parent"></ListView>

</.>

We can see that SwipeRefreshLayout is the parent layout of the ListView. When sliding to the boundary of the ListView, SwipeRefreshLayout will display the refreshing animation, and will provide an onRefresh event for us to load the data.

3. Provide data sources

Here we just need to use ArrayAdapter, so we just need to define string-array directly.

 &lt;string-array name="singer_names"&gt;
    &lt;item&gt;Jay Chou&lt;/item&gt;
    &lt;item&gt;Na Ying&lt;/item&gt;
    &lt;item&gt;Andy Lau&lt;/item&gt;
    &lt;item&gt;Jacky Cheung&lt;/item&gt;
    &lt;item&gt;Xu Wei&lt;/item&gt;
    &lt;item&gt;Pu Shu&lt;/item&gt;
    &lt;item&gt;Eason Chan&lt;/item&gt;
    &lt;item&gt;A_Lin&lt;/item&gt;
    &lt;item&gt;Yang Zongwei&lt;/item&gt;
  &lt;/string-array&gt;

4. Set up adapter

 setContentView(.activity_main);
    mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(.id_swipe_refresh);
    mListView =(ListView)findViewById(.id_listview);
    String[] singer = getResources().getStringArray(.singer_names);
    mAdapter = new ArrayAdapter<>(this, .simple_list_item_1, singer);
    ((ListAdapter) mAdapter);
    (new () {
      @Override
      public void onRefresh() {
        refreshContent();
      }
    });
 private void refreshContent(){
   new Handler().postDelayed(new Runnable() {
     @Override
     public void run() {
       mAdapter = new ArrayAdapter&lt;&gt;(, .simple_list_item_1, getSingerNames());
       ((ListAdapter) mAdapter);
       //Set whether the icon of refresh loading effect will continue to display       (false);
     }
   },2000);
    }
  private List&lt;String&gt; getSingerNames() {
    List&lt;String&gt; newCatNames = new ArrayList&lt;String&gt;();
    for (int i = 0; i &lt; ; i++) {
      int randomCatNameIndex = new Random().nextInt( - 1);
      (mSingerNames[randomCatNameIndex]);
    }
    return newCatNames;
  }

The main thing is to implement the interface, then implement onRefresh to refresh the data, and then update the data by refreshing the data source. In fact, it is still very simple to use.

Let's take a look at other properties of SwipeRefreshLayout.

setColorSchemeResources(, , ); Change the color of the loading icon. In this way, when SwipeRefreshLayout rotates, it will switch between these three colors.

setEnabled(false) Disables the use of refresh notifications

This property may be used in one place, that is, when SwipereFreshLayout contains multiple childViews, there is a problem of sliding event conflict. ListView can only slide up, but cannot pull down. Once the pull-down is pulled down, the pull-down refresh of SwipeRefreshLayout will be triggered. This situation must be a problem with the event distribution. Pull-down events should normally be processed by ListView; when the ListView is at the top, it is processed by SwipeRefreshLayout. And now the situation is all handled by SwipeRefreshLayout. There are two solutions to this problem:

1. We know that this is due to the problem of sliding distribution, so we can customize an ImprovedSwipeLayout inherited by SwipeRefreshLayout;

Create a new one in the values ​​folder, with the following content:

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <declare-styleable name="ImprovedSwipeLayoutAttrs">
    <attr name="scrollableChildId" format="reference" />
  </declare-styleable>
</resources>

Specify the id of the ListView in Using Custom View:

<
  xmlns:andro
  xmlns:tools="/tools"
  xmlns:fab="/apk/res-auto"
  xmlns:isl="/apk/res-auto"
  android:
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="@color/md_blue_grey_50"
  isl:scrollableChildId="@+id/list_statuses"
 >

  <FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
      android:
      android:minHeight="?android:attr/listPreferredItemHeight"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:paddingTop="12dp"
      android:paddingBottom="12dp"
      android:paddingLeft="8dp"
      android:paddingRight="8dp"
      android:clipToPadding="false"
      android:divider="@android:color/transparent"
      android:dividerHeight="12dp"/>

    <TextView
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:text="2234544543"
    />
  </FrameLayout>

</>

Finally, there are all my ImprovedSwipeLayout codes:

public class ImprovedSwipeLayout extends SwipeRefreshLayout {

  private static final String TAG = ();
  private int mScrollableChildId;
  private View mScrollableChild;

  public ImprovedSwipeLayout(Context context) {
    this(context, null);
  }

  public ImprovedSwipeLayout(Context context, AttributeSet attrs) {
    super(context, attrs);

    TypedArray a = (
        attrs, );
    mScrollableChildId = (.ImprovedSwipeLayoutAttrs_scrollableChildId, 0);
    mScrollableChild = findViewById(mScrollableChildId);
    ();
  }

  @Override
  public boolean canChildScrollUp() {
    ensureScrollableChild();

    if (.SDK_INT < 14) {
      if (mScrollableChild instanceof AbsListView) {
        final AbsListView absListView = (AbsListView) mScrollableChild;
        return () > 0
            && (() > 0 || (0)
            .getTop() < ());
      } else {
        return () > 0;
      }
    } else {
      return (mScrollableChild, -1);
    }
  }

  private void ensureScrollableChild() {
    if (mScrollableChild == null) {
      mScrollableChild = findViewById(mScrollableChildId);
    }
  }

}

Another method is to use the setEnabled above to implement it, through the OnScrollListener of ListView. When sliding to the first visible item is 0, we setEnabled(true), otherwise otherwise.

 (new () {
      @Override
       public void onScrollStateChanged(AbsListView absListView, int i) {

    }

      @Override
       public void onScroll(AbsListView absListView, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        if (firstVisibleItem == 0)
          (true);
        else
          (false);
    }
  });

In this way, this problem can be solved well.

Thank you for reading, I hope it can help you. Thank you for your support for this site!