android 项目学习随笔十一(ListView下拉刷新提示)
1、 设置mHeaderView.setPadding TOPPADING为负值,隐藏刷新提示头布局
在onTouchEvent事件中进行头布局显示隐藏切换
import java.text.SimpleDateFormat; import java.util.Date; import android.content.Context; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; import android.view.animation.Animation; import android.view.animation.RotateAnimation; import android.widget.ImageView; import android.widget.ListView; import android.widget.ProgressBar; import android.widget.TextView; import com.itheima.zhsh66.R; /** * 下拉刷新的listview * */ public class RefreshListView extends ListView { private static final int STATE_PULL_TO_REFRESH = 1;// 下拉刷新 private static final int STATE_RELEASE_TO_REFRESH = 2;// 松开刷新 private static final int STATE_REFRESHING = 3;// 正在刷新 // 下拉刷新头布局 private View mHeaderView; // 头布局高度 private int mHeaderViewHeight; private int startY = -1; // 当前下拉刷新的状态 private int mCurrentState = STATE_PULL_TO_REFRESH;// 默认是下拉刷新 private TextView tvTitle; private ImageView ivArrow; private ProgressBar pbLoading; private TextView tvTime; private RotateAnimation animUp;// 箭头向上动画 private RotateAnimation animDown;// 箭头向下动画 public RefreshListView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); initView(); } public RefreshListView(Context context, AttributeSet attrs) { super(context, attrs); initView(); } public RefreshListView(Context context) { super(context); initView(); } private void initView() { mHeaderView = View.inflate(getContext(), R.layout.list_refresh_header, null); this.addHeaderView(mHeaderView);// 添加头布局 // 隐藏头布局(1, 获取头布局高度, 2.设置负paddingTop,布局就会往上走) // int height = mHeaderView.getHeight();//此处无法获取高度,因为布局还没有绘制完成 // 绘制之前就要获取布局高度 mHeaderView.measure(0, 0);// 手动测量布局 mHeaderViewHeight = mHeaderView.getMeasuredHeight();// 测量之后的高度 // 隐藏头布局 mHeaderView.setPadding(0, -mHeaderViewHeight, 0, 0); tvTitle = (TextView) mHeaderView.findViewById(R.id.tv_title); ivArrow = (ImageView) mHeaderView.findViewById(R.id.iv_arrow); pbLoading = (ProgressBar) mHeaderView.findViewById(R.id.pb_loading); tvTime = (TextView) mHeaderView.findViewById(R.id.tv_time); initAnim(); setCurrentTime();// 设置初始时间 } @Override public boolean onTouchEvent(MotionEvent ev) { switch (ev.getAction()) { case MotionEvent.ACTION_DOWN: startY = (int) ev.getY(); break; case MotionEvent.ACTION_MOVE: if (startY == -1) {// 如果用户按住头条新闻向下滑动, 会导致listview无法拿到ACTION_DOWN, //因为此时事件会被交给父控件处理 // 此时要重新获取startY startY = (int) ev.getY(); } // 如果当前正在刷新, 什么都不做了 if (mCurrentState == STATE_REFRESHING) { break; } int endY = (int) ev.getY(); int dy = endY - startY; if (dy > 0 && getFirstVisiblePosition() == 0) {// 向下滑动&当前显示的是第一个item,才允许下拉刷新 int paddingTop = dy - mHeaderViewHeight;// 计算当前的paddingtop值 // 根据padding切换状态 if (paddingTop >= 0 && mCurrentState != STATE_RELEASE_TO_REFRESH) { // 切换到松开刷新 mCurrentState = STATE_RELEASE_TO_REFRESH; refreshState(); } else if (paddingTop < 0 && mCurrentState != STATE_PULL_TO_REFRESH) { // 切换到下拉刷新 mCurrentState = STATE_PULL_TO_REFRESH; refreshState(); } mHeaderView.setPadding(0, paddingTop, 0, 0);// 重新设置头布局padding return true; } break; case MotionEvent.ACTION_UP: startY = -1;// 起始坐标归零 if (mCurrentState == STATE_RELEASE_TO_REFRESH) { // 如果当前是松开刷新, 就要切换为正在刷新 mCurrentState = STATE_REFRESHING; // 显示头布局 mHeaderView.setPadding(0, 0, 0, 0); refreshState(); // 下拉刷新回调 if (mListener != null) { mListener.onRefresh(); } } else if (mCurrentState == STATE_PULL_TO_REFRESH) { // 隐藏头布局 mHeaderView.setPadding(0, -mHeaderViewHeight, 0, 0); } break; default: break; } return super.onTouchEvent(ev); } /** * 初始化箭头动画 */ private void initAnim() { animUp = new RotateAnimation(0, -180, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); animUp.setDuration(500); animUp.setFillAfter(true);// 保持状态 animDown = new RotateAnimation(-180, 0, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); animDown.setDuration(500); animDown.setFillAfter(true);// 保持状态 } /** * 根据当前状态刷新界面 */ private void refreshState() { switch (mCurrentState) { case STATE_PULL_TO_REFRESH: tvTitle.setText("下拉刷新"); // 箭头向下移动 ivArrow.startAnimation(animDown); // 隐藏进度条 pbLoading.setVisibility(View.INVISIBLE); ivArrow.setVisibility(View.VISIBLE); break; case STATE_RELEASE_TO_REFRESH: tvTitle.setText("松开刷新"); // 箭头向上移动 ivArrow.startAnimation(animUp); // 隐藏进度条 pbLoading.setVisibility(View.INVISIBLE); ivArrow.setVisibility(View.VISIBLE); break; case STATE_REFRESHING: tvTitle.setText("正在刷新..."); pbLoading.setVisibility(View.VISIBLE); ivArrow.clearAnimation();// 必须清除动画,才能隐藏控件 ivArrow.setVisibility(View.INVISIBLE); break; default: break; } } private OnRefreshListener mListener; public void setOnRefreshListener(OnRefreshListener listener) { mListener = listener; } /** * 设置上次刷新时间 */ private void setCurrentTime() { // 08:10 8:10 1 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// HH表示24小时制 String time = format.format(new Date()); tvTime.setText(time); } // 刷新完成 public void onRefreshComplete(boolean success) { // 隐藏头布局 mHeaderView.setPadding(0, -mHeaderViewHeight, 0, 0); mCurrentState = STATE_PULL_TO_REFRESH; // 隐藏进度条 pbLoading.setVisibility(View.INVISIBLE); ivArrow.setVisibility(View.VISIBLE); tvTitle.setText("下拉刷新"); // 刷新失败,不需要更新时间 if (success) { setCurrentTime(); } } public interface OnRefreshListener { // 下拉刷新的回调 public void onRefresh(); } }
2、给箭头图片设置动画
public class RefreshListView extends ListView {
private static final int STATE_PULL_TO_REFRESH = 1;// 下拉刷新
private static final int STATE_RELEASE_TO_REFRESH = 2;// 松开刷新
private static final int STATE_REFRESHING = 3;// 正在刷新
// 下拉刷新头布局
private View mHeaderView;
// 头布局高度
private int mHeaderViewHeight;
private int startY = -1;
// 当前下拉刷新的状态
private int mCurrentState = STATE_PULL_TO_REFRESH;// 默认是下拉刷新
private TextView tvTitle;
private ImageView ivArrow;
private ProgressBar pbLoading;
private TextView tvTime;
private RotateAnimation animUp;// 箭头向上动画
private RotateAnimation animDown;// 箭头向下动画
public RefreshListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initView();
}
public RefreshListView(Context context, AttributeSet attrs) {
super(context, attrs);
initView();
}
public RefreshListView(Context context) {
super(context);
initView();
}
private void initView() {
mHeaderView = View.inflate(getContext(), R.layout.list_refresh_header,
null);
this.addHeaderView(mHeaderView);// 添加头布局
// 隐藏头布局(1, 获取头布局高度, 2.设置负paddingTop,布局就会往上走)
// int height = mHeaderView.getHeight();//此处无法获取高度,因为布局还没有绘制完成
// 绘制之前就要获取布局高度
mHeaderView.measure(0, 0);// 手动测量布局
mHeaderViewHeight = mHeaderView.getMeasuredHeight();// 测量之后的高度
// 隐藏头布局
mHeaderView.setPadding(0, -mHeaderViewHeight, 0, 0);
tvTitle = (TextView) mHeaderView.findViewById(R.id.tv_title);
ivArrow = (ImageView) mHeaderView.findViewById(R.id.iv_arrow);
pbLoading = (ProgressBar) mHeaderView.findViewById(R.id.pb_loading);
tvTime = (TextView) mHeaderView.findViewById(R.id.tv_time);
initAnim();
setCurrentTime();// 设置初始时间
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
startY = (int) ev.getY();
break;
case MotionEvent.ACTION_MOVE:
if (startY == -1) {// 如果用户按住头条新闻向下滑动, 会导致listview无法拿到ACTION_DOWN,
//因为此时事件会被交给父控件处理
// 此时要重新获取startY
startY = (int) ev.getY();
}
// 如果当前正在刷新, 什么都不做了
if (mCurrentState == STATE_REFRESHING) {
break;
}
int endY = (int) ev.getY();
int dy = endY - startY;
if (dy > 0 && getFirstVisiblePosition() == 0) {// 向下滑动&当前显示的是第一个item,才允许下拉刷新
int paddingTop = dy - mHeaderViewHeight;// 计算当前的paddingtop值
// 根据padding切换状态
if (paddingTop >= 0
&& mCurrentState != STATE_RELEASE_TO_REFRESH) {
// 切换到松开刷新
mCurrentState = STATE_RELEASE_TO_REFRESH;
refreshState();
} else if (paddingTop < 0
&& mCurrentState != STATE_PULL_TO_REFRESH) {
// 切换到下拉刷新
mCurrentState = STATE_PULL_TO_REFRESH;
refreshState();
}
mHeaderView.setPadding(0, paddingTop, 0, 0);// 重新设置头布局padding
return true;
}
break;
case MotionEvent.ACTION_UP:
startY = -1;// 起始坐标归零
if (mCurrentState == STATE_RELEASE_TO_REFRESH) {
// 如果当前是松开刷新, 就要切换为正在刷新
mCurrentState = STATE_REFRESHING;
// 显示头布局
mHeaderView.setPadding(0, 0, 0, 0);
refreshState();
// 下拉刷新回调
if (mListener != null) {
mListener.onRefresh();
}
} else if (mCurrentState == STATE_PULL_TO_REFRESH) {
// 隐藏头布局
mHeaderView.setPadding(0, -mHeaderViewHeight, 0, 0);
}
break;
default:
break;
}
return super.onTouchEvent(ev);
}
/**
* 初始化箭头动画
*/
private void initAnim() {
animUp = new RotateAnimation(0, -180, Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
animUp.setDuration(500);
animUp.setFillAfter(true);// 保持状态
animDown = new RotateAnimation(-180, 0, Animation.RELATIVE_TO_SELF,
0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
animDown.setDuration(500);
animDown.setFillAfter(true);// 保持状态
}
/**
* 根据当前状态刷新界面
*/
private void refreshState() {
switch (mCurrentState) {
case STATE_PULL_TO_REFRESH:
tvTitle.setText("下拉刷新");
// 箭头向下移动
ivArrow.startAnimation(animDown);
// 隐藏进度条
pbLoading.setVisibility(View.INVISIBLE);
ivArrow.setVisibility(View.VISIBLE);
break;
case STATE_RELEASE_TO_REFRESH:
tvTitle.setText("松开刷新");
// 箭头向上移动
ivArrow.startAnimation(animUp);
// 隐藏进度条
pbLoading.setVisibility(View.INVISIBLE);
ivArrow.setVisibility(View.VISIBLE);
break;
case STATE_REFRESHING:
tvTitle.setText("正在刷新...");
pbLoading.setVisibility(View.VISIBLE);
ivArrow.clearAnimation();// 必须清除动画,才能隐藏控件
ivArrow.setVisibility(View.INVISIBLE);
break;
default:
break;
}
}
//ListView调用此方法,回调接口中的方法进行刷新数据
private OnRefreshListener mListener;
public void setOnRefreshListener(OnRefreshListener listener) {
mListener = listener;
}
/**
* 设置上次刷新时间
*/
private void setCurrentTime() {
// 08:10 8:10 1
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// HH表示24小时制
String time = format.format(new Date());
tvTime.setText(time);
}
// 刷新完成时调用此方法
public void onRefreshComplete(boolean success) {
// 隐藏头布局
mHeaderView.setPadding(0, -mHeaderViewHeight, 0, 0);
mCurrentState = STATE_PULL_TO_REFRESH;
// 隐藏进度条
pbLoading.setVisibility(View.INVISIBLE);
ivArrow.setVisibility(View.VISIBLE);
tvTitle.setText("下拉刷新");
// 刷新失败,不需要更新时间
if (success) {
setCurrentTime();
}
}
public interface OnRefreshListener {
// 下拉刷新的回调
public void onRefresh();
}
}
---------------------------------------------------------------------------------------------------------------------------------------
public class TabDetailPager extends BaseMenuDetailPager {
// 页签分类的网络信息
private NewsTabData mTabData;
// 网络返回的新闻列表数据
private NewsData mNewsTabData;
// 加载新闻列表的url
private String mUrl;
// 头条新闻的网络数据
private ArrayList<TopNews> mTopNewsList;
// 头条新闻的数据适配器
private TopNewsAdapter mTopNewsAdapter;
// 新闻列表的集合
private ArrayList<News> mNewsList;
private NewsAdapter mNewsAdapter;
@ViewInject(R.id.vp_tab_detail)
private HorizontalScrollViewPager mViewPager;
@ViewInject(R.id.lv_tab_detail)
private RefreshListView lvList;
@ViewInject(R.id.indicator)
private CirclePageIndicator mIndicator;
@ViewInject(R.id.tv_title)
private TextView tvTopNewsTitle;
public TabDetailPager(Activity activity, NewsTabData tabData) {
super(activity);
mTabData = tabData;
mUrl = Constants.SERVER_URL + mTabData.url;
}
@Override
public View initView() {
View view = View.inflate(mActivity, R.layout.pager_tab_detail, null);
ViewUtils.inject(this, view);
View header = View.inflate(mActivity, R.layout.list_header_topnews,
null);
ViewUtils.inject(this, header);// 必须也将头布局注入到ViewUtils
// 给listview添加头布局
lvList.addHeaderView(header);
// 设置下拉刷新监听
lvList.setOnRefreshListener(new OnRefreshListener() {
@Override
public void onRefresh() {
// 从网络加载数据
getDataFromServer();
}
});
return view;
}
@Override
public void initData() {
String cache = CacheUtils.getCache(mUrl, mActivity);
if (!TextUtils.isEmpty(cache)) {
processResult(cache);
}
getDataFromServer();
}
private void getDataFromServer() {
HttpUtils utils = new HttpUtils();
utils.send(HttpMethod.GET, mUrl, new RequestCallBack<String>() {
@Override
public void onSuccess(ResponseInfo<String> responseInfo) {
String result = responseInfo.result;
processResult(result);
System.out.println("访问网络成功!!!");
CacheUtils.setCache(mUrl, result, mActivity);
// 收起下拉刷新控件
lvList.onRefreshComplete(true);
}
@Override
public void onFailure(HttpException error, String msg) {
// 收起下拉刷新控件
lvList.onRefreshComplete(false);
error.printStackTrace();
Toast.makeText(mActivity, msg, Toast.LENGTH_SHORT).show();
}
});
}
protected void processResult(String result) {
Gson gson = new Gson();
mNewsTabData = gson.fromJson(result, NewsData.class);
// 初始化头条新闻
mTopNewsList = mNewsTabData.data.topnews;
if (mTopNewsList != null) {
mTopNewsAdapter = new TopNewsAdapter();
mViewPager.setAdapter(mTopNewsAdapter);
mIndicator.setViewPager(mViewPager);// 将指示器和viewpager绑定
mIndicator.setSnap(true);// 快照模式
mIndicator.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
System.out.println("position:" + position);
TopNews topNews = mTopNewsList.get(position);
tvTopNewsTitle.setText(topNews.title);
}
@Override
public void onPageScrolled(int position, float positionOffset,
int positionOffsetPixels) {
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
mIndicator.onPageSelected(0);// 将小圆点位置归零, 解决它会在页面销毁时仍记录上次位置的bug
tvTopNewsTitle.setText(mTopNewsList.get(0).title);// 初始化第一页标题
}
// 初始化新闻列表
mNewsList = mNewsTabData.data.news;
if (mNewsList != null) {
mNewsAdapter = new NewsAdapter();
lvList.setAdapter(mNewsAdapter);
}
}
class TopNewsAdapter extends PagerAdapter {
BitmapUtils mBitmapUtils;
public TopNewsAdapter() {
// 初始化xutils中的加载图片的工具
mBitmapUtils = new BitmapUtils(mActivity);
// 设置默认加载图片
mBitmapUtils
.configDefaultLoadingImage(R.drawable.topnews_item_default);
}
@Override
public int getCount() {
return mTopNewsList.size();
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == arg1;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView view = new ImageView(mActivity);
view.setScaleType(ScaleType.FIT_XY);// 设置图片填充效果, 表示填充父窗体
// 获取图片链接, 使用链接下载图片, 将图片设置给ImageView, 考虑内存溢出问题, 图片本地缓存
mBitmapUtils.display(view, mTopNewsList.get(position).topimage);
container.addView(view);
return view;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
}
class NewsAdapter extends BaseAdapter {
public BitmapUtils mBitmapUtils;
public NewsAdapter() {
mBitmapUtils = new BitmapUtils(mActivity);
mBitmapUtils
.configDefaultLoadingImage(R.drawable.pic_item_list_default);
}
@Override
public int getCount() {
return mNewsList.size();
}
@Override
public News getItem(int position) {
return mNewsList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = View.inflate(mActivity, R.layout.list_item_news,
null);
holder = new ViewHolder();
holder.tvTitle = (TextView) convertView
.findViewById(R.id.tv_title);
holder.tvDate = (TextView) convertView
.findViewById(R.id.tv_date);
holder.ivIcon = (ImageView) convertView
.findViewById(R.id.iv_icon);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
News news = getItem(position);
holder.tvTitle.setText(news.title);
holder.tvDate.setText(news.pubdate);
mBitmapUtils.display(holder.ivIcon, news.listimage);
return convertView;
}
}
static class ViewHolder {
public TextView tvTitle;
public TextView tvDate;
public ImageView ivIcon;
}
}