ListView下拉刷新实现方式分析
1.添加顶部下拉加载界面。
2.监听onScrollListener,来判断当前是否在ListView最顶部。
3.监听onTouch事件,根据手势变化改变当前状态以及界面显示。
4.根据当前状态加载数据。
android.layout.simple_list_item_1:安卓自带的简单textView控件,可用在listView的数据源ArrayAdapter上。
下拉刷新控件源代码:
public class ReFreshListView extends ListView implements AbsListView.OnScrollListener{
View header;
int headerHeight; //顶部布局文件的高度
int firstVisibleItem; //当前第一个可见的item的位置
int scrollState; //listview当前滚动状态
boolean isRemark; //标记当前是否是在listview最顶端按下的
int startY; //按下时的Y值
int state; //当前的状态
final int NONE = 0; //正常状态
final int PULL = 1; //下拉状态
final int RELEASE = 2; //提示释放
final int REFRESHING = 3;//正在刷新
IRefreshListener iRefreshListener;//刷新数据的接口
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);
}
/**
* 添加顶部布局文件
* */
private void initView (Context context){
LayoutInflater inflater = LayoutInflater.from(context);
header = inflater.inflate(R.layout.header_refresh,null);
measureView(header);
headerHeight = header.getMeasuredHeight();
topPadding(-headerHeight);
this.addHeaderView(header); //添加布局文件
this.setOnScrollListener(this);
}
/**
* 通知父布局占用的宽高
* @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);
}
/**
* 设置上边距
* */
private void topPadding(int topPadding){
header.setPadding(header.getPaddingLeft(),topPadding,header.getPaddingRight(),header.getPaddingBottom());
header.invalidate();
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
this.scrollState = scrollState;
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
this.firstVisibleItem = firstVisibleItem;
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
switch (ev.getAction()){
case MotionEvent.ACTION_DOWN:
if (firstVisibleItem==0){
isRemark=true; //在最顶端按下屏幕
startY = (int)ev.getY();
}
break;
case MotionEvent.ACTION_UP:
if(state == RELEASE){
state = REFRESHING;
//加载最新数据
refreshViewByState(state);
iRefreshListener.onRefresh();
}else if(state==PULL){
state = NONE;
isRemark = false;
refreshViewByState(state);
}
break;
case MotionEvent.ACTION_MOVE:
onMove(ev);
break;
}
return super.onTouchEvent(ev);
}
/**
* 判断移动过程中的操作,对当前状态进行转换
* @param ev
*/
private void onMove(MotionEvent ev){
if(!isRemark){
return;
}
int tempY = (int)ev.getY();
int space = tempY - startY;
int topPadding = space - headerHeight;
switch (state){
case NONE:
if(space>0){
state = PULL;
refreshViewByState(state);
}
break;
case PULL:
topPadding(topPadding);
if(space>headerHeight+30
&&scrollState == SCROLL_STATE_TOUCH_SCROLL){
state = RELEASE;
refreshViewByState(state);
}
break;
case RELEASE:
topPadding(topPadding);
if(space<headerHeight+30){
state = PULL;
refreshViewByState(state);
}else if(space<=0){
state = NONE;
isRemark = false;
refreshViewByState(state);
}
break;
}
}
/**
* 根据当前状态刷新界面
* @param st
*/
private void refreshViewByState(int st){
TextView tip = (TextView)header.findViewById(R.id.tip);
ImageView arrow = (ImageView)header.findViewById(R.id.arrow);
ProgressBar progress = (ProgressBar)header.findViewById(R.id.progress);
//箭头反转动画
RotateAnimation animation = new RotateAnimation(0,180,
RotateAnimation.RELATIVE_TO_SELF,0.5f,
RotateAnimation.RELATIVE_TO_SELF,0.5f);
animation.setDuration(500);
animation.setFillAfter(true);
RotateAnimation animation1 = new RotateAnimation(180,0,
RotateAnimation.RELATIVE_TO_SELF,0.5f,
RotateAnimation.RELATIVE_TO_SELF,0.5f);
animation.setDuration(500);
animation.setFillAfter(true);
switch (st){
case NONE:
arrow.clearAnimation();
topPadding(-headerHeight);
break;
case PULL:
arrow.setVisibility(View.VISIBLE);
progress.setVisibility(View.GONE);
tip.setText("下拉可以刷新");
arrow.clearAnimation();
arrow.setAnimation(animation1);
break;
case RELEASE:
arrow.setVisibility(View.VISIBLE);
progress.setVisibility(View.GONE);
tip.setText("松开可以刷新");
arrow.clearAnimation();
arrow.setAnimation(animation);
break;
case REFRESHING:
topPadding(50);
arrow.setVisibility(View.GONE);
progress.setVisibility(View.VISIBLE);
tip.setText("正在刷新");
arrow.clearAnimation();
break;
}
}
/**
*表示刷新结束
*/
public void refreshComplete(){
state = NONE;
isRemark = false;
refreshViewByState(state);
TextView lastUpadteTime=(TextView)header.findViewById(R.id.lastupdateTime);
SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日 hh:mm:ss");
Date date = new Date(System.currentTimeMillis());
String time = format.format(date);
lastUpadteTime.setText(time);
}
public void setInterface(IRefreshListener iRefreshListener){
this.iRefreshListener = iRefreshListener;
}
public interface IRefreshListener{
public void onRefresh();
}
下拉刷新控件布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="10dip"
android:paddingBottom="10dip"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_centerInParent="true"
android:gravity="center"
android:id="@+id/linearLayout">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tip"
android:text="下拉可以刷新"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/lastupdateTime"
/>
</LinearLayout>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/arrow"
android:src="@drawable/arrowdown"
android:layout_below="@+id/progress"
android:layout_toLeftOf="@+id/linearLayout" />
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/progress"
android:visibility="gone"
style="?android:attr/progressBarStyleSmall"
android:layout_marginRight="20dip"
/>
</RelativeLayout>
</LinearLayout>