pulltorefresh上拉加载 下拉刷新
2016-07-29 12:16
211 查看
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 + "条目");
}
}
}
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 + "条目");
}
}
}
相关文章推荐
- PullToRefresh实现下拉刷新和上拉加载
- pulltorefresh(一)使用介绍,实现下拉刷新上拉加载更多
- android ListView上拉加载更多 下拉刷新功能实现(采用pull-to-refresh)
- 使用PullToRefresh实现下拉刷新和上拉加载
- 下拉刷新,上拉加载更多 PullToRefresh
- 使用PullToRefresh实现下拉刷新和上拉加载
- Android-PullToRefresh:下拉刷新和加载更多
- 使用PullToRefresh实现下拉刷新和上拉加载
- android ListView上拉加载更多 下拉刷新功能实现(采用pull-to-refresh)
- Android项目:使用pulltorefresh开源项目扩展为下拉刷新上拉加载更多的处理方法,监听listview滚动方向 推荐
- 使用PullToRefresh实现下拉刷新和上拉加载
- android ListView上拉加载更多 下拉刷新功能实现(采用pull-to-refresh)
- 安卓开发笔记——关于开源组件PullToRefresh实现下拉刷新和上拉加载(一分钟搞定,超级简单)
- PullToRefresh扩展为上拉加载和下拉刷新
- 使用PullToRefresh实现下拉刷新和上拉加载
- Android项目:使用pulltorefresh开源项目扩展为下拉刷新上拉加载更多的处理方法,监听listview滚动方向
- Android项目:使用pulltorefresh开源项目扩展为下拉刷新上拉加载更多的处理方法,监听listview滚动方向
- 《android》下拉刷新上拉加载更多控件 PullToRefreshView
- PullToRefresh实现下拉刷新和上拉加载
- 开源项目PullToRefresh上拉加载、下拉刷新的使用方法整理