Android實(shí)現(xiàn)上拉加載更多以及下拉刷新功能(ListView)
首先為大家介紹Andorid5.0原生下拉刷新簡(jiǎn)單實(shí)現(xiàn)。
先上效果圖;
相對(duì)于上一個(gè)19.1.0版本中的橫條效果好看了很多。使用起來(lái)也很簡(jiǎn)單。

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:ignore="MergeRootFrame" >
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipe_container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</android.support.v4.widget.SwipeRefreshLayout>
</FrameLayout>
package hi.xiaoyu.swiperefreshlayout;
import hi.xiaoyu.swiperefreshlayout.adapter.TestAdapter;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.widget.ListView;
public class MainActivity extends Activity implements OnRefreshListener {
private SwipeRefreshLayout swipeLayout;
private ListView listView;
private List<String> listDatas;
private TestAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
swipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);
listView = (ListView) findViewById(R.id.list);
swipeLayout.setOnRefreshListener(this);
swipeLayout.setColorSchemeResources(android.R.color.holo_orange_dark,
android.R.color.holo_green_light,
android.R.color.holo_orange_light,
android.R.color.holo_red_light);
listDatas = new ArrayList<String>();
for (int i = 0; i < 10; i++) {
listDatas.add("item" + i);
}
adapter = new TestAdapter(this, listDatas, R.layout.test_item);
listView.setAdapter(adapter);
}
public void onRefresh() {
new Handler().postDelayed(new Runnable() {
public void run() {
swipeLayout.setRefreshing(false);
listDatas.addAll(listDatas);
adapter.notifyDataSetChanged();
}
}, 3000);
}
}
幾行代碼就可以實(shí)現(xiàn)下拉刷新,效果也還不錯(cuò),不用引入第三方j(luò)ar,唯一的缺憾就是沒(méi)有上拉加載,不知道谷歌工程師基于什么方面的考慮,希望能在下個(gè)版本看到。不過(guò)自己修改下源碼加一個(gè)上拉也比較簡(jiǎn)單,結(jié)合上個(gè)一個(gè)版本的刷新效果做成上拉效果還不錯(cuò)。
二、Android實(shí)現(xiàn)上拉加載更多功能以及下拉刷新功能
采用了目前比較火的PullToRefresh,他是目前實(shí)現(xiàn)比較好的下拉刷新的類庫(kù)。
目前他支持的控件有:ListView, ExpandableListView,GridView,WebView等。
首先第一步當(dāng)然是導(dǎo)入libriay到咱們的項(xiàng)目了,具體導(dǎo)入方式,這里不再贅述。
下面是個(gè)例子采用的是ListView,當(dāng)然其余的和這個(gè)類似
1、布局文件activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <com.handmark.pulltorefresh.library.PullToRefreshListView android:id="@+id/pull_refresh_list" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </RelativeLayout>
2、要實(shí)現(xiàn)下拉刷新的功能很簡(jiǎn)單,只需要實(shí)現(xiàn)OnRefreshListener的OnRefresh方法即可。
這里說(shuō)一下如何實(shí)現(xiàn)上拉和下拉分別執(zhí)行不同的操作。
原理是:根據(jù)下拉和上拉顯示的布局的可見(jiàn)狀態(tài)類區(qū)分上拉還是下拉,然后執(zhí)行相應(yīng)操作。
在PullToRefresh的類庫(kù)的com.handmark.pulltorefresh.library包下,打開(kāi)PullToRefreshBase.java,在這個(gè)類的最后面添加如下代碼:
public boolean isHeaderShown() {
return getHeaderLayout().isShown();
}
public boolean isFooterShown() {
return getFooterLayout().isShown();
}
3、在Activity的代碼如下:
public class MainActivity extends ListActivity {
private PullToRefreshListView mPullToRefreshListView;
private LinkedList<String> mItemList;
private ArrayAdapter<String> adapter;
private Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
initData();
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mItemList);
//初始化控件
mPullToRefreshListView = (PullToRefreshListView)findViewById(R.id.pull_refresh_list);
ListView mListView = mPullToRefreshListView.getRefreshableView();
mListView.setAdapter(adapter);
//設(shè)置pull-to-refresh模式為Mode.Both
mPullToRefreshListView.setMode(Mode.BOTH);
//設(shè)置上拉下拉事件
mPullToRefreshListView.setOnRefreshListener(new OnRefreshListener<ListView>() {
@Override
public void onRefresh(PullToRefreshBase<ListView> refreshView) {
if (refreshView.isHeaderShown()){
Toast.makeText(context, "下拉刷新",Toast.LENGTH_SHORT).show();
//下拉刷新 業(yè)務(wù)代碼
}else {
Toast.makeText(context, "上拉加載更多",Toast.LENGTH_SHORT).show();
//上拉加載更多 業(yè)務(wù)代碼
}
}
});
}
private void initData(){
//初始化數(shù)據(jù)
mItemList = new LinkedList<String>();
mItemList.addAll(Arrays.asList(data));
}
private String[] data = new String[]{"data1","data2","data3","data4","data5","data6",
"data1","data2","data3","data4","data5","data6"};
}
如上代碼所示,在OnRefresh的實(shí)現(xiàn)代碼中,用以區(qū)分上拉還是下拉,關(guān)鍵代碼如下:
if (refreshView.isHeaderShown()){
Toast.makeText(context, "下拉刷新",Toast.LENGTH_SHORT).show();
//下拉刷新 業(yè)務(wù)代碼
}else {
Toast.makeText(context, "上拉加載更多",Toast.LENGTH_SHORT).show();
//上拉加載更多 業(yè)務(wù)代碼
}
至此,運(yùn)行項(xiàng)目,可以得到演示結(jié)果了。
本文已經(jīng)被整理到《Android下拉刷新上拉加載效果》,歡迎大家學(xué)習(xí)研究。
文章內(nèi)容很豐富,希望對(duì)大家學(xué)習(xí)Android實(shí)現(xiàn)上拉加載更多以及下拉刷新功能有所幫助。
- Android下拉刷新完全解析,教你如何一分鐘實(shí)現(xiàn)下拉刷新功能(附源碼)
- Android下拉刷新ListView——RTPullListView(demo)
- Android PullToRefreshLayout下拉刷新控件的終結(jié)者
- Android中使用RecyclerView實(shí)現(xiàn)下拉刷新和上拉加載
- Android下拉刷新上拉加載控件(適用于所有View)
- Android官方下拉刷新控件SwipeRefreshLayout使用詳解
- Android RecyclerView實(shí)現(xiàn)下拉刷新和上拉加載
- android開(kāi)發(fā)教程之實(shí)現(xiàn)listview下拉刷新和上拉刷新效果
- Android自定義SwipeRefreshLayout高仿微信朋友圈下拉刷新
- Android實(shí)現(xiàn)簡(jiǎn)單的下拉刷新控件
相關(guān)文章
android開(kāi)發(fā)socket編程之udp發(fā)送實(shí)例分析
這篇文章主要介紹了android開(kāi)發(fā)socket編程之udp發(fā)送,實(shí)例分析了Android開(kāi)發(fā)socket網(wǎng)絡(luò)編程中udp發(fā)送的相關(guān)技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04
eclipse導(dǎo)入appcompat項(xiàng)目報(bào)錯(cuò)解決辦法
這篇文章主要介紹了eclipse導(dǎo)入appcompat項(xiàng)目報(bào)錯(cuò)解決辦法的相關(guān)資料,需要的朋友可以參考下2017-04-04
Android編程實(shí)現(xiàn)文字倒影效果的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)文字倒影效果的方法,涉及Android布局與圖形繪制相關(guān)操作技巧,需要的朋友可以參考下2017-03-03
Android編程實(shí)現(xiàn)只顯示圖片一部分的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)只顯示圖片一部分的方法,涉及Android針對(duì)圖片的局部顯示操作技巧,需要的朋友可以參考下2016-10-10
Android TextView前增加紅色必填項(xiàng)星號(hào)*的示例代碼
TextView是一個(gè)完整的文本編輯器,但是基類為不允許編輯,其子類EditText允許文本編輯,這篇文章主要介紹了Android TextView前增加紅色必填項(xiàng)星號(hào)*的示例代碼,需要的朋友可以參考下2024-03-03
Android onSaveInstanceState和onRestoreInstanceState觸發(fā)的時(shí)機(jī)
這篇文章主要介紹了Android onSaveInstanceState和onRestoreInstanceState觸發(fā)的時(shí)機(jī)的相關(guān)資料,需要的朋友可以參考下2017-05-05
android 下載時(shí)文件名是中文和空格會(huì)報(bào)錯(cuò)解決方案
項(xiàng)目中遇到了下載文件文件名是中文而且還有空格如果不對(duì)連接進(jìn)行處理下載就會(huì)報(bào)錯(cuò)要想解決這個(gè)問(wèn)題只需對(duì)你的url進(jìn)行編碼然后替換空格用編碼表示,感興趣的朋友可以詳細(xì)了解下2013-01-01
Android仿微信Viewpager-Fragment惰性加載(lazy-loading)
這篇文章主要為大家詳細(xì)介紹了Android仿微信Viewpager-Fragment惰性加載lazy-loading,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08

