国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android SwipereFreshLayout下拉刷新

2019-10-23 18:27:33
字體:
來源:轉載
供稿:網友

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, android.R.layout.simple_list_item_1, 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開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 景宁| 镇远县| 苏尼特右旗| 镇赉县| 连云港市| 股票| 浮山县| 资溪县| 衡南县| 安国市| 资阳市| 周宁县| 台州市| 金昌市| 汉中市| 宣汉县| 郑州市| 仁怀市| 五大连池市| 全州县| 射洪县| 凉城县| 石林| 高密市| 宜黄县| 商都县| 浮梁县| 桐梓县| 喀什市| 临洮县| 苏尼特左旗| 通州市| 水富县| 淮南市| 青龙| 崇左市| 百色市| 大冶市| 龙井市| 旌德县| 雷波县|