pullToRefresh使用(實現listview上拉下拉)
阿新 • • 發佈:2019-02-05
使用pullToRefresh:
1.pullToRefresh的匯入
在https://github.com/chrisbanes/Android-PullToRefresh 下載pullToRefresh
的開原始檔 Android-PullToRefresh-master
將檔案中library匯入工程(點選new按鈕 -> import Module…..庫檔案)
匯入的時候編譯完成可能會報錯,,,,,buildToolsVersion “19.1.0”…compileSdkVersion 16
所以需要下載19版本的buildTools,,,16版本的sdk(自動下載Android SDK Build-Tools 19.1)
2.關聯到當前的應用…新增依賴
Open Module settings–>Dependencies–>新增第三個找到library
(與Xlistview的區別:)
PullToRefreshExpandableListView可重新整理的二級列表
PullToRefreshGridView可以重新整理的GridView
PullToRefreshListView支援listVIEW的重新整理
PullToRefreshWebView可以重新整理的WebView
PullToRefreshScrollView可以重新整理的scrollView
XlistView只支援listView的重新整理
3.佈局設定:
1.在佈局頁面中新增元件,需要使用全類名,沒有提示
<!--
ptr:ptrDrawable="@drawable/default_ptr_flip":重新整理時顯示的圖片
ptr:ptrAnimationStyle="flip":重新整理的圖片以何種方式顯示出來
ptr:ptrHeaderBackground="#383838":重新整理時頭部的佈局
ptr:ptrHeaderTextColor="#FFFFFF":重新整理時頭部字型的顏色
//當下面ptr屬性導不進包時,手動匯入此程式碼
xmlns:ptr="http://schemas.android.com/apk/res-auto"
-->
<com.handmark.pulltorefresh.library.PullToRefreshListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/pull_lv"
ptr:ptrDrawable="@drawable/default_ptr_flip"
ptr:ptrAnimationStyle="flip"
ptr:ptrHeaderBackground="#383838"
ptr:ptrHeaderTextColor="#FFFFFF" >
</com.handmark.pulltorefresh.library.PullToRefreshListView>
自定義方法(在main中引用即可):
public void initLv(){
//設定重新整理模式 ,both代表支援上拉和下拉,pull_from_end代表上拉,pull_from_start代表下拉
pullToRefreshListView.setMode(PullToRefreshBase.Mode.BOTH);
/*這裡通過getLoadingLayoutProxy 方法來指定上拉和下拉時顯示的狀態的區別,第一個true 代表下來狀態 ,第二個true 代表上拉的狀態
如果想區分上拉和下拉狀態的不同,可以分別設定*/
ILoadingLayout startLabels = pullToRefreshListView.getLoadingLayoutProxy(true, false);
startLabels.setPullLabel("下拉重新整理");
startLabels.setRefreshingLabel("正在拉");
startLabels.setReleaseLabel("放開重新整理");
ILoadingLayout endLabels = pullToRefreshListView.getLoadingLayoutProxy(false, true);
endLabels.setPullLabel("上拉重新整理");
endLabels.setRefreshingLabel("正在載入...");
endLabels.setReleaseLabel("放開重新整理...");
/*如果Mode設定成Mode.BOTH,需要設定重新整理Listener為OnRefreshListener2,並實現onPullDownToRefresh()、onPullUpToRefresh()兩個方法。
如果Mode設定成Mode.PULL_FROM_START或Mode.PULL_FROM_END,需要設定重新整理Listener為OnRefreshListener,同時實現onRefresh()方法。
當然也可以設定為OnRefreshListener2,但是Mode.PULL_FROM_START的時候只調用onPullDownToRefresh()方法,Mode.PULL_FROM_END的時候只調用onPullUpToRefresh()方法.
載入資料完成後 必須 呼叫下 onRefreshComplete() 完成關閉 header,footer檢視
*/
pullToRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
@Override
public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {//下拉重新整理的回撥
//下拉重新整理的資料,顯示在listview列表的最上面
if(pageIndex>0)
{
addtoTop();
pageIndex--;
}else{
Toast.makeText(MainActivity.this,"上面沒有資料了",Toast.LENGTH_SHORT).show();
}
myHandler.postDelayed(new Runnable() {
@Override
public void run() {
//重新整理完成,必須在非同步下完成
pullToRefreshListView.onRefreshComplete();
//重新整理介面卡
setAdapter();
}
},1000);
}
@Override
public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {//上拉載入的回撥
//載入更多的資料,新增到集合列表的最後面
addtoBottom();
myHandler.postDelayed(new Runnable() {
@Override
public void run() {
//重新整理完成,必須在非同步下完成
pullToRefreshListView.onRefreshComplete();
//重新整理介面卡
setAdapter();
}
},1000);
}
});
}
載入資料的方法(在main方法中引用即可)
public void initData(){
MyTask myTask=new MyTask(new MyTask.Icallbacks() {
@Override
public void updateUiByjson(String jsonstr) {
Gson gson=new Gson();
Bean bean=gson.fromJson(jsonstr, Bean.class);
//新增到大集合物件中
sjlist.addAll(bean.getNewslist());
//設定介面卡
setAdapter();
}
});
myTask.execute("https://api.tianapi.com/wxnew/?key=8d6e3228d25298f13af4fc40ce6c9679&num=10&page=3");
}
下拉載入的方法:
public void addtoTop(){
MyTask myTask=new MyTask(new MyTask.Icallbacks() {
@Override
public void updateUiByjson(String jsonstr) {
Gson gson=new Gson();
Bean bean=gson.fromJson(jsonstr, Bean.class);
//新增到大集合物件中
sjlist.addAll(0,bean.getNewslist());
//設定介面卡
setAdapter();
}
});
myTask.execute("https://api.tianapi.com/wxnew/?key=8d6e3228d25298f13af4fc40ce6c9679&num=10&page="+pageIndex );
}
上拉重新整理的方法:
public void addtoBottom(){
pageIndex++;
MyTask myTask=new MyTask(new MyTask.Icallbacks() {
@Override
public void updateUiByjson(String jsonstr) {
Gson gson=new Gson();
Bean bean=gson.fromJson(jsonstr, Bean.class);
//新增到大集合物件中
sjlist.addAll(bean.getNewslist());
//設定介面卡
setAdapter();
}
});
myTask.execute("https://api.tianapi.com/wxnew/?key=8d6e3228d25298f13af4fc40ce6c9679&num=10&page="+pageIndex);
}
設定介面卡的方法:
public void setAdapter(){
if(adapter==null){
adapter=new MyAdapter();
pullToRefreshListView.setAdapter(adapter);
}else{
adapter.notifyDataSetChanged();
}
}
Main方法:
private Handler myHandler=new Handler();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pullToRefreshListView = (PullToRefreshListView) findViewById(R.id.plv);
initLv();
initData();
setAdapter();
}
自己定義一個介面卡