现在不论是微博还是什么,很多app都有上拉加载和下拉刷新,这个效果PullToRefresh
就可以实现。
下拉刷新:
上拉加载:
这个东西不仅仅是ListView,很多都可以适用。
只要把这些都当作你正常的ListView,GridView使用就好了。
全包名:< com.handmark.pulltorefresh.library.PullToRefreshListView
需要导入第三方library,产生依赖,项目中属性中设置。和上次的抽屉效果
SlidingMenu一样,不知道的可以看上一篇,同样将library导入项目。
<com.handmark.pulltorefresh.library.PullToRefreshListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/main_plv"
></com.handmark.pulltorefresh.library.PullToRefreshListView>
PullToRefresh的使用:
1、设置模式(上拉或下拉,或都有)
plv.setMode(Mode.BOTH);
2、设置头和尾。
getLoadingLayoutProxy(boolean includeStart, boolean includeEnd)
第一个为true返回的是头,第二个为true返回的是尾。
setLastUpdatedLabel 设置最后加载(刷新)的提示(一般是时间)
setLoadingDrawable 设置加载(刷新)的图标
setPullLabel 设置下拉(上拉)的提示
setRefreshingLabel 设置正在刷新(加载)的提示
setReleaseLabel 设置释放刷新(加载)的提示
和ListView一样,同样有监听。
plv.setOnRefreshListener需要重写的两个方法:
1.onPullDownToRefresh 下拉刷新
2.onPullUpToRefresh 上拉加载
通常是从网上获取数据,有耗时操作需要放在子线程当中,使用Handler机制。
我们来实现刚才的效果。
Mainactivity:
package com.example.pulltorefreshtest;
import java.util.ArrayList;
import java.util.List;
import com.handmark.pulltorefresh.library.ILoadingLayout;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends Activity implements OnRefreshListener2<ListView> {
private PullToRefreshListView plv;
private List<String> datas;
private ArrayAdapter<String> adapter;
private Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
if (msg.what == 10) {
plv.onRefreshComplete();
adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, datas);
plv.setAdapter(adapter);
}
if (msg.what == 11) {
plv.onRefreshComplete();
adapter.notifyDataSetChanged();
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
initDatas();
initListener();
}
private void initListener() {
// TODO Auto-generated method stub
plv.setOnRefreshListener(this);
}
private void initDatas() {
// TODO Auto-generated method stub
datas = new ArrayList<String>();
for (int i = 1; i < 16; i++) {
datas.add("Item " + i);
}
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, datas);
plv.setMode(Mode.BOTH);
initHeaderAndFooter();
plv.setAdapter(adapter);
}
private void initHeaderAndFooter() {
// TODO Auto-generated method stub
ILoadingLayout header=plv.getLoadingLayoutProxy(true, false);
header.setLastUpdatedLabel("最后一次加载");
Drawable drawable=getResources().getDrawable(R.drawable.default_ptr_rotate);
header.setLoadingDrawable(drawable);
header.setPullLabel("下拉刷新!");
header.setRefreshingLabel("正在刷新...");
header.setReleaseLabel("放手刷新~");
ILoadingLayout footer=plv.getLoadingLayoutProxy(false,true);
footer.setLastUpdatedLabel("最后一次加载");
footer.setLoadingDrawable(drawable);
footer.setPullLabel("下拉刷新!");
footer.setRefreshingLabel("正在刷新...");
footer.setReleaseLabel("放手刷新~");
}
private void initViews() {
// TODO Auto-generated method stub
plv = (PullToRefreshListView) findViewById(R.id.main_plv);
}
@Override
public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
// TODO Auto-generated method stub
new Thread() {
@Override
public void run() {
// TODO Auto-generated method stub
changedatas();
handler.sendEmptyMessage(10);
}
}.start();
}
@Override
public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
// TODO Auto-generated method stub
new Thread() {
public void run() {
addDatas();
handler.sendEmptyMessage(11);
};
}.start();
;
}
private void addDatas() {
// TODO Auto-generated method stub
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for (int i = 16; i < 31; i++) {
datas.add("Item " + i);
}
}
protected void changedatas() {
// TODO Auto-generated method stub
datas.clear();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for (int i = 16; i < 31; i++) {
datas.add("Item " + i);
}
}
}
为了模拟耗时操作,特意让线程睡眠来达到子线程的耗时操作。