Android 自定义ListView实现底部分页刷新与顶部下拉刷新,androidlistview
在项目开发中,由于数据过大时,需要进行分页加载或下拉刷新,来缓解一次性加载的过长等待。本篇博文实例讲解通过自定义的ListView实现底部分页加载和顶部下拉刷新的效果。
其效果图:
一.ListView 底部分页加载
整个底部分页加载,主要分一下几步:
1.加载底部自定义View;
2.响应OnScrollListener监听事件,onScroll方法记录最后可见的View Item以及整个totalItemCount。当onScrollStateChanged状态改变时,
当滑动到底端,并滑动状态为 SCROLL_STATE_IDLE,显示底部加载View,开始实现自定义加载接口;
3.当数据加载完成时,隐藏底部下拉View.
自定义底部下拉加载PaginationListView代码如下:
<span style="font-family:KaiTi_GB2312;font-size:18px;">package com.example.paginationrefreshlistdemo.listview; import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ListView; import com.example.paginationrefreshlistdemo.R; public class PaginationListView extends ListView implements OnScrollListener{
//底部View
private View footerView;
//ListView item个数
int totalItemCount = 0;
//最后可见的Item
int lastVisibleItem = 0;
//是否加载标示
boolean isLoading = false; public PaginationListView(Context context) {
super(context);
initView(context);
} public PaginationListView(Context context, AttributeSet attrs) {
super(context, attrs);
initView(context); } public PaginationListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initView(context);
} /**
* 初始化ListView
*/
private void initView(Context context){
LayoutInflater mInflater = LayoutInflater.from(context);
footerView = mInflater.inflate(R.layout.footer, null);
footerView.setVisibility(View.GONE);
this.setOnScrollListener(this);
//添加底部View
this.addFooterView(footerView);
} @Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
//当滑动到底端,并滑动状态为 not scrolling
if(lastVisibleItem == totalItemCount && scrollState == SCROLL_STATE_IDLE){
if(!isLoading){
isLoading = true;
//设置可见
footerView.setVisibility(View.VISIBLE);
//加载数据
onLoadListener.onLoad();
}
}
} @Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
this.lastVisibleItem = firstVisibleItem + visibleItemCount;
this.totalItemCount = totalItemCount;
} private OnLoadListener onLoadListener;
public void setOnLoadListener(OnLoadListener onLoadListener){
this.onLoadListener = onLoadListener;
} /**
* 加载数据接口
* @author Administrator
*
*/
public interface OnLoadListener{
void onLoad();
} /**
* 数据加载完成
*/
public void loadComplete(){
footerView.setVisibility(View.GONE);
isLoading = false;
this.invalidate();
} }
</span>
底部下拉加载布局footer.xml
<span style="font-family:KaiTi_GB2312;font-size:18px;"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/footer_ll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal" > <ProgressBar
android:id="@+id/progress"
style="?android:attr/progressBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="30dp"
android:layout_marginTop="30dp" /> <TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="正在加载..."
android:textColor="#FF0000" /> </LinearLayout></span>
二.ListView顶部下拉刷新
数据顶部下拉刷新的步骤如下:
1.加载顶部自定义View,通过设置View的边距来显示和隐藏View;
2.通过OnScrollListener监听事件,获取ListView的活动状态以及firstVisibleItem首位可见的Item;
3.通过OnTouchListener监听事件,通过判断是否在首位,以及处理ACTION_DOWN,ACTION_MOVE,ACTION_UP
各个状态事件,并记录none(正常),pull(下拉),release(释放),reflashing(刷新)等状态。
ACTION_DOWN:判断是否在首列,若是,记录该点位置startY;
ACTION_MOVE:记录移动后的tempY,获取间距space,与View的高度比较后,动态获取View上边距,并设置。
ACTION_UP:记录为reflashing状态,刷新数据
4.刷新完成后,还原各自所处状态,隐藏View。
顶部下拉刷新自定义ReflashListVIew
<span style="font-family:KaiTi_GB2312;font-size:18px;">package com.example.paginationrefreshlistdemo.listview; import java.text.SimpleDateFormat;
import java.util.Date;
import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.RotateAnimation;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ListView;
import com.example.paginationrefreshlistdemo.R; public class RefreshListView extends ListView implements OnScrollListener{
//顶部View
private View topView;
//第一个可见View
int firstVisibleItem;
//顶部View的高度
int headerHeight;
// listview 当前滚动状态;
int scrollState;
// 标记,当前是在listview最顶端摁下的;
boolean isRefresh;
// 摁下时的Y值;
int startY; int state;// 当前的状态;
final int NONE = 0;// 正常状态;
final int PULL = 1;// 提示下拉状态;
final int RELESE = 2;// 提示释放状态;
final int REFLASHING = 3;// 刷新状态; public RefreshListView(Context context) {
super(context);
initView(context);
} public RefreshListView(Context context, AttributeSet attrs) {
super(context, attrs);
initView(context); } public RefreshListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initView(context);
} /**
* 初始化ListView
*/
private void initView(Context context){
LayoutInflater mInflater = LayoutInflater.from(context);
topView = mInflater.inflate(R.layout.top, null); measureView(topView);
headerHeight = topView.getMeasuredHeight();
//初始状态设置隐藏
topPadding(-headerHeight); this.setOnScrollListener(this);
this.addHeaderView(topView);
} @Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
//当滑动到底端,并滑动状态为 not scrolling
this.scrollState = scrollState;
} /**
* 通知父布局,占用的宽,高;
*
* @param view
*/
private void measureView(View view) {
ViewGroup.LayoutParams p = view.getLayoutParams();
if (p == null) {
p = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
}
int width = ViewGroup.getChildMeasureSpec(0, 0, p.width);
int height;
int tempHeight = p.height;
if (tempHeight > 0) {
height = MeasureSpec.makeMeasureSpec(tempHeight,
MeasureSpec.EXACTLY);
} else {
height = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
}
view.measure(width, height);
} /**
* 设置header 布局 上边距;
*
* @param topPadding
*/
private void topPadding(int topPadding) {
topView.setPadding(topView.getPaddingLeft(), topPadding,
topView.getPaddingRight(), topView.getPaddingBottom());
topView.invalidate();
} @Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
this.firstVisibleItem = firstVisibleItem ;
} @Override
public boolean onTouchEvent(MotionEvent ev) {
// TODO Auto-generated method stub
switch (ev.getAction()) {
//往下
case MotionEvent.ACTION_DOWN:
if (firstVisibleItem == 0) {
isRefresh = true;
startY = (int) ev.getY();
}
break;
//移动
case MotionEvent.ACTION_MOVE:
onMove(ev);
break;
//向上
case MotionEvent.ACTION_UP:
if (state == RELESE) {
state = REFLASHING;
// 加载最新数据;
reflashViewByState();
onRefreshListener.onRefresh();
} else if (state == PULL) {
state = NONE;
isRefresh = false;
reflashViewByState();
}
break;
}
return super.onTouchEvent(ev);
} /**
* 判断移动过程操作;
*
* @param ev
*/
private void onMove(MotionEvent ev) {
if (!isRefresh) {
return;
}
int tempY = (int) ev.getY();
int space = tempY - startY;
int topPadding = space - headerHeight;
switch (state) {
case NONE:
if (space > 0) {
state = PULL;
reflashViewByState();
}
break;
case PULL:
topPadding(topPadding);
if (space > headerHeight + 30
&& scrollState == SCROLL_STATE_TOUCH_SCROLL) {
state = RELESE;
reflashViewByState();
}
break;
case RELESE:
topPadding(topPadding);
if (space < headerHeight + 30) {
state = PULL;
reflashViewByState();
} else if (space <= 0) {
state = NONE;
isRefresh = false;
reflashViewByState();
}
break;
}
} /**
* 根据当前状态,改变界面显示;
*/
private void reflashViewByState() {
TextView tip = (TextView) topView.findViewById(R.id.tip);
ImageView arrow = (ImageView) topView.findViewById(R.id.arrow);
ProgressBar progress = (ProgressBar) topView.findViewById(R.id.progress_refresh);
RotateAnimation anim = new RotateAnimation(0, 180,
RotateAnimation.RELATIVE_TO_SELF, 0.5f,
RotateAnimation.RELATIVE_TO_SELF, 0.5f);
anim.setDuration(500);
anim.setFillAfter(true);
RotateAnimation anim1 = new RotateAnimation(180, 0,
RotateAnimation.RELATIVE_TO_SELF, 0.5f,
RotateAnimation.RELATIVE_TO_SELF, 0.5f);
anim1.setDuration(500);
anim1.setFillAfter(true);
switch (state) {
case NONE:
arrow.clearAnimation();
topPadding(-headerHeight);
break; case PULL:
arrow.setVisibility(View.VISIBLE);
progress.setVisibility(View.GONE);
tip.setText("下拉可以刷新!");
arrow.clearAnimation();
arrow.setAnimation(anim1);
break;
case RELESE:
arrow.setVisibility(View.VISIBLE);
progress.setVisibility(View.GONE);
tip.setText("松开可以刷新!");
arrow.clearAnimation();
arrow.setAnimation(anim);
break;
case REFLASHING:
topPadding(50);
arrow.setVisibility(View.GONE);
progress.setVisibility(View.VISIBLE);
tip.setText("正在刷新...");
arrow.clearAnimation();
break;
}
} private OnRefreshListener onRefreshListener;
public void setOnRefreshListener(OnRefreshListener onRefreshListener){
this.onRefreshListener = onRefreshListener;
} /**
* 加载数据接口
* @author Administrator
*
*/
public interface OnRefreshListener{
void onRefresh();
} /**
* 数据加载完成
*/
@SuppressLint("SimpleDateFormat")
public void refreshComplete(){
state = NONE;
isRefresh = false;
reflashViewByState();
TextView lastupdatetime = (TextView) topView
.findViewById(R.id.lastupdate_time);
SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日 hh:mm:ss");
Date date = new Date(System.currentTimeMillis());
String time = format.format(date);
lastupdatetime.setText(time);
} }
</span>
顶部下拉刷新布局top.xml
<span style="font-family:KaiTi_GB2312;font-size:18px;"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" > <RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="10dip"
android:paddingTop="10dip" > <LinearLayout
android:id="@+id/layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:orientation="vertical" > <TextView
android:id="@+id/tip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下拉可以刷新!" /> <TextView
android:id="@+id/lastupdate_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout> <ImageView
android:id="@+id/arrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/layout"
android:layout_marginRight="20dip"
android:src="@drawable/pull" /> <ProgressBar
android:id="@+id/progress_refresh"
style="?android:attr/progressBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/layout"
android:layout_marginRight="20dip"
android:visibility="gone" />
</RelativeLayout> </LinearLayout></span>
三.其他文件代码
1.主布局activity_main.xml
<span style="font-family:KaiTi_GB2312;font-size:18px;"><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"
android:orientation="vertical"
> <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ListView底部分页刷新和顶部下拉刷新"
android:gravity="center"
android:textSize="18sp"
android:textColor="#FF0000"
android:layout_marginBottom="20dp"
android:layout_marginTop="20dp"/> <ListView
android:id="@+id/main_lv"
android:layout_width="match_parent"
android:layout_height="match_parent"
></ListView> </LinearLayout>
</span>
2.底部分页布局activity_pagination.xml
<span style="font-family:KaiTi_GB2312;font-size:18px;"><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"
android:orientation="vertical"
> <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ListView底部分页刷新"
android:gravity="center"
android:textSize="18sp"
android:textColor="#FF0000"
android:layout_marginBottom="20dp"
android:layout_marginTop="20dp"/> <com.example.paginationrefreshlistdemo.listview.PaginationListView
android:id="@+id/pagination_lv"
android:layout_width="match_parent"
android:layout_height="match_parent"
/> </LinearLayout>
</span>
3.顶部下拉刷新activity_refresh.xml
<span style="font-family:KaiTi_GB2312;font-size:18px;"><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"
android:orientation="vertical"
> <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="ListView底部分页刷新"
android:gravity="center"
android:textSize="18sp"
android:textColor="#FF0000"
android:layout_marginBottom="20dp"
android:layout_marginTop="20dp"/> <com.example.paginationrefreshlistdemo.listview.PaginationListView
android:id="@+id/pagination_lv"
android:layout_width="match_parent"
android:layout_height="match_parent"
/> </LinearLayout>
</span>
4.Adapter加载布局list_view.xml
<span style="font-family:KaiTi_GB2312;font-size:18px;"><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" > <LinearLayout
android:id="@+id/content_ll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginLeft="20dp"
android:gravity="center"
android:orientation="vertical" > <TextView
android:id="@+id/content_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="亲亲,来一下"
android:textColor="#FF0000" /> <TextView
android:id="@+id/date_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#00FF00" />
</LinearLayout> <ImageView
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="@id/content_ll"
android:src="@drawable/emotion" /> </RelativeLayout></span>
5.实体ApkBean
<span style="font-family:KaiTi_GB2312;font-size:18px;">package com.example.paginationrefreshlistdemo.bean; public class ApkBean {
private String content;
private String dateString;
/**
* @return the content
*/
public String getContent() {
return content;
}
/**
* @param content the content to set
*/
public void setContent(String content) {
this.content = content;
}
/**
* @return the dateString
*/
public String getDateString() {
return dateString;
}
/**
* @param dateString the dateString to set
*/
public void setDateString(String dateString) {
this.dateString = dateString;
} }
</span>
6.适配器DemoAdapter
<span style="font-family:KaiTi_GB2312;font-size:18px;">package com.example.paginationrefreshlistdemo.adapter; import java.util.List; import com.example.paginationrefreshlistdemo.R;
import com.example.paginationrefreshlistdemo.bean.ApkBean; import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView; public class DemoAdapter extends BaseAdapter {
private List<ApkBean> datas ;
private LayoutInflater mInfalter; public DemoAdapter(Context context,List<ApkBean> datas){
this.datas = datas;
this.mInfalter = LayoutInflater.from(context);
} public void updateView( List<ApkBean> datas ){
this.datas = datas;
this.notifyDataSetChanged();
} @Override
public int getCount() {
// TODO Auto-generated method stub
return datas.size();
} @Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return datas.get(position);
} @Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
} @Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
HolderView holderView; if(convertView == null){
holderView = new HolderView();
convertView = mInfalter.inflate(R.layout.list_view_main, null);
holderView.contentTv = (TextView) convertView.findViewById(R.id.content_tv);
holderView.dateTv = (TextView) convertView.findViewById(R.id.date_tv);
convertView.setTag(holderView); }else{
holderView =(HolderView) convertView.getTag();
}
holderView.contentTv.setText(datas.get(position).getContent());
if(datas.get(position).getDateString() == null){
holderView.dateTv.setVisibility(View.GONE);
}else{
holderView.dateTv.setVisibility(View.VISIBLE);
holderView.dateTv.setText(datas.get(position).getDateString());
}
return convertView;
} public class HolderView{
ImageView iv;
TextView contentTv,dateTv;
} }
</span>
7.主函数MainActivity.java,一个ListView中包含底部下拉加载和顶部下拉刷新数据。
<span style="font-family:KaiTi_GB2312;font-size:18px;">package com.example.paginationrefreshlistdemo.activity; import java.util.ArrayList;
import java.util.List; import com.example.paginationrefreshlistdemo.R;
import com.example.paginationrefreshlistdemo.adapter.DemoAdapter;
import com.example.paginationrefreshlistdemo.bean.ApkBean; import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView; public class MainActivity extends Activity { private ListView mainLv;
private DemoAdapter demoAdapter;
private List<ApkBean> datas = new ArrayList<ApkBean>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initData();
mainLv =(ListView) this.findViewById(R.id.main_lv); mainLv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
switch (position) {
case 0:
startActvity(PaginationActivity.class);
break; case 1:
startActvity(RefreshActivity.class);
break;
}
}
}); showView();
} private void startActvity(Class<?> clz){
Intent intent = new Intent();
intent.setClass(this, clz);
this.startActivity(intent);
} private void initData(){
ApkBean apkBean = new ApkBean();
apkBean.setContent("ListView分页刷新"); ApkBean apkBean2 = new ApkBean();
apkBean2.setContent("ListView下拉刷新");
datas.add(apkBean);
datas.add(apkBean2);
} private void showView(){
if(demoAdapter == null){
demoAdapter = new DemoAdapter(this, datas);
mainLv.setAdapter(demoAdapter);
}else{
demoAdapter.updateView(datas);
}
} }
</span>
8.分页加载PaginationActivity.java,使用PaginationListView
<span style="font-family:KaiTi_GB2312;font-size:18px;">package com.example.paginationrefreshlistdemo.activity; import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List; import com.example.paginationrefreshlistdemo.R;
import com.example.paginationrefreshlistdemo.adapter.DemoAdapter;
import com.example.paginationrefreshlistdemo.bean.ApkBean;
import com.example.paginationrefreshlistdemo.listview.PaginationListView;
import com.example.paginationrefreshlistdemo.listview.PaginationListView.OnLoadListener; import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener; public class PaginationActivity extends Activity implements OnLoadListener { private PaginationListView paginationLv;
private DemoAdapter paginationAdapter;
private List<ApkBean> datas = new ArrayList<ApkBean>(); @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pagination);
initData();
paginationLv = (PaginationListView) this
.findViewById(R.id.pagination_lv);
paginationLv.setOnLoadListener(this); paginationLv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) { }
}); showView();
} @SuppressLint("SimpleDateFormat")
private void initData() {
ApkBean apkBean;
String dateString;
long dateLong = new Date().getTime();
for (int i = 0; i < 20; i++) {
apkBean = new ApkBean();
apkBean.setContent("这是一个美好的夜晚 " + i);
dateLong = dateLong + i * 1000 * 6;
dateString = new SimpleDateFormat("yyyy-MM-dd HHmmss")
.format(new Date(dateLong));
apkBean.setDateString(dateString);
datas.add(apkBean);
}
} /**
* 加载适配器
*/
private void showView() {
if (paginationAdapter == null) {
paginationAdapter = new DemoAdapter(this, datas);
paginationLv.setAdapter(paginationAdapter);
} else {
paginationAdapter.updateView(datas);
}
} @Override
public void onLoad() {
// 为了显示效果,采用延迟加载
new Handler().postDelayed(new Runnable() { @Override
public void run() {
// TODO Auto-generated method stub
initLoadData();
showView();
paginationLv.loadComplete();
}
}, 3000);
} @SuppressLint("SimpleDateFormat")
private void initLoadData() {
ApkBean apkBean;
String dateString;
long dateLong = new Date().getTime();
for (int i = 0; i < 20; i++) {
apkBean = new ApkBean();
apkBean.setContent("这是一个新的开始 " + i);
dateLong = dateLong + i * 1000 * 6 * 60;
dateString = new SimpleDateFormat("yyyy-MM-dd HHmmss")
.format(new Date(dateLong));
apkBean.setDateString(dateString);
datas.add(apkBean);
}
} }
</span>
9.顶部刷新RefreshActivity.java,使用自定义RefreshListView
<span style="font-family:KaiTi_GB2312;font-size:18px;">package com.example.paginationrefreshlistdemo.activity; import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List; import com.example.paginationrefreshlistdemo.R;
import com.example.paginationrefreshlistdemo.adapter.DemoAdapter;
import com.example.paginationrefreshlistdemo.bean.ApkBean;
import com.example.paginationrefreshlistdemo.listview.RefreshListView;
import com.example.paginationrefreshlistdemo.listview.RefreshListView.OnRefreshListener; import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener; public class RefreshActivity extends Activity implements OnRefreshListener{ private RefreshListView refreshLv;
private DemoAdapter refreshAdapter;
private List<ApkBean> datas = new ArrayList<ApkBean>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_refresh);
initData();
refreshLv =(RefreshListView) this.findViewById(R.id.refresh_lv);
refreshLv.setOnRefreshListener(this);
refreshLv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) { }
}); showView();
} @SuppressLint("SimpleDateFormat")
private void initData(){
ApkBean apkBean;
String dateString;
long dateLong = new Date().getTime();
for (int i = 0; i < 20; i++) {
apkBean = new ApkBean();
apkBean.setContent("这是一个刷新的早晨 " + i);
dateLong = dateLong + i * 1000 * 6;
dateString = new SimpleDateFormat("yyyy-MM-dd HHmmss")
.format(new Date(dateLong));
apkBean.setDateString(dateString);
datas.add(apkBean);
}
} private void showView(){
if(refreshAdapter == null){
refreshAdapter = new DemoAdapter(this, datas);
refreshLv.setAdapter(refreshAdapter);
}else{
refreshAdapter.updateView(datas);
}
} @Override
public void onRefresh() {
new Handler().postDelayed(new Runnable() { @Override
public void run() {
initLoadData();
refreshAdapter.updateView(datas);
refreshLv.refreshComplete();
}
}, 3000);
} @SuppressLint("SimpleDateFormat")
private void initLoadData() {
ApkBean apkBean;
String dateString;
long dateLong = new Date().getTime();
for (int i = 0; i < 5; i++) {
apkBean = new ApkBean();
apkBean.setContent("刷新新的的数据 " + i);
dateLong = dateLong + i * 1000 * 6 * 60;
dateString = new SimpleDateFormat("yyyy-MM-dd HHmmss")
.format(new Date(dateLong));
apkBean.setDateString(dateString);
datas.add(apkBean);
}
} }
</span>
Android 自定义ListView实现底部分页刷新与顶部下拉刷新,androidlistview的更多相关文章
- 滚动到底部加载更多及下拉刷新listview的使用
最新内容建议直接访问原文:滚动到底部加载更多及下拉刷新listview的使用 本文主要介绍可同时实现下拉刷新及滑动到底部加载更多的ListView的使用. 该ListView优点包括:a. 可自定义下 ...
- Android 学习笔记之AndBase框架学习(六) PullToRefrech 下拉刷新的实现
PS:Struggle for a better future 学习内容: 1.PullToRefrech下拉刷新的实现... 不得不说AndBase这个开源框架确实是非常的强大..把大部分的东西 ...
- Android项目:使用pulltorefresh开源项目扩展为下拉刷新上拉加载更多的处理方法,监听listview滚动方向
很多android应用的下拉刷新都是使用的pulltorefresh这个开源项目,但是它的扩展性在下拉刷新同时又上拉加载更多时会有一定的局限性.查了很多地方,发现这个开源项目并不能很好的同时支持下拉刷 ...
- Android零基础入门第72节:SwipeRefreshLayout下拉刷新
在实际开发中,经常都会遇到下拉刷新.上拉加载更多的情形,这一期就一起来学习Android系统的SwipeRefreshLayout下拉刷新组件. 一.SwipeRefreshLayout简介 Swip ...
- Android 5.X新特性之为RecyclerView添加下拉刷新和上拉加载及SwipeRefreshLayout实现原理
RecyclerView已经写过两篇文章了,分别是Android 5.X新特性之RecyclerView基本解析及无限复用 和 Android 5.X新特性之为RecyclerView添加Header ...
- .Net 转战 Android 4.4 日常笔记(10)--PullToRefresh下拉刷新使用
下拉刷新很多地方都用到了,新浪微博,微信,百度新闻 这里我们使用一个开源的库叫:PullToRefresh 开源地址:https://github.com/chenyoca/pull-to-refre ...
- Android Material Design控件使用(四)——下拉刷新 SwipeRefreshLayout
使用下拉刷新SwipeRefreshLayout 说明 SwipeRefreshLayout是Android官方的一个下拉刷新控件,一般我们使用此布局和一个RecyclerView嵌套使用 使用 xm ...
- 关于分页接口设计(下拉刷新上拉加载原理,解决page count请求重复数据的问题)
- Android 它们的定义ListView实现底部和页下拉刷新刷新的顶
在项目开发.由于数据量过大,寻呼需要加载或下拉刷新.为了缓解长期等待-time负载.这个博客的评论中被自己的定义实例ListView实现底部的下拉刷新页面正在加载结果和顶部. 其效果图: 一.List ...
随机推荐
- 使用eclipse创建maven项目出现的一个问题
错误信息 This error occurs when you employ a plugin that Maven could not download. Possible causes for t ...
- SPOJ 694 Distinct Substrings/SPOJ 705 New Distinct Substrings(后缀数组)
Given a string, we need to find the total number of its distinct substrings. Input T- number of test ...
- POJ 3348 Cows(凸包+多边形面积)
Description Your friend to the south is interested in building fences and turning plowshares into sw ...
- 3DMAX2016破解教程
首先,断网. 然后,下载3DMAX2016注册机. 然后,打开已经安装的3DMAX2016,会出现下图,点击激活按钮. 之后,以管理员身份打开3DMAX2016注册机,把申请号复制到注册机里面的请求码 ...
- PAT L1-048 矩阵A乘以B
https://pintia.cn/problem-sets/994805046380707840/problems/994805082313310208 给定两个矩阵A和B,要求你计算它们的乘积矩阵 ...
- checkBox1_CheckedChanged(object sender, EventArgs e)和checkBox1_CheckStateChanged(object sender, EventArgs e)不同
using System; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms ...
- PL/SQL在 win8.1系统下连接Oracle11g没有database处理方法(亲身实验,吐血分享)
一.问题 这里首先说明下我的环境:win8.1(64bit)+oracle11g(64bit)+PL/SQL(32bit).状况是:net manager正常配置,测试也成功,但是用PL/SQL连接的 ...
- Delphi 制作自定义数据感知控件并装入包(dpk文件)中(与DBText类似的数据感知控件)
一.基础知识简介: 1.包的命名介绍: 包的命名没有规则,但是建议遵守包的命名约定:包的命名与包的版本相关,包的名称前面几个字符通常表示作者或公司名,也可以是控件的一个描述词,后面紧跟的Std表示运行 ...
- Tomcat 设计模式分析
门面设计模式 门面设计模式在 Tomcat 中有多处使用,在 Request 和 Response 对象封装中.Standard Wrapper 到 ServletConfig 封装中.Applica ...
- BZOJ 1297 迷路(矩阵快速幂)
很容易想到记忆化搜索的算法. 令dp[n][T]为到达n点时时间为T的路径条数.则dp[n][T]=sigma(dp[i][T-G[i][n]]); 但是空间复杂度为O(n*T),时间复杂度O(n*n ...