溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Android SwipereFreshLayout下拉刷新

發(fā)布時間:2020-10-21 19:15:04 來源:腳本之家 閱讀:143 作者:lqh 欄目:移動開發(fā)

Android SwipereFreshLayout下拉刷新

我們都知道現(xiàn)在android5.0以后就提倡使用Material Design設(shè)計了。在Material Design設(shè)計就有一個非常好的設(shè)計SwipereFreshLayout,下面我們就來看看它的使用。既然它來源于Material Design,我們第一步就應(yīng)該是添加它的庫。

1、我們就在build.gradle添加庫:

 compile 'com.android.support:support-v4:22.1.1'

2、然后我們就直接在res/layouts/activity_main.xml布局里面使用:

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

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

我們可以看到SwipeRefreshLayout作為ListView的父布局,當(dāng)滑動到ListView的邊界時,SwipeRefreshLayout就會顯示正在刷新的動畫,同時會提供一個onRefresh的事件供我們加載數(shù)據(jù)。

3、提供數(shù)據(jù)源

這里我們直接用ArrayAdapter就行了,所以我們直接來定義string-array就行了。

 <string-array name="singer_names">
    <item>周杰倫</item>
    <item>那英</item>
    <item>劉德華</item>
    <item>張學(xué)友</item>
    <item>許巍</item>
    <item>樸樹</item>
    <item>陳奕迅</item>
    <item>A_Lin</item>
    <item>楊宗緯</item>
  </string-array>

4、設(shè)置adapter

 setContentView(R.layout.activity_main);
    mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.id_swipe_refresh);
    mListView =(ListView)findViewById(R.id.id_listview);
    String[] singer = getResources().getStringArray(R.array.singer_names);
    mAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, singer);
    mListView.setAdapter((ListAdapter) mAdapter);
    mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
      @Override
      public void onRefresh() {
        refreshContent();
      }
    });
 private void refreshContent(){
   new Handler().postDelayed(new Runnable() {
     @Override
     public void run() {
       mAdapter = new ArrayAdapter<>(MainActivity.this, android.R.layout.simple_list_item_1, getSingerNames());
       mListView.setAdapter((ListAdapter) mAdapter);
       //設(shè)置刷新加載效果的icon是否繼續(xù)顯示
       mSwipeRefreshLayout.setRefreshing(false);
     }
   },2000);
    }
  private List<String> getSingerNames() {
    List<String> newCatNames = new ArrayList<String>();
    for (int i = 0; i < mSingerNames.length; i++) {
      int randomCatNameIndex = new Random().nextInt(mSingerNames.length - 1);
      newCatNames.add(mSingerNames[randomCatNameIndex]);
    }
    return newCatNames;
  }

主要是實現(xiàn)SwipeRefreshLayout.OnRefreshListener接口,然后實現(xiàn)onRefresh就可以刷新數(shù)據(jù)了,然后通過刷新數(shù)據(jù)源就可以更新數(shù)據(jù)了。其實用起來還是很簡單的。

我們再來看看SwipeRefreshLayout的其他屬性。

setColorSchemeResources(R.color.orange, R.color.green, R.color.blue); 改變加載圖標(biāo)的顏色。這樣SwipeRefreshLayout旋轉(zhuǎn)的時候?qū)谶@三種顏色間切換

setEnabled(false)禁止使用刷新通知

這個屬性在一個地方可能會用到,那就是SwipereFreshLayout包含多個childView的時候,有一個滑動事件沖突的問題,ListView只能上滑,而不能下拉。一旦下拉,就會觸發(fā)SwipeRefreshLayout的下拉刷新。這種情況肯定是在事件派發(fā)上出了問題。下拉的事件在通常情況下應(yīng)該由ListView來進行處理;當(dāng)ListView位于頂部時,由SwipeRefreshLayout來進行處理。而現(xiàn)在的情況是全都由SwipeRefreshLayout來處理的。這個問題有兩種解決的辦法:

1、我們知道這個是因為滑動派發(fā)的問題,那我們可以自定義一個SwipeRefreshLayout繼承的ImprovedSwipeLayout;

在values文件夾中新建一個attrs.xml,內(nèi)容如下:

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

在使用自定義View中指定ListView的id:

<com.goach.palm.demo.ImprovedSwipeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  xmlns:fab="http://schemas.android.com/apk/res-auto"
  xmlns:isl="http://schemas.android.com/apk/res-auto"
  android:id="@+id/swipe_container"
  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:id="@+id/list_statuses"
      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>

</com.goach.palm.demo.ImprovedSwipeLayout>

最后是我的ImprovedSwipeLayout全部代碼:

public class ImprovedSwipeLayout extends SwipeRefreshLayout {

  private static final String TAG = ImprovedSwipeLayout.class.getCanonicalName();
  private int mScrollableChildId;
  private View mScrollableChild;

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

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

    TypedArray a = context.obtainStyledAttributes(
        attrs, R.styleable.ImprovedSwipeLayoutAttrs);
    mScrollableChildId = a.getResourceId(R.styleable.ImprovedSwipeLayoutAttrs_scrollableChildId, 0);
    mScrollableChild = findViewById(mScrollableChildId);
    a.recycle();
  }

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

    if (android.os.Build.VERSION.SDK_INT < 14) {
      if (mScrollableChild instanceof AbsListView) {
        final AbsListView absListView = (AbsListView) mScrollableChild;
        return absListView.getChildCount() > 0
            && (absListView.getFirstVisiblePosition() > 0 || absListView.getChildAt(0)
            .getTop() < absListView.getPaddingTop());
      } else {
        return mScrollableChild.getScrollY() > 0;
      }
    } else {
      return ViewCompat.canScrollVertically(mScrollableChild, -1);
    }
  }

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

}

還有一種方法就是我們使用上面的setEnabled來實現(xiàn),通過ListView的OnScrollListener來實現(xiàn),當(dāng)滑動到第一個可見的item為0的時候,我們就setEnabled(true),否則反之。

 lView.setOnScrollListener(new AbsListView.OnScrollListener() {
      @Override
       public void onScrollStateChanged(AbsListView absListView, int i) {

    }

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

這樣,就可以很好的解決這個問題了。

 感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI