『壹』 android下拉刷新怎麼寫
如果你用的是androidStudio 開發工具, 那麼在 gradle 中下了 material design,那麼你就可以直接用一個下拉刷新的控制項SwipeRefreshlayout,將它作為recyclerView或listView的父級(即包含recyclerView或listView),就可以實現刷新的樣式。(但實際上沒有刷新,只是有個圈圈在轉,真正的數據重新載入需要你重新網路請求數據)
『貳』 android怎麼實現下拉刷新
首先講一下實現原理。這里我們將採取的方案是使用組合View的方式,先自定義一個布局繼承自LinearLayout,然後在這個布局中加入下拉頭和ListView這兩個子元素,並讓這兩個子元素縱向排列。初始化的時候,讓下拉頭向上偏移出屏幕,這樣我們看到的就只有ListView了。然後對ListView的touch事件進行監聽,如果當前ListView已經滾動到頂部並且手指還在向下拉的話,那就將下拉頭顯示出來,鬆手後進行刷新操作,並將下拉頭隱藏。
『叄』 android中listview的下拉刷新上拉載入是怎麼實現的
這是兩個分開的部分。如果你是新手,先一個一個來。
我只能跟你說一下思路,具體的東西你在網上查查,不行再問我,新手的話慢慢來。
1.
下拉刷新,獲取listview的下拉時間顯示header,然後調用更新數據的介面就可以了。
2.
上啦載入,是分頁獲取數據,獲取listview的是否拉到最底,如果拉倒最底,獲取數據,讓後list的數據添加獲取的數據,更新adapter就可以了。
『肆』 android 下拉滾動頁面怎麼實現
以下是我自己花功夫編寫了一種非常簡單的下拉刷新實現方案,現在拿出來和大家分享一下。相信在閱讀完本篇文章之後,大家都可以在自己的項目中一分鍾引入下拉刷新功能 最近項目中需要用到ListView下拉刷新的功能,一開始想圖省事,在網上直接找一個現成的,可是嘗試了網上多個版本的下拉刷新之後發現效果都不 怎麼理想。有些是因為功能不完整或有Bug,有些是因為使用起來太復雜,十全十美的還真沒找到。因此我也是放棄了在網上找現成代碼的想法,自己花功夫編寫 了一種非常簡單的下拉刷新實現方案,現在拿出來和大家分享一下。相信在閱讀完本篇文章之後,大家都可以在自己的項目中一分鍾引入下拉刷新功能。 首先講一下實現原理。這里我們將採取的方案是使用組合View的方式,先自定義一個布局繼承自LinearLayout,然後在這個布局中加入下拉 頭和ListView這兩個子元素,並讓這兩個子元素縱向排列。初始化的時候,讓下拉頭向上偏移出屏幕,這樣我們看到的就只有ListView了。然後對 ListView的touch事件進行監聽,如果當前ListView已經滾動到頂部並且手指還在向下拉的話,那就將下拉頭顯示出來,鬆手後進行刷新操 作,並將下拉頭隱藏。原理示意圖如下: 那我們現在就來動手實現一下,新建一個項目起名叫PullToRefreshTest,先在項目中定義一個下拉頭的布局文件pull_to_refresh/apk/res/android" xmlns:tools="schemas/tools" android:id="@+id/pull_to_refresh_head" android:layout_width="fill_parent" android:layout_height="60dip" > <LinearLayout android:layout_width="200dip" android:layout_height="60dip" android:layout_centerInParent="true" android:orientation="horizontal" > <RelativeLayout android:layout_width="0dip" android:layout_height="60dip" android:layout_weight="3" > <ImageView android:id="@+id/arrow" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:src="@drawable/arrow" /> <ProgressBar android:id="@+id/progress_bar" android:layout_width="30dip" android:layout_height="30dip" android:layout_centerInParent="true" android:visibility="gone" /> </RelativeLayout> <LinearLayout android:layout_width="0dip" android:layout_height="60dip" android:layout_weight="12" android:orientation="vertical" > <TextView android:id="@+id/description" android:layout_width="fill_parent" android:layout_height="0dip" android:layout_weight="1" android:gravity="center_horizontalbottom" android:text="@string/pull_to_refresh" /> <TextView android:id="@+id/updated_at" android:layout_width="fill_parent" android:layout_height="0dip" android:layout_weight="1" android:gravity="center_horizontaltop" android:text="@string/updated_at" /> </LinearLayout> </LinearLayout> </RelativeLayout> 在這個布局中,我們包含了一個下拉指示箭頭,一個下拉狀態文字提示,和一個上次更新的時間。當然,還有一個隱藏的旋轉進度條,只有正在刷新的時候我們才會將它顯示出來。 布局中所有引用的字元串我們都放在stringsmit(); new HideHeaderTask()/apk/res/android" xmlns:tools="schemas/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <com.example.pulltorefreshtest.RefreshableView android:id="@+id/refreshable_view" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ListView android:id="@+id/list_view" android:layout_width="fill_parent" android:layout_height="fill_parent" > </ListView> </com.example.pulltorefreshtest.RefreshableView> </RelativeLayout> 可以看到,我們在自定義的RefreshableView中加入了一個ListView,這就意味著給這個ListView加入了下拉刷新的功能,就是這么簡單! 然後我們再來看一下程序的主Activity,打開或新建MainActivity,加入如下代碼: 復制代碼 代碼如下: public class MainActivity extends Activity { RefreshableView refreshableView; ListView listView; ArrayAdapter<String> adapter; String[] items = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L" }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); refreshableView = (RefreshableView) findViewById(R.id.refreshable_view); listView = (ListView) findViewById(R.id.list_view); adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items); listView.setAdapter(adapter); refreshableView.setOnRefreshListener(new PullToRefreshListener() { @Override public void onRefresh() { try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } refreshableView.finishRefreshing(); } }, 0); } } 可 以看到,我們通過調用RefreshableView的setOnRefreshListener方法注冊了一個監聽器,當ListView正在刷新時就 會回調監聽器的onRefresh方法,刷新的具體邏輯就在這里處理。而且這個方法已經自動開啟了線程,可以直接在onRefresh方法中進行耗時操 作,比如向伺服器請求最新數據等,在這里我就簡單讓線程睡眠3秒鍾。另外在onRefresh方法的最後,一定要調用RefreshableView中的 finishRefreshing方法,這個方法是用來通知RefreshableView刷新結束了,不然我們的ListView將一直處於正在刷新的 狀態。 不知道大家有沒有注意到,setOnRefreshListener這個方法其實是有兩個參數的,我們剛剛也是傳入了一個不起眼的 0。那這第二個參數是用來做什麼的呢?由於RefreshableView比較智能,它會自動幫我們記錄上次刷新完成的時間,然後下拉的時候會在下拉頭中 顯示距上次刷新已過了多久。這是一個非常好用的功能,讓我們不用再自己手動去記錄和計算時間了,但是卻存在一個問題。如果當前我們的項目中有三個地方都使 用到了下拉刷新的功能,現在在一處進行了刷新,其它兩處的時間也都會跟著改變!因為刷新完成的時間是記錄在配置文件中的,由於在一處刷新更改了配置文件, 導致在其它兩處讀取到的配置文件時間已經是更改過的了。那解決方案是什麼?就是每個用到下拉刷新的地方,給setOnRefreshListener方法 的第二個參數中傳入不同的id就行了。這樣各處的上次刷新完成時間都是單獨記錄的,相互之間就不會再有影響。 好了,全部的代碼都在這里了,讓我們來運行一下,看看效果吧。 效果看起來還是非常不錯的。我們最後再來總結一下,在項目中引入ListView下拉刷新功能只需三步: 1. 在Activity的布局文件中加入自定義的RefreshableView,並讓ListView包含在其中。 2. 在Activity中調用RefreshableView的setOnRefreshListener方法注冊回調介面。 3. 在onRefresh方法的最後,記得調用RefreshableView的finishRefreshing方法,通知刷新結束。 從此以後,在項目的任何地方,一分鍾引入下拉刷新功能妥妥的。 好了,今天的講解到此結束,有疑問的朋友請在下面留言。 源碼下載,請點擊這里
『伍』 android recyclerview 怎麼實現上拉載入下拉刷新
代碼如下:
引入
compile 'me.leefeng:lfrecyclerview:1.0.1'
/*找到控制項*/
recycleview = (LFRecyclerView) findViewById(R.id.recycleview);
/*設置屬性*/
recycleview.setLoadMore(true);//設置為可上拉載入,默認 false
recycleview.setRefresh(true);// 設置為可下拉刷新,默認 true
recycleview.setAutoLoadMore(true);//設置滑動到底部自動載入,默認 false
recycleview.setOnItemClickListener(this);// 條目點擊,點擊和長按監聽
recycleview.setLFRecyclerViewListener(this);//下拉刷新上拉載入監聽
recycleview.setScrollChangeListener(this);//滑動監聽
recycleview.hideTimeView();//隱藏時間,默認顯示時間
recycleview.setHeaderView(tv);//設置一個頭部,只有一個大概滿足了多數的要求
recycleview.setNoDateShow();//沒有數據時,底部顯示"沒有數據"字樣,默認不顯示
/*添加適配器*/
adapter=new MainAdapter(list);
recycleview.setAdapter(adapter);
『陸』 android自動下拉刷新autorefresh怎麼使用
一般就是直接在xml文件中,像使用textview一樣設置,然後他會提供一個回調介面的。