最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android SwipereFreshLayout下拉刷新

 更新時間:2017年06月01日 11:14:36   投稿:lqh  
這篇文章主要介紹了Android SwipereFreshLayout下拉刷新的相關(guān)資料,需要的朋友可以參考下

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;
  }

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

我們再來看看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來進(jìn)行處理;當(dāng)ListView位于頂部時,由SwipeRefreshLayout來進(jìn)行處理。而現(xiàn)在的情況是全都由SwipeRefreshLayout來處理的。這個問題有兩種解決的辦法:

1、我們知道這個是因?yàn)榛瑒优砂l(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來實(shí)現(xiàn),通過ListView的OnScrollListener來實(shí)現(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);
    }
  });

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

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

相關(guān)文章

  • Android簡單實(shí)現(xiàn)天氣預(yù)報App

    Android簡單實(shí)現(xiàn)天氣預(yù)報App

    這篇文章主要為大家詳細(xì)介紹了Android簡單實(shí)現(xiàn)天氣預(yù)報App,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-09-09
  • Kotlin數(shù)據(jù)容器深入講解

    Kotlin數(shù)據(jù)容器深入講解

    Kotlin的數(shù)據(jù)容器分為數(shù)組和集合。其中集合分為集合Set、隊(duì)列List、映射Map等三種集合,每種又包括只讀和可變兩種類型,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧
    2022-09-09
  • Android中創(chuàng)建快捷方式及刪除快捷方式實(shí)現(xiàn)方法

    Android中創(chuàng)建快捷方式及刪除快捷方式實(shí)現(xiàn)方法

    這篇文章主要介紹了Android中創(chuàng)建快捷方式及刪除快捷方式實(shí)現(xiàn)方法,本文直接給出實(shí)現(xiàn)代碼,需要的朋友可以參考下
    2015-06-06
  • Android仿微信朋友圈全文、收起功能的實(shí)例代碼

    Android仿微信朋友圈全文、收起功能的實(shí)例代碼

    本篇文章主要介紹了Android仿微信朋友圈全文、收起功能的實(shí)例代碼,具有一定的參考價值,有興趣的可以了解一下
    2017-08-08
  • Android WebView userAgent 設(shè)置為桌面UA實(shí)例

    Android WebView userAgent 設(shè)置為桌面UA實(shí)例

    這篇文章主要介紹了Android WebView userAgent 設(shè)置為桌面UA實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-03-03
  • Android實(shí)現(xiàn)語音合成與識別功能

    Android實(shí)現(xiàn)語音合成與識別功能

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)語音合成與識別功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-07-07
  • Android簡易電話撥號器實(shí)例詳解

    Android簡易電話撥號器實(shí)例詳解

    這篇文章主要為大家詳細(xì)介紹了Android簡易電話撥號器實(shí)例,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-07-07
  • Android提高之SurfaceView與多線程的混搭實(shí)例

    Android提高之SurfaceView與多線程的混搭實(shí)例

    這篇文章主要介紹了Android提高之SurfaceView與多線程的混搭,很實(shí)用的功能,需要的朋友可以參考下
    2014-08-08
  • 深入理解Android中的xmlns:tools屬性

    深入理解Android中的xmlns:tools屬性

    關(guān)于xmlns:tools屬性的介紹網(wǎng)上有很多,小編覺得有必要整理一篇介紹較為詳細(xì)的內(nèi)容給大家,下面這篇文章就很深入的介紹了關(guān)于Android中的xmlns:tools屬性,有需要的朋友們可以參考借鑒,下面來一起看看吧。
    2016-12-12
  • Android Studio給各種控件加邊框的操作方法

    Android Studio給各種控件加邊框的操作方法

    這篇文章主要介紹了Android Studio給各種控件加邊框的操作方法,本文通過圖文實(shí)例相結(jié)合給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-12-12

最新評論

霞浦县| 大悟县| 牡丹江市| 绍兴市| 白朗县| 三门峡市| 广南县| 河北省| 百色市| 措美县| 射洪县| 贵溪市| 镇宁| 东乌珠穆沁旗| 遵义县| 澄江县| 曲麻莱县| 通州市| 周至县| 汉川市| 宿州市| 米泉市| 大足县| 梧州市| 望都县| 景谷| 海南省| 石家庄市| 通道| 台中市| 礼泉县| 尉犁县| 乌拉特中旗| 新巴尔虎右旗| 博野县| 姜堰市| 云梦县| 桃源县| 海丰县| 郯城县| 清徐县|