博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android 学习之 开源项目PullToRefresh的使用
阅读量:4582 次
发布时间:2019-06-09

本文共 4302 字,大约阅读时间需要 14 分钟。

首先 下载 Android-PullToRefresh-master

下载地址  

下载之后将其解压

现在  我们用eclipse 创建一个项目取名PullToRefresh 

将上面的library 引入我们的项目

引入成功之后打开项目的project.properties文件我们可以看到

android.library.reference.1=../Android-PullToRefresh-master/library

这样就表示可以引用成功了

我们在res/layout创建 布局文件main.xml

接着创建 MainActivity.java

package com.pulltorefresh;import java.util.Arrays;import java.util.LinkedList;import android.app.Activity;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.OnRefreshListener2;import com.handmark.pulltorefresh.library.PullToRefreshBase.State;import com.handmark.pulltorefresh.library.PullToRefreshListView;import com.handmark.pulltorefresh.library.extras.SoundPullEventListener;public class MainActivity extends Activity {            static final int MENU_MANUAL_REFRESH = 0;    static final int MENU_DISABLE_SCROLL = 1;    static final int MENU_SET_MODE = 2;    static final int MENU_DEMO = 3;        private LinkedList
mListItems; private PullToRefreshListView mPullRefreshListView; private ArrayAdapter
mAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mPullRefreshListView = (PullToRefreshListView) findViewById(R.id.pull_refresh_list); /** * 实现 接口 OnRefreshListener2
以便与监听 滚动条到顶部和到底部 */ mPullRefreshListView.setOnRefreshListener(new OnRefreshListener2
() { @Override public void onPullDownToRefresh( PullToRefreshBase
refreshView) { Toast.makeText(MainActivity.this, "onPullDownToRefresh", Toast.LENGTH_SHORT).show(); new GetDataTask().execute(); } @Override public void onPullUpToRefresh( PullToRefreshBase
refreshView) { Toast.makeText(MainActivity.this, "onPullUpToRefresh", Toast.LENGTH_SHORT).show(); new GetDataTask().execute(); } }); ListView actualListView = mPullRefreshListView.getRefreshableView(); // Need to use the Actual ListView when registering for Context Menu registerForContextMenu(actualListView); mListItems = new LinkedList
(); mListItems.addAll(Arrays.asList(mStrings)); mAdapter = new ArrayAdapter
(this, android.R.layout.simple_list_item_1, mListItems); /** * Add Sound Event Listener */ /** * 设置下拉刷新和上拉加载时的 铃声(可有可无) */ SoundPullEventListener
soundListener = new SoundPullEventListener
(this); soundListener.addSoundEvent(State.PULL_TO_REFRESH, R.raw.pull_event); soundListener.addSoundEvent(State.RESET, R.raw.reset_sound); soundListener.addSoundEvent(State.REFRESHING, R.raw.refreshing_sound); mPullRefreshListView.setOnPullEventListener(soundListener); // You can also just use setListAdapter(mAdapter) or // mPullRefreshListView.setAdapter(mAdapter) actualListView.setAdapter(mAdapter); } //模拟网络加载数据的 异步请求类 // private class GetDataTask extends AsyncTask
{ //子线程请求数据 @Override protected String[] doInBackground(Void... params) { // Simulates a background job. try { Thread.sleep(10); } catch (InterruptedException e) { } return mStrings; } //主线程更新UI @Override protected void onPostExecute(String[] result) { //向RefreshListView Item 添加一行数据 并刷新ListView //mListItems.addLast("Added after refresh..."); mListItems.addFirst("Added after refresh..."); mAdapter.notifyDataSetChanged(); //通知RefreshListView 我们已经更新完成 // Call onRefreshComplete when the list has been refreshed. 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" };}

目前编码已经完成  我们测试一下

 

转载于:https://www.cnblogs.com/zhujiabin/p/5323584.html

你可能感兴趣的文章
mfix中更改time dependent VTK filename的最大时间步数的容量
查看>>
Windows7安装 docker-compose的过程
查看>>
关于nodeJS多线程的支持,目前看来无法实现,讲解v8的一些东西
查看>>
php递归创建文件夹的两种方法
查看>>
6.新增事件
查看>>
|洛谷|二分|P1182 数列分段Section II
查看>>
少儿编程Scratch第四讲:射击游戏的制作,克隆的奥秘
查看>>
Oracle学习第七课-表连接及其应用
查看>>
Python基础篇【第十三篇】:面向对象
查看>>
bzoj 2465 小球
查看>>
Study Plan - The Thirty-Fifth Day
查看>>
图的深度优先遍历和广度优先遍历理解
查看>>
multi_index_container性能测试
查看>>
【阿里云产品公测】结构化数据服务OTS之JavaSDK初体验
查看>>
AngularJs学习笔记--IE Compatibility 兼容老版本IE
查看>>
sql server还原数据库文件(.bak)常见问题解决办法笔记
查看>>
列表,元组,字典的常规操作及内置方法
查看>>
LayoutInflater介绍及例子
查看>>
python中星号变量的几种特殊用法
查看>>
centreon 画图x轴乱码
查看>>