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

<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);
}
}
幾行代碼就可以實現(xiàn)下拉刷新,效果也還不錯,不用引入第三方j(luò)ar,唯一的缺憾就是沒有上拉加載,不知道谷歌工程師基于什么方面的考慮,希望能在下個版本看到。不過自己修改下源碼加一個上拉也比較簡單,結(jié)合上個一個版本的刷新效果做成上拉效果還不錯。
二、Android實現(xiàn)上拉加載更多功能以及下拉刷新功能
采用了目前比較火的PullToRefresh,他是目前實現(xiàn)比較好的下拉刷新的類庫。
目前他支持的控件有:ListView, ExpandableListView,GridView,WebView等。
首先第一步當(dāng)然是導(dǎo)入libriay到咱們的項目了,具體導(dǎo)入方式,這里不再贅述。
下面是個例子采用的是ListView,當(dāng)然其余的和這個類似
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、要實現(xiàn)下拉刷新的功能很簡單,只需要實現(xiàn)OnRefreshListener的OnRefresh方法即可。
這里說一下如何實現(xiàn)上拉和下拉分別執(zhí)行不同的操作。
原理是:根據(jù)下拉和上拉顯示的布局的可見狀態(tài)類區(qū)分上拉還是下拉,然后執(zhí)行相應(yīng)操作。
在PullToRefresh的類庫的com.handmark.pulltorefresh.library包下,打開PullToRefreshBase.java,在這個類的最后面添加如下代碼:
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的實現(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)行項目,可以得到演示結(jié)果了。
本文已經(jīng)被整理到《Android下拉刷新上拉加載效果》,歡迎大家學(xué)習(xí)研究。
文章內(nèi)容很豐富,希望對大家學(xué)習(xí)Android實現(xiàn)上拉加載更多以及下拉刷新功能有所幫助。
- Android下拉刷新完全解析,教你如何一分鐘實現(xiàn)下拉刷新功能(附源碼)
- Android下拉刷新ListView——RTPullListView(demo)
- Android PullToRefreshLayout下拉刷新控件的終結(jié)者
- Android中使用RecyclerView實現(xiàn)下拉刷新和上拉加載
- Android下拉刷新上拉加載控件(適用于所有View)
- Android官方下拉刷新控件SwipeRefreshLayout使用詳解
- Android RecyclerView實現(xiàn)下拉刷新和上拉加載
- android開發(fā)教程之實現(xiàn)listview下拉刷新和上拉刷新效果
- Android自定義SwipeRefreshLayout高仿微信朋友圈下拉刷新
- Android實現(xiàn)簡單的下拉刷新控件
相關(guān)文章
appium運(yùn)行各種坑爹報錯問題及解決方法【推薦】
這篇文章主要介紹了 appium運(yùn)行各種坑爹報錯問題及解決方法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2019-06-06
recycleview實現(xiàn)拼多多首頁水平滑動效果
這篇文章主要為大家詳細(xì)介紹了recycleview實現(xiàn)拼多多首頁水平滑動效,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-05-05
Android 使用Vitamio打造自己的萬能播放器(9)—— 在線播放 (在線電視)
本文主要介紹Android 使用Vitamio開發(fā)播放器,實現(xiàn)在線電視播放,這里提供效果圖和實例代碼以便大家參考,2016-07-07
Android入門教程之RecyclerView的具體使用詳解
RecyclerView是Android一個更強(qiáng)大的控件,其不僅可以實現(xiàn)和ListView同樣的效果,還有優(yōu)化了ListView中的各種不足。其可以實現(xiàn)數(shù)據(jù)縱向滾動,也可以實現(xiàn)橫向滾動(ListView做不到橫向滾動)。接下來講解RecyclerView的用法2021-10-10
詳解Android應(yīng)用中使用TabHost組件進(jìn)行布局的基本方法
這篇文章主要介紹了Android應(yīng)用中使用TabHost組件進(jìn)行布局的基本方法,不繼承TabActivity并以最基本的布局文件方式進(jìn)行布局,需要的朋友可以參考下2016-04-04
Android使用View Animation實現(xiàn)動畫加載界面
這篇文章主要為大家詳細(xì)介紹了Android使用View Animation實現(xiàn)動畫加載界面的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-04-04
Flutter?Android多窗口方案落地實戰(zhàn)
這篇文章主要為大家介紹了Flutter?Android多窗口方案落地實戰(zhàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02

