package com.dkkj.communitylsv;
import java.util.Date;
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.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
public class RefreshListView extends ListView implements OnScrollListener, OnClickListener{
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);
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) {
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:
break;
case IListViewState.LVS_PULL_REFRESH:
mHeadView.setPadding(0, -1 * mHeadContentHeight, 0, 0);
switchViewState(IListViewState.LVS_NORMAL);
break;
case IListViewState.LVS_RELEASE_REFRESH:
mHeadView.setPadding(0, 0, 0, 0);
switchViewState(IListViewState.LVS_LOADING);
onRefresh();
break;
}
}
// 切换headview视图
private void switchViewState(int state)
{
switch(state)
{
case IListViewState.LVS_NORMAL:
{
Log.e("!!!!!!!!!!!", "convert to IListViewState.LVS_NORMAL");
mArrowImageView.clearAnimation();
mArrowImageView.setImageResource(R.drawable.arrow);
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
Listview(常用).rar (59个子文件)
.project 848B
project.properties 361B
proguard.cfg 1KB
src
com
dkkj
communitylsv
MyListViewAdapter.java 2KB
RefreshListView.java 11KB
ListViewDemo.java 6KB
AndroidManifest.xml 804B
res
drawable-ldpi
ic_launcher.png 2KB
item_bg.9.png 1KB
adv_flag.png 532B
locate.png 3KB
drawable-hdpi
ic_launcher.png 4KB
arrow.png 449B
drawable
load_more_selected.xml 192B
load_more_selector.xml 307B
corners_bg.xml 368B
drawable-mdpi
ic_launcher.png 3KB
layout
item.xml 4KB
head.xml 3KB
titlebar.xml 868B
loadmore.xml 1KB
main.xml 617B
drawable-xhdpi
values
colors.xml 274B
style.xml 947B
strings.xml 168B
.settings
org.eclipse.jdt.core.prefs 177B
org.eclipse.core.resources.prefs 88B
assets
gen
com
dkkj
communitylsv
BuildConfig.java 163B
R.java 3KB
.classpath 364B
bin
resources.ap_ 23KB
classes
com
dkkj
communitylsv
R$layout.class 523B
R$style.class 493B
RefreshListView$ILoadMoreViewState.class 371B
BuildConfig.class 351B
R$drawable.class 656B
R$attr.class 349B
ListViewDemo$LoadMoreDataAsynTask.class 2KB
MyListViewAdapter.class 3KB
RefreshListView$IOnRefreshListener.class 273B
ListViewDemo.class 6KB
RefreshListView.class 9KB
R$id.class 1KB
RefreshListView$IOnLoadMoreListener.class 276B
ListViewDemo$RefreshDataAsynTask.class 2KB
R.class 654B
R$string.class 436B
RefreshListView$IListViewState.class 411B
ListViewDemo$1.class 935B
R$color.class 526B
AndroidManifest.xml 804B
dexedLibs
res
drawable-ldpi
ic_launcher.png 2KB
adv_flag.png 529B
locate.png 3KB
drawable-hdpi
ic_launcher.png 4KB
arrow.png 449B
drawable-mdpi
ic_launcher.png 2KB
classes.dex 19KB
ListViewDemo.apk 34KB
共 59 条
- 1
资源评论
liuwencaia
- 粉丝: 0
- 资源: 11
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功