PullToRefresh使用
来源:互联网 发布:个人淘宝店铺转让 编辑:程序博客网 时间:2024/05/22 15:37
布局:
<?xml version="1.0" encoding="utf-8"?>
<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">
<com.handmark.pulltorefresh.library.PullToRefreshListView xmlns:ptr="http://schemas.android.com/apk/res-auto"
android:id="@+id/refresh_list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
ptr:ptrAnimationStyle="flip"
ptr:ptrDrawable="@drawable/default_ptr_flip"
ptr:ptrHeaderBackground="#383838"
ptr:ptrHeaderTextColor="#FFFFFF"
/>
</RelativeLayout>
代码:
package com.example.a16_pulltorefresh_demo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.handmark.pulltorefresh.library.ILoadingLayout;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private PullToRefreshListView refreshListView;
private List<String> list = new ArrayList<>();
private ArrayAdapter<String> adapter;
private ILoadingLayout startLabels;
private ILoadingLayout endLabels;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
refreshListView = (PullToRefreshListView) findViewById(R.id.refresh_list_view);
//获取数据,,,设置适配器
getData();
//设置刷新时显示的刷新状态
/**设置pullToRefreshListView的刷新模式,BOTH代表支持上拉和下拉,
PULL_FROM_END代表上拉,PULL_FROM_START代表下拉 */
refreshListView.setMode(PullToRefreshBase.Mode.BOTH);
/**
* 设置下拉和上拉的时候显示的文字
*/
//通过getLoadingLayoutProxy 方法来指定上拉和下拉时显示的文字的区别,第一个true 代表下来状态 ,第二个true 代表上拉的状态
startLabels = refreshListView.getLoadingLayoutProxy(true, false);
startLabels.setPullLabel("下拉刷新");
startLabels.setRefreshingLabel("正在刷新...");
startLabels.setReleaseLabel("放开刷新");
endLabels = refreshListView.getLoadingLayoutProxy(false, true);
endLabels.setPullLabel("上拉刷新");
endLabels.setRefreshingLabel("正在载入...");
endLabels.setReleaseLabel("放开刷新...");
//设置上拉下拉的监听事件
/**
* 当然也可以设置为OnRefreshListener2,但是Mode.PULL_FROM_START的时候只
* 调用onPullDownToRefresh()方法,
* Mode.PULL_FROM_END的时候只调用onPullUpToRefresh()方法.
*/
refreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
//下拉的时候调用
@Override
public void onPullDownToRefresh(final PullToRefreshBase<ListView> refreshView) {
//加载数据(异步的操作)....数据完成之后,取消刷新
//执行onRefreshComplete();方法必须在异步下执行,不能和主进程一起执行
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... voids) {
try {
//表示两秒钟请求到了数据
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
getData();
//下拉刷新设置刷新的时间
startLabels.setLastUpdatedLabel("上次刷新:2017-09-14");
//停止刷新
refreshView.onRefreshComplete();
super.onPostExecute(aVoid);
}
}.execute();
}
//上拉的时候调用
@Override
public void onPullUpToRefresh(final PullToRefreshBase<ListView> refreshView) {
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... voids) {
try {
//表示两秒钟请求到了数据
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
getData();
endLabels.setLastUpdatedLabel("上次加载时间:09-15");
//停止刷新
refreshView.onRefreshComplete();
super.onPostExecute(aVoid);
}
}.execute();
}
});
}
private void getData() {
for (int i = 0;i<10;i++){
list.add("这是条目"+i);
}
if (adapter == null){
adapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,android.R.id.text1,list);
refreshListView.setAdapter(adapter);
}else {
adapter.notifyDataSetChanged();
}
}
}
- PullToRefresh 使用
- PullToRefresh使用
- PullToRefresh的使用
- Android-PullToRefresh 使用心得
- Android-PullToRefresh 使用心得
- ActionBar-pulltorefresh使用教程
- PullToRefresh使用详解
- PullToRefresh使用详解
- ActionBar-PullToRefresh使用
- pulltorefresh的使用
- PullToRefresh使用详解
- ActionBar-PullToRefresh的使用
- PullToRefresh的简单使用
- ActionBar-PullToRefresh的使用
- Android-PullToRefresh 使用心得
- PullToRefresh使用详解
- Android PullToRefresh 使用详解
- Android-PullToRefresh使用
- Android Notification 开发经验分享
- 设备识别跳转电脑端自动跳转手机端js
- Unity_虚拟现实_自我见解
- <33>——Search in Rotated Sorted Array
- 三秒钟后跳转
- PullToRefresh使用
- 关于mysql inner join 连接查询的优化
- 什么是关联挖掘?如何利用顾客的购物习惯提高销售额?
- wx.saveImageToPhotosAlbum将图片保存到本地
- CSS基础2-悬停
- AOP面向切面编程
- 南阳理工OJ915解题报告
- FTPrep, 80 Remove Duplicates from Sorted Array II
- qt 创建插件和使用