以后会经常用到下拉刷新和上拉刷新的,今天用了,就记录一下,方便以后的使用。
下载地址:https://github.com/chrisbanes/Android-PullToRefresh
1.导包
2.xml布局:
<LinearLayout 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
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>3.Activity中:
public class MainActivity extends Activity implements
OnRefreshListener<ListView> {
private MyHandler handler = new MyHandler();
/** 从第三页开始显示吧 */
private int current_head_index = 3;
/** 后面是第三页开始 */
private int current_foot_index = 4;
private PullToRefreshListView mPullRefreshListView;
/** 数据集合 */
private ArrayList<String> data;
/** 简单的适配器 */
private ArrayAdapter<String> adapter;
private boolean ishead = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mPullRefreshListView = (PullToRefreshListView) findViewById(R.id.listview);
data = new ArrayList<String>();
addData();
adapter = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_list_item_1, data);
mPullRefreshListView.setOnRefreshListener(this);
/** 上下都刷新 */
mPullRefreshListView.setMode(Mode.BOTH);
mPullRefreshListView.getLoadingLayoutProxy(false, true).setPullLabel(
"下拉中");
mPullRefreshListView.getLoadingLayoutProxy(false, true)
.setRefreshingLabel("刷新中");
mPullRefreshListView.getLoadingLayoutProxy(false, true)
.setReleaseLabel("释放了");
/** 需要listView */
ListView actualListView = mPullRefreshListView.getRefreshableView();
actualListView.setAdapter(adapter);
actualListView.setOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
// TODO Auto-generated method stub
/** 如果第一条看得见 就下拉刷新 */
if (firstVisibleItem == 0) {
ishead = true;
} else {
ishead = false;
}
}
});
}
private void addData() {
if (current_head_index >= 1) {
if (ishead) {
int tempIndex = current_head_index * 10;
for (int i = tempIndex; i > tempIndex - 10; i--) {
data.add(0, i + "");
}
current_head_index--;
} else {
int tempIndex = current_foot_index * 10;
for (int i = tempIndex - 10; i < tempIndex; i++) {
data.add(i + "");
}
current_foot_index++;
}
}
}
@Override
public void onRefresh(PullToRefreshBase<ListView> refreshView) {
// TODO Auto-generated method stub
new MyThread().start();
}
private class MyThread extends Thread {
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
try {
sleep(3000);
/** 让主线程更新UI */
Message msg = handler.obtainMessage();
handler.sendMessage(msg);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private class MyHandler extends Handler {
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
/** 更新数据 */
addData();
adapter.notifyDataSetChanged();
mPullRefreshListView.onRefreshComplete();
}
}
}
记得以下几个点:
1.用一个变量来标志请求数据是上拉还是下拉(在listView的setOnScrollListener() 的监听器中来监听是下拉还是上拉,如果第一条看见了,肯定下拉,前提是第一次的条目要很多,使得第一条会滚出屏幕!!!)
2.上拉的话,数据记得加在0处,下拉就直接加在后面了
3.差点忘记了,那个ListView 是这样得到的
ListView actualListView = mPullRefreshListView.getRefreshableView();
4.记得
adapter.notifyDataSetChanged();
mPullRefreshListView.onRefreshComplete();
5.明天还要做别的不知道能不能搞定呢??
项目和支持库下载地址:
http://download.youkuaiyun.com/detail/yymonkeydo/8121035
listView代码中配置:
//滚动条的宽度
istView.setScrollBarSize(3);
//滚动条的背景图片为空
listView.setDivider(null);
//条目之间的距离
listView.setDividerHeight(10);
292

被折叠的 条评论
为什么被折叠?



