android下拉刷新,上拉获取更多
来源:互联网 发布:薛之谦 上海炮王 知乎 编辑:程序博客网 时间:2024/05/07 18:42
很经典的功能,可以尝试去写写,项目赶的很,就借用了别人的。github上搜索的,选择了 chrisbanes/Android-PullToRefresh(https://github.com/chrisbanes/Android-PullToRefresh)。效果实现了,在此记录下。
1、配置环境,这个比较简单,下载后作为一个library直接导入;
2、实现一个下拉刷新和上拉获取更多,(借鉴自带的一个listview):
MainActivity.java
package com.ttdevs.pullrefresh;import java.util.Arrays;import java.util.LinkedList;import android.app.ListActivity;import android.os.AsyncTask;import android.os.Bundle;import android.widget.ArrayAdapter;import android.widget.ListView;import android.widget.Toast;import com.handmark.pulltorefresh.library.PullToRefreshBase;import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;import com.handmark.pulltorefresh.library.PullToRefreshListView;public class MainActivity extends ListActivity {private PullToRefreshListView mPullRefreshListView;private LinkedList<String> mListItems;private ArrayAdapter<String> mAdapter;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mPullRefreshListView = (PullToRefreshListView) findViewById(R.id.pull_refresh_list);mPullRefreshListView.setMode(Mode.BOTH);mPullRefreshListView.setOnRefreshListener(new OnRefreshListener2<ListView>() {@Overridepublic void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {Toast.makeText(getApplicationContext(), "下拉刷新", Toast.LENGTH_LONG).show();new GetDataTask().execute();}@Overridepublic void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {Toast.makeText(getApplicationContext(), "上拉获取更多", Toast.LENGTH_LONG).show();new GetDataTask().execute();}});ListView actualListView = mPullRefreshListView.getRefreshableView();// Need to use the Actual ListView when registering for Context MenuregisterForContextMenu(actualListView);mListItems = new LinkedList<String>();mListItems.addAll(Arrays.asList(mStrings));mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mListItems);actualListView.setAdapter(mAdapter);}private class GetDataTask extends AsyncTask<Void, Void, String[]> {@Overrideprotected String[] doInBackground(Void... params) {try {Thread.sleep(1000);} catch (InterruptedException e) {}return mStrings;}@Overrideprotected void onPostExecute(String[] result) {mListItems.addFirst("Added after refresh...");mAdapter.notifyDataSetChanged();mPullRefreshListView.onRefreshComplete();super.onPostExecute(result);}}private String[] mStrings = { "Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam", "Abondance", "Ackawi","Acorn", "Adelost", "Affidelice au Chablis", "Afuega'l Pitu", "Airag", "Airedale", "Aisy Cendre","Allgauer Emmentaler", "Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam", "Abondance", "Ackawi","Acorn", "Adelost", "Affidelice au Chablis", "Afuega'l Pitu", "Airag", "Airedale", "Aisy Cendre","Allgauer Emmentaler" };}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <!-- The PullToRefreshListView replaces a standard ListView widget. --> <com.handmark.pulltorefresh.library.PullToRefreshListView android:id="@+id/pull_refresh_list" android:layout_width="match_parent" android:layout_height="match_parent" android:cacheColorHint="#00000000" android:divider="#19000000" android:dividerHeight="4dp" android:fadingEdge="none" android:fastScrollEnabled="false" android:footerDividersEnabled="false" android:headerDividersEnabled="false" android:smoothScrollbar="true" /></LinearLayout>
3、主要是拷贝demo中的listview那个,实现的监听器是OnRefreshListener2,看了下源码,OnRefreshListener和OnRefreshListener2只可以设置一个,也就是最后设置的那个有效,最后就是设置滑动的模式:mPullRefreshListView.setMode(Mode.BOTH);这个很重要,否则上拉无效,最后效果如下:
4、Chrisbanes的PullToRefresh:下载
- android下拉刷新,上拉获取更多
- Android ListView 下拉刷新 上拉更多
- android listview 下拉刷新,上拉更多
- Android ListView 下拉刷新 上拉更多
- android 上拉刷新,下拉加载更多
- 下拉刷新、上拉获取更多、左右滑动的ListView
- 可以下拉刷新、上拉获取更多的SwipeRefreshAndLoadLayout
- Android ListView 下拉刷新 上拉更多[实例]
- Android ListView 下拉刷新 上拉更多[实例]
- Android ListView下拉刷新、上拉载入更多
- Android ListView下拉刷新、上拉载入更多[转]
- android ListView下拉刷新上拉加载更多
- Android下拉刷新上拉加载更多ListView控件
- Android中ListView下拉刷新、上拉载入更多示例
- android 下拉刷新上拉加载更多(即微博效果)
- android下拉刷新上拉加载更多通用Demo
- Android ListView下拉刷新上拉加载更多的实现
- Android ListView 下拉刷新 上拉更多[实例]
- java网络开发中的数据转换问题
- linux 下C语言实现 读取网卡速度 .
- COCOS2D-X之精灵移动到所点击的位置Demo
- linux下cpio.gz文件的解压方法
- 索引查找简单例子
- android下拉刷新,上拉获取更多
- Lost Cows
- 校招季——编程题目(15、16) 约瑟夫问题 最大子矩阵和
- 【不满丈夫找小三,妻子一怒之下卖掉酒楼回了娘家】
- 用JavaScript动态创建一个100行10列的表格。
- 27. 不只是学语言,更要领悟其文化
- 统计大于均值的数字个数
- 本学期规划2013-9-7
- OpenStack Ceilometer简介