Android SwipereFreshLayout下拉刷新
Android SwipereFreshLayout下拉刷新
我們都知道現(xiàn)在android5.0以后就提倡使用Material Design設(shè)計(jì)了。在Material Design設(shè)計(jì)就有一個(gè)非常好的設(shè)計(jì)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)滑動(dòng)到ListView的邊界時(shí),SwipeRefreshLayout就會(huì)顯示正在刷新的動(dòng)畫,同時(shí)會(huì)提供一個(gè)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í)用起來還是很簡單的。
我們?cè)賮砜纯碨wipeRefreshLayout的其他屬性。
setColorSchemeResources(R.color.orange, R.color.green, R.color.blue); 改變加載圖標(biāo)的顏色。這樣SwipeRefreshLayout旋轉(zhuǎn)的時(shí)候?qū)?huì)在這三種顏色間切換
setEnabled(false)禁止使用刷新通知
這個(gè)屬性在一個(gè)地方可能會(huì)用到,那就是SwipereFreshLayout包含多個(gè)childView的時(shí)候,有一個(gè)滑動(dòng)事件沖突的問題,ListView只能上滑,而不能下拉。一旦下拉,就會(huì)觸發(fā)SwipeRefreshLayout的下拉刷新。這種情況肯定是在事件派發(fā)上出了問題。下拉的事件在通常情況下應(yīng)該由ListView來進(jìn)行處理;當(dāng)ListView位于頂部時(shí),由SwipeRefreshLayout來進(jìn)行處理。而現(xiàn)在的情況是全都由SwipeRefreshLayout來處理的。這個(gè)問題有兩種解決的辦法:
1、我們知道這個(gè)是因?yàn)榛瑒?dòng)派發(fā)的問題,那我們可以自定義一個(gè)SwipeRefreshLayout繼承的ImprovedSwipeLayout;
在values文件夾中新建一個(gè)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)滑動(dòng)到第一個(gè)可見的item為0的時(shí)候,我們就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); } });
這樣,就可以很好的解決這個(gè)問題了。
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
- Android 使用SwipeRefreshLayout控件仿抖音做的視頻下拉刷新效果
- Android SwipeRefreshLayout仿抖音app靜態(tài)刷新
- android使用SwipeRefreshLayout實(shí)現(xiàn)ListView下拉刷新上拉加載
- android基于SwipeRefreshLayout實(shí)現(xiàn)類QQ的側(cè)滑刪除
- Android SwipeRefreshLayout下拉刷新組件示例
- Android實(shí)現(xiàn)SwipeRefreshLayout首次進(jìn)入自動(dòng)刷新
- Android SwipeRefreshLayout下拉刷新源碼解析
- Android SwipeRefreshLayout超詳細(xì)講解
相關(guān)文章
Android 使用Vitamio打造自己的萬能播放器(3)——本地播放(主界面、播放列表)
本文主要介紹 Android Vitamio本地播放功能,這里提供實(shí)例代碼和效果圖以便大家參考,有需要的小伙伴可以參考下2016-07-07android實(shí)現(xiàn)可上下回彈的scrollview
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)可上下回彈的scrollview,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04Android復(fù)選框CheckBox與開關(guān)按鈕Switch及單選按鈕RadioButton使用示例詳解
這篇文章主要介紹了Android復(fù)選框CheckBox與開關(guān)按鈕Switch及單選按鈕RadioButton使用示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2022-09-09Android開發(fā)中R.java文件丟失或無法更新的解決方法
這篇文章主要介紹了Android開發(fā)中R.java文件丟失或無法更新的解決方法,較為詳細(xì)的列舉分析了出現(xiàn)R.java文件丟失或無法更新的常見原因及相應(yīng)的解決方法,需要的朋友可以參考下2016-02-02Android獲取網(wǎng)絡(luò)連接狀態(tài)新方法整理
這篇文章主要給大家介紹了關(guān)于Android獲取網(wǎng)絡(luò)連接狀態(tài)新方法的相關(guān)資料,在開發(fā)安卓移動(dòng)端時(shí)幾乎每一個(gè)app都需要連接網(wǎng)絡(luò),因此對(duì)設(shè)備的網(wǎng)絡(luò)狀態(tài)檢測是很有必要的,需要的朋友可以參考下2023-11-11Android?自定義view中根據(jù)狀態(tài)修改drawable圖片
這篇文章主要介紹了Android?自定義view中根據(jù)狀態(tài)修改drawable圖片的相關(guān)資料,需要的朋友可以參考下2023-07-07