1、因为给LISTVIEW增加了两个头布局,所以在点击事件ITEM索引会增加2,比如原来第一条数据的索引应该为0,增加两个头布局后,它的索引变为 2,为了使LISTVIEW的ITEM在点击时索引不变,实现以下方法
2、实现接口android.widget.AdapterView.OnItemClickListener
3、重写setOnItemClickListener onItemClick方法
private OnItemClickListener mItemClickListener;
// 重写item点击方法
@Override
public void setOnItemClickListener(
android.widget.AdapterView.OnItemClickListener listener) {
mItemClickListener = listener;
super.setOnItemClickListener(this);// 将点击事件设置给当前的RefreshListView
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
if (mItemClickListener != null) {
mItemClickListener.onItemClick(parent, view, position
- getHeaderViewsCount(), id);
}
}
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.AbsListView; import android.widget.AbsListView.OnScrollListener; import android.widget.AdapterView; 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 implements OnScrollListener, android.widget.AdapterView.OnItemClickListener { 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 View mFooterView; private int mFooterViewHeight; 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;// 箭头向下动画 private boolean isLoadingMore;// 标记是否正在加载更多 public RefreshListView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); initHeaderView(); initFooterView(); } public RefreshListView(Context context, AttributeSet attrs) { super(context, attrs); initHeaderView(); initFooterView(); } public RefreshListView(Context context) { super(context); initHeaderView(); initFooterView(); } /** * 初始化头布局 */ private void initHeaderView() { 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();// 设置初始时间 } /** * 初始化脚布局 */ private void initFooterView() { mFooterView = View.inflate(getContext(), R.layout.list_refresh_footer, null); this.addFooterView(mFooterView); mFooterView.measure(0, 0); mFooterViewHeight = mFooterView.getMeasuredHeight(); // 隐藏脚布局 mFooterView.setPadding(0, -mFooterViewHeight, 0, 0); // 设置滑动监听 this.setOnScrollListener(this); } @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()