Android SwipereFreshLayout下拉刷新

Android SwipereFreshLayout下拉刷新

我们都知道现在android5.0以后就提倡使用Material Design设计了。在Material Design设计就有一个非常好的设计SwipereFreshLayout,下面我们就来看看它的使用。既然它来源于Material Design,我们第一步就应该是添加它的库。

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的父布局,当滑动到ListView的边界时,SwipeRefreshLayout就会显示正在刷新的动画,同时会提供一个onRefresh的事件供我们加载数据。

3、提供数据源

这里我们直接用ArrayAdapter就行了,所以我们直接来定义string-array就行了。

 <string-array name="singer_names">
    <item>周杰伦</item>
    <item>那英</item>
    <item>刘德华</item>
    <item>张学友</item>
    <item>许巍</item>
    <item>朴树</item>
    <item>陈奕迅</item>
    <item>A_Lin</item>
    <item>杨宗纬</item>
  </string-array>

4、设置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,getSingerNames());
       mListView.setAdapter((ListAdapter) mAdapter);
       //设置刷新加载效果的icon是否继续显示
       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;
  }

主要是实现SwipeRefreshLayout.OnRefreshListener接口,然后实现onRefresh就可以刷新数据了,然后通过刷新数据源就可以更新数据了。其实用起来还是很简单的。

我们再来看看SwipeRefreshLayout的其他属性。

setColorSchemeResources(R.color.orange,R.color.green,R.color.blue); 改变加载图标的颜色。这样SwipeRefreshLayout旋转的时候将会在这三种颜色间切换

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

这个属性在一个地方可能会用到,那就是SwipereFreshLayout包含多个childView的时候,有一个滑动事件冲突的问题,ListView只能上滑,而不能下拉。一旦下拉,就会触发SwipeRefreshLayout的下拉刷新。这种情况肯定是在事件派发上出了问题。下拉的事件在通常情况下应该由ListView来进行处理;当ListView位于顶部时,由SwipeRefreshLayout来进行处理。而现在的情况是全都由SwipeRefreshLayout来处理的。这个问题有两种解决的办法:

1、我们知道这个是因为滑动派发的问题,那我们可以自定义一个SwipeRefreshLayout继承的ImprovedSwipeLayout;

在values文件夹中新建一个attrs.xml,内容如下:

<?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来实现,通过ListView的OnScrollListener来实现,当滑动到第一个可见的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);
    }
  });

这样,就可以很好的解决这个问题了。

 感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

以上是来客网为你收集整理的Android SwipereFreshLayout下拉刷新全部内容,希望文章能够帮你解决Android SwipereFreshLayout下拉刷新所遇到的程序开发问题。

如果觉得来客网网站内容还不错,欢迎将来客网网站推荐给程序员好友。