package com.genius.widget;
import java.util.Date;
import com.genius.test.R;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.MeasureSpec;
import android.view.View.OnClickListener;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.AbsListView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.AbsListView.OnScrollListener;
public class RefreshListView extends ListView implements OnScrollListener, OnClickListener{
public interface IListViewState
{
int LVS_NORMAL = 0; // 普通状态
int LVS_PULL_REFRESH = 1; // 下拉刷新状态
int LVS_RELEASE_REFRESH = 2; // 松开刷新状态
int LVS_LOADING = 3; // 加载状态
}
public interface IOnRefreshListener
{
void OnRefresh();
}
private View mHeadView;
private TextView mRefreshTextview;
private TextView mLastUpdateTextView;
private ImageView mArrowImageView;
private ProgressBar mHeadProgressBar;
private int mHeadContentWidth;
private int mHeadContentHeight;
private IOnRefreshListener mOnRefreshListener; // 头部刷新监听器
// 用于保证startY的值在一个完整的touch事件中只被记录一次
private boolean mIsRecord = false;
// 标记的Y坐标值
private int mStartY = 0;
// 当前视图能看到的第一个项的索引
private int mFirstItemIndex = -1;
// MOVE时保存的Y坐标值
private int mMoveY = 0;
// LISTVIEW状态
private int mViewState = IListViewState.LVS_NORMAL;
private final static int RATIO = 2;
private RotateAnimation animation;
private RotateAnimation reverseAnimation;
private boolean mBack = false;
public RefreshListView(Context context) {
super(context);
// TODO Auto-generated constructor stub
init(context);
}
public RefreshListView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public void setOnRefreshListener(IOnRefreshListener listener)
{
mOnRefreshListener = listener;
}
private void onRefresh()
{
if (mOnRefreshListener != null)
{
mOnRefreshListener.OnRefresh();
}
}
public void onRefreshComplete()
{
mHeadView.setPadding(0, -1 * mHeadContentHeight, 0, 0);
mLastUpdateTextView.setText("最近更新:" + new Date().toLocaleString());
switchViewState(IListViewState.LVS_NORMAL);
}
private void init(Context context)
{
initHeadView(context);
initLoadMoreView(context);
setOnScrollListener(this);
}
// 初始化headview试图
private void initHeadView(Context context)
{
mHeadView = LayoutInflater.from(context).inflate(R.layout.head, null);
mArrowImageView = (ImageView) mHeadView.findViewById(R.id.head_arrowImageView);
mArrowImageView.setMinimumWidth(60);
mHeadProgressBar= (ProgressBar) mHeadView.findViewById(R.id.head_progressBar);
mRefreshTextview = (TextView) mHeadView.findViewById(R.id.head_tipsTextView);
mLastUpdateTextView = (TextView) mHeadView.findViewById(R.id.head_lastUpdatedTextView);
measureView(mHeadView);
mHeadContentHeight = mHeadView.getMeasuredHeight();
mHeadContentWidth = mHeadView.getMeasuredWidth();
mHeadView.setPadding(0, -1 * mHeadContentHeight, 0, 0);
mHeadView.invalidate();
addHeaderView(mHeadView, null, false);
animation = new RotateAnimation(0, -180,
RotateAnimation.RELATIVE_TO_SELF, 0.5f,
RotateAnimation.RELATIVE_TO_SELF, 0.5f);
animation.setInterpolator(new LinearInterpolator());
animation.setDuration(250);
animation.setFillAfter(true);
reverseAnimation = new RotateAnimation(-180, 0,
RotateAnimation.RELATIVE_TO_SELF, 0.5f,
RotateAnimation.RELATIVE_TO_SELF, 0.5f);
reverseAnimation.setInterpolator(new LinearInterpolator());
reverseAnimation.setDuration(200);
reverseAnimation.setFillAfter(true);
}
// 初始化footview试图
private void initLoadMoreView(Context context)
{
mFootView= LayoutInflater.from(context).inflate(R.layout.loadmore, null);
mLoadMoreView = mFootView.findViewById(R.id.load_more_view);
mLoadMoreTextView = (TextView) mFootView.findViewById(R.id.load_more_tv);
mLoadingView = mFootView.findViewById(R.id.loading_layout);
mLoadMoreView.setOnClickListener(this);
addFooterView(mFootView);
}
// 此方法直接照搬自网络上的一个下拉刷新的demo,计算headView的width以及height
private void measureView(View child) {
ViewGroup.LayoutParams p = child.getLayoutParams();
if (p == null) {
p = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
}
int childWidthSpec = ViewGroup.getChildMeasureSpec(0, 0 + 0, p.width);
int lpHeight = p.height;
int childHeightSpec;
if (lpHeight > 0) {
childHeightSpec = MeasureSpec.makeMeasureSpec(lpHeight,
MeasureSpec.EXACTLY);
} else {
childHeightSpec = MeasureSpec.makeMeasureSpec(0,
MeasureSpec.UNSPECIFIED);
}
child.measure(childWidthSpec, childHeightSpec);
}
public void onScroll(AbsListView arg0, int firstVisiableItem, int visibleItemCount, int totalItemCount) {
mFirstItemIndex = firstVisiableItem;
}
public void onScrollStateChanged(AbsListView arg0, int arg1) {
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
// TODO Auto-generated method stub
if (mOnRefreshListener != null)
{
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
doActionDown(ev);
break;
case MotionEvent.ACTION_MOVE:
doActionMove(ev);
break;
case MotionEvent.ACTION_UP:
doActionUp(ev);
break;
default:
break;
}
}
return super.onTouchEvent(ev);
}
private void doActionDown(MotionEvent ev)
{
if(mIsRecord == false && mFirstItemIndex == 0)
{
mStartY = (int) ev.getY();
mIsRecord = true;
}
}
private void doActionMove(MotionEvent ev)
{
mMoveY = (int) ev.getY();
if(mIsRecord == false && mFirstItemIndex == 0)
{
mStartY = (int) ev.getY();
mIsRecord = true;
}
if (mIsRecord == false || mViewState == IListViewState.LVS_LOADING)
{
return ;
}
int offset = (mMoveY - mStartY) / RATIO;
switch(mViewState)
{
case IListViewState.LVS_NORMAL:
{
if (offset > 0)
{
mHeadView.setPadding(0, offset - mHeadContentHeight, 0, 0);
switchViewState(IListViewState.LVS_PULL_REFRESH);
}
}
break;
case IListViewState.LVS_PULL_REFRESH:
{
setSelection(0);
mHeadView.setPadding(0, offset - mHeadContentHeight, 0, 0);
if (offset < 0)
{
switchViewState(IListViewState.LVS_NORMAL);
}else if (offset > mHeadContentHeight)
{
switchViewState(IListViewState.LVS_RELEASE_REFRESH);
}
}
break;
case IListViewState.LVS_RELEASE_REFRESH:
{
setSelection(0);
mHeadView.setPadding(0, offset - mHeadContentHeight, 0, 0);
if (offset >= 0 && offset <= mHeadContentHeight)
{
mBack = true;
switchViewState(IListViewState.LVS_PULL_REFRESH);
}else if (offset < 0)
{
switchViewState(IListViewState.LVS_NORMAL);
}else{
}
}
break;
default:
return;
};
}
private void doActionUp(MotionEvent ev)
{
mIsRecord = false;
mBack = false;
if (mViewState == IListViewState.LVS_LOADING)
{
return ;
}
switch(mViewState)
{
case IListViewState.LVS_NORMAL:
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
RefreshListviewDemo.zip (41个子文件)
RefreshListviewDemo
AndroidManifest.xml 746B
res
layout
head.xml 2KB
listview_item_layout.xml 601B
loadmore.xml 1KB
main.xml 554B
drawable-ldpi
ic_launcher.png 2KB
drawable
load_more_selector.xml 307B
load_more_selected.xml 192B
drawable-mdpi
ic_launcher.png 3KB
drawable-hdpi
arrow.png 3KB
ic_launcher.png 4KB
values
strings.xml 204B
colors.xml 231B
proguard.cfg 1KB
src
com
genius
widget
RefreshListView.java 12KB
MyListViewAdapter.java 1KB
test
RefreshListviewDemoActivity.java 3KB
bin
classes
com
genius
widget
RefreshListView$ILoadMoreViewState.class 363B
RefreshListView.class 9KB
MyListViewAdapter.class 2KB
RefreshListView$IOnLoadMoreListener.class 268B
RefreshListView$IOnRefreshListener.class 265B
RefreshListView$IListViewState.class 403B
test
R$color.class 476B
RefreshListviewDemoActivity$LoadMoreDataAsynTask.class 2KB
R.class 555B
RefreshListviewDemoActivity$RefreshDataAsynTask.class 2KB
R$layout.class 489B
R$attr.class 331B
R$id.class 735B
R$string.class 418B
R$drawable.class 511B
RefreshListviewDemoActivity.class 4KB
res
RefreshListviewDemo.apk 28KB
classes.dex 15KB
resources.ap_ 18KB
.classpath 364B
assets
project.properties 360B
.settings
org.eclipse.core.resources.prefs 88B
.project 855B
gen
com
genius
test
R.java 2KB
共 41 条
- 1
蓝斯
- 粉丝: 1302
- 资源: 33
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
- 5
- 6
前往页