为了账号安全,请及时绑定邮箱和手机立即绑定

为什么点击到item上无法下拉

如果放在item上下拉就不好使,必须在item边界点击下拉才好使


public class ReFlashListView extends ListView implements OnScrollListener {
  View header;
  int headerHeight;
  int firstVisibleItem;
  int scrollState;
  boolean isRemark;
  int startY;

  int state;
  final int NONE = 0;
  final int PULL = 1;
  final int RELESE = 2;
  final int REFLASHING = 3;
  IReflashListener iReflashListener;
  public ReFlashListView(Context context) {
     super(context);
     // TODO Auto-generated constructor stub
     initView(context);
  }

  public ReFlashListView(Context context, AttributeSet attrs) {
     super(context, attrs);
     // TODO Auto-generated constructor stub
     initView(context);
  }

  public ReFlashListView(Context context, AttributeSet attrs, int defStyle) {
     super(context, attrs, defStyle);
     // TODO Auto-generated constructor stub
     initView(context);
  }

  private void initView(Context context) {
     LayoutInflater inflater = LayoutInflater.from(context);
     header = inflater.inflate(R.layout.header_layout, null);
     measureView(header);
     headerHeight = header.getMeasuredHeight();
     Log.i("tag", "headerHeight = " + headerHeight);
     topPadding(-headerHeight);
     this.addHeaderView(header);
     this.setOnScrollListener(this);
  }

  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 onScroll(AbsListView view, int firstVisibleItem,
                  int visibleItemCount, int totalItemCount) {
     // TODO Auto-generated method stub
     this.firstVisibleItem = firstVisibleItem;
  }

  @Override
  public void onScrollStateChanged(AbsListView view, int scrollState) {
     // TODO Auto-generated method stub
     this.scrollState = scrollState;
  }

  @Override
  public boolean onTouchEvent(MotionEvent ev) {
     // TODO Auto-generated method stub
     switch (ev.getAction()) {
     case MotionEvent.ACTION_DOWN:
        if (firstVisibleItem == 0) {
           isRemark = true;
           startY = (int) ev.getY();
        }
        break;

     case MotionEvent.ACTION_MOVE:
        onMove(ev);
        break;
     case MotionEvent.ACTION_UP:
        if (state == RELESE) {
           state = REFLASHING;
           reflashViewByState();
           iReflashListener.onReflash();
        } else if (state == PULL) {
           state = NONE;
           isRemark = false;
           reflashViewByState();
        }
        break;
     }
     return super.onTouchEvent(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;
           reflashViewByState();
        }
        break;
     case PULL:
        topPadding(topPadding);
        if (space > headerHeight + 30
              && scrollState == SCROLL_STATE_TOUCH_SCROLL) {
           state = RELESE;
           reflashViewByState();
        }
        break;
     case RELESE:
        topPadding(topPadding);
        if (space < headerHeight + 30) {
           state = PULL;
           reflashViewByState();
        } else if (space <= 0) {
           state = NONE;
           isRemark = false;
           reflashViewByState();
        }
        break;
     }
  }

  private void reflashViewByState() {
     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 anim = new RotateAnimation(0, 180,
           RotateAnimation.RELATIVE_TO_SELF, 0.5f,
           RotateAnimation.RELATIVE_TO_SELF, 0.5f);
     anim.setDuration(500);
     anim.setFillAfter(true);
     RotateAnimation anim1 = new RotateAnimation(180, 0,
           RotateAnimation.RELATIVE_TO_SELF, 0.5f,
           RotateAnimation.RELATIVE_TO_SELF, 0.5f);
     anim1.setDuration(500);
     anim1.setFillAfter(true);
     switch (state) {
     case NONE:
        arrow.clearAnimation();
        topPadding(-headerHeight);
        break;

     case PULL:
        arrow.setVisibility(View.VISIBLE);
        progress.setVisibility(View.GONE);
        tip.setText("下拉刷新");
        arrow.clearAnimation();
        arrow.setAnimation(anim1);
        break;
     case RELESE:
        arrow.setVisibility(View.VISIBLE);
        progress.setVisibility(View.GONE);
        tip.setText("释放刷新");
        arrow.clearAnimation();
        arrow.setAnimation(anim);
        break;
     case REFLASHING:
        topPadding(50);
        arrow.setVisibility(View.GONE);
        progress.setVisibility(View.VISIBLE);
        tip.setText("正在刷新...");
        arrow.clearAnimation();
        break;
     }
  }

  public void reflashComplete() {
     state = NONE;
     isRemark = false;
     reflashViewByState();
     TextView lastupdatetime = (TextView) header
           .findViewById(R.id.lastupdate_time);
     SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日 hh:mm:ss");
     Date date = new Date(System.currentTimeMillis());
     String time = format.format(date);
     lastupdatetime.setText(time);
  }
 
  public void setInterface(IReflashListener iReflashListener){
     this.iReflashListener = iReflashListener;
  }
  public interface IReflashListener{
     public void onReflash();
  }
}

正在回答

举报

0/150
提交
取消
Android的ListView下拉刷新
  • 参与学习       44056    人
  • 解答问题       130    个

本课程就分享一个Android实现ListView下拉刷新功能的技巧

进入课程

为什么点击到item上无法下拉

我要回答 关注问题
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号