您的位置:首页 > 移动开发 > Android开发

Android学习之开源项目PullToRefresh的使用

2016-01-19 16:54 477 查看


首先 下载 Android-PullToRefresh-master



下载地址 https://github.com/chrisbanes/Android-PullToRefresh
下载之后将其解压



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

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

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

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

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

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

view
sourceprint?

01.
<?xml
version=
"1.0"
encoding=
"utf-8"
?>


02.
<LinearLayout
xmlns:android=
"http://schemas.android.com/apk/res/android"


03.
android:layout_width=
"match_parent"


04.
android:layout_height=
"match_parent"


05.
android:background=
"#FFFFFF"


06.
android:orientation=
"vertical"
>


07.


08.
<!--
xmlns:ptr =
"http://schemas.android.com/apk/res-auto"
为我们要使用PullToRefresh
里面一些属性需要引的命名空间 -->


09.
<com.handmark.pulltorefresh.library.PullToRefreshListView


10.
xmlns:ptr
=
"http://schemas.android.com/apk/res-auto"


11.
android:id=
"@+id/pull_refresh_list"


12.
android:layout_width=
"fill_parent"


13.
android:layout_height=
"fill_parent"


14.
android:dividerHeight=
"4dp"


15.
android:fadingEdge=
"none"


16.
android:fastScrollEnabled=
"false"


17.
android:footerDividersEnabled=
"false"


18.
android:headerDividersEnabled=
"false"


19.
android:smoothScrollbar=
"true"


20.
ptr:ptrMode=
"both"


21.
/>


22.
</LinearLayout>


接着创建 MainActivity.java

view
sourceprint?

001.
package
com.pulltorefresh;


002.


003.
import
java.util.Arrays;


004.
import
java.util.LinkedList;


005.


006.
import
android.app.Activity;


007.
import
android.os.AsyncTask;


008.
import
android.os.Bundle;


009.
import
android.widget.ArrayAdapter;


010.
import
android.widget.ListView;


011.
import
android.widget.Toast;


012.


013.
import
com.handmark.pulltorefresh.library.PullToRefreshBase;


014.
import
com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;


015.
import
com.handmark.pulltorefresh.library.PullToRefreshBase.State;


016.
import
com.handmark.pulltorefresh.library.PullToRefreshListView;


017.
import
com.handmark.pulltorefresh.library.extras.SoundPullEventListener;


018.


019.


020.


021.
public
class
MainActivity
extends
Activity
{


022.


023.


024.
static
final
int
MENU_MANUAL_REFRESH
=
0
;


025.
static
final
int
MENU_DISABLE_SCROLL
=
1
;


026.
static
final
int
MENU_SET_MODE
=
2
;


027.
static
final
int
MENU_DEMO
=
3
;


028.


029.
private
LinkedList<String>
mListItems;


030.
private
PullToRefreshListView
mPullRefreshListView;


031.
private
ArrayAdapter<String>
mAdapter;


032.


033.
@Override


034.
protected
void
onCreate(Bundle
savedInstanceState) {


035.
super
.onCreate(savedInstanceState);


036.
setContentView(R.layout.main);


037.
mPullRefreshListView
= (PullToRefreshListView) findViewById(R.id.pull_refresh_list);


038.


039.


040.
/**


041.
*
实现 接口  OnRefreshListener2<ListView>  以便与监听  滚动条到顶部和到底部


042.
*/


043.
mPullRefreshListView.setOnRefreshListener(
new
OnRefreshListener2<ListView>()
{


044.
@Override


045.
public
void
onPullDownToRefresh(
PullToRefreshBase<ListView> refreshView) {


046.
Toast.makeText(MainActivity.
this
,
"onPullDownToRefresh"
,
Toast.LENGTH_SHORT).show();


047.
new
GetDataTask().execute();


048.
}


049.
@Override


050.
public
void
onPullUpToRefresh(
PullToRefreshBase<ListView> refreshView) {


051.
Toast.makeText(MainActivity.
this
,
"onPullUpToRefresh"
,
Toast.LENGTH_SHORT).show();


052.
new
GetDataTask().execute();


053.
}


054.
});


055.


056.


057.


058.
ListView
actualListView = mPullRefreshListView.getRefreshableView();


059.


060.
//
Need to use the Actual ListView when registering for Context Menu


061.
registerForContextMenu(actualListView);


062.


063.
mListItems
=
new
LinkedList<String>();


064.
mListItems.addAll(Arrays.asList(mStrings));


065.


066.
mAdapter
=
new
ArrayAdapter<String>(
this
,
android.R.layout.simple_list_item_1,mListItems);


067.


068.
/**


069.
*
Add Sound Event Listener


070.
*/


071.


072.
/**


073.
*
设置下拉刷新和上拉加载时的 铃声(可有可无)


074.
*/


075.
SoundPullEventListener<ListView>
soundListener =
new
SoundPullEventListener<ListView>(
this
);


076.
soundListener.addSoundEvent(State.PULL_TO_REFRESH,
R.raw.pull_event);


077.
soundListener.addSoundEvent(State.RESET,
R.raw.reset_sound);


078.
soundListener.addSoundEvent(State.REFRESHING,
R.raw.refreshing_sound);


079.
mPullRefreshListView.setOnPullEventListener(soundListener);


080.


081.
//
You can also just use setListAdapter(mAdapter) or


082.
//
mPullRefreshListView.setAdapter(mAdapter)


083.
actualListView.setAdapter(mAdapter);


084.


085.


086.


087.
}


088.
//模拟网络加载数据的
异步请求类


089.
//


090.
private
class
GetDataTask
extends
AsyncTask<Void,
Void,String[]> {


091.


092.
//子线程请求数据


093.
@Override


094.
protected
String[]
doInBackground(Void... params) {


095.
//
Simulates a background job.


096.
try
{


097.
Thread.sleep(
10
);


098.
}
catch
(InterruptedException
e) {


099.
}


100.
return
mStrings;


101.
}


102.


103.
//主线程更新UI


104.
@Override


105.
protected
void
onPostExecute(String[]
result) {


106.


107.
//向RefreshListView
Item 添加一行数据  并刷新ListView


108.
//mListItems.addLast("Added
after refresh...");


109.
mListItems.addFirst(
"Added
after refresh..."
);


110.
mAdapter.notifyDataSetChanged();


111.


112.
//通知RefreshListView
我们已经更新完成


113.
//
Call onRefreshComplete when the list has been refreshed.


114.
mPullRefreshListView.onRefreshComplete();


115.


116.
super
.onPostExecute(result);


117.
}


118.
}


119.


120.


121.


122.
//数据源


123.
private
String[]
mStrings = {
"Abbaye
de Belloc"
,
"Abbaye
du Mont des Cats"
,
"Abertam"
,
"Abondance"
,
"Ackawi"
,


124.
"Acorn"
,
"Adelost"
,
"Affidelice
au Chablis"
,
"Afuega'l
Pitu"
,
"Airag"
,
"Airedale"
,
"Aisy
Cendre"
,


125.
"Allgauer
Emmentaler"
,
"Abbaye
de Belloc"
,
"Abbaye
du Mont des Cats"
,
"Abertam"
,
"Abondance"
,
"Ackawi"
,


126.
"Acorn"
,
"Adelost"
,
"Affidelice
au Chablis"
,
"Afuega'l
Pitu"
,
"Airag"
,
"Airedale"
,
"Aisy
Cendre"
,


127.
"Allgauer
Emmentaler"
};


128.
}


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





到此我们学习完成
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android