pulltorefresh上拉加载 下拉刷新

本文介绍了一个使用PullToRefreshListView实现下拉刷新和上拉加载更多功能的示例。通过MainActivity类展示了如何设置监听器来刷新列表,并在列表滚动到底部时加载更多数据。此外,还介绍了使用AsyncTask进行异步数据加载的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.example.mypulltorefreshdemo;


import java.util.ArrayList;


import android.app.ListActivity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;


import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnLastItemVisibleListener;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

//需要第三方lib包,


public class MainActivity extends ListActivity {
ArrayList<String> dataList = new ArrayList<String>();
private PullToRefreshListView pull_refresh_list;
private ArrayAdapter<String> arrayAdapter;
private int index = 0;
private int TIME_COUNT = 20;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pull_refresh_list = (PullToRefreshListView) findViewById(R.id.pull_refresh_list);


// Set a listener to be invoked when the list should be refreshed.
pull_refresh_list
.setOnRefreshListener(new OnRefreshListener<ListView>() {
@Override
public void onRefresh(
PullToRefreshBase<ListView> refreshView) {
String label = DateUtils.formatDateTime(
getApplicationContext(),
System.currentTimeMillis(),
DateUtils.FORMAT_SHOW_TIME
| DateUtils.FORMAT_SHOW_DATE
| DateUtils.FORMAT_ABBREV_ALL);


// Update the LastUpdatedLabel
refreshView.getLoadingLayoutProxy()
.setLastUpdatedLabel(label);


// Do work to refresh the list here.
new GetDataTask().execute();
}
});


// Add an end-of-list listener
pull_refresh_list
.setOnLastItemVisibleListener(new OnLastItemVisibleListener() {


@Override
public void onLastItemVisible() {
if (index < 100) {
index = index + TIME_COUNT;
initData();
arrayAdapter.notifyDataSetChanged();
} else {
// 做上拉加载的操作
Toast.makeText(MainActivity.this, "End of List!",
Toast.LENGTH_SHORT).show();
}


}
});


initData();
ListView actualListView = pull_refresh_list.getRefreshableView();


arrayAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1,
dataList);
actualListView.setAdapter(arrayAdapter);


}


class GetDataTask extends AsyncTask<Void, Void, ArrayList<String>> {


@Override
protected ArrayList<String> doInBackground(Void... arg0) {
try {


Thread.sleep(4000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return dataList;
}


@Override
protected void onPostExecute(ArrayList<String> result) {
// mListItems.addFirst("Added after refresh...");
index = 0;
dataList.clear();
initData();
arrayAdapter.notifyDataSetChanged();
// Call onRefreshComplete when the list has been refreshed.
pull_refresh_list.onRefreshComplete();
super.onPostExecute(result);
}
}
private void initData() {
for (int i = index; i < index + TIME_COUNT; i++) {
dataList.add("我是第" + i + "条目");
}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值