listView下拉刷新时,为什么同时触发点击事件,如何让下拉时不触发点击事件

平凡一人生
  • 22

import java.util.ArrayList;
import com.example.pull.view.MyListView;
import com.example.pull.view.MyListView.OnRefreshListener;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class MainActivity extends Activity {

MyListView myListView;
ArrayList<String> list=new ArrayList<String>();
Myadapter adapter;

private Handler handler=new Handler(){
    public void handleMessage(android.os.Message msg) {
        //更新UI
        adapter.notifyDataSetChanged();
        
        myListView.completeRefresh();
    };
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    initview();
    initData();
}
private void initview() {
    setContentView(R.layout.activity_main);
    myListView=(MyListView) findViewById(R.id.myListView);
    
}

private void initData() {
    for (int i = 0; i < 15; i++) {
        list.add("listView原来数据—>"+i);
    }

    adapter=new Myadapter();
    myListView.setAdapter(adapter);
    myListView.setOnRefreshListener(new OnRefreshListener() {
        
        @Override
        public void onPullRefresh() {
            //需要联网请求服务器的数据,然后更新UI
            requestDataFromServer(false);
        }

        @Override
        public void onLoadingMore() {
            requestDataFromServer(true);
        }
    });
    myListView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            System.out.println("点击:"+position);
        }
    });
}

private void requestDataFromServer(final boolean isLoadingMore){
    new Thread(){
        public void run(){
            SystemClock.sleep(3000);
            if(isLoadingMore){
                list.add("上拉刷新加载数据—>1");
                list.add("上拉刷新加载数据—>2");
                list.add("上拉刷新加载数据—>3");
            }else{
                list.add(0,"下拉刷新的数据");                    
            }
            
            //在UI线程更新UI
            handler.sendEmptyMessage(0);
        }
    }.start();
}




class Myadapter extends BaseAdapter{
    @Override
    public int getCount() {
        
        return list.size();
    }

    @Override
    public Object getItem(int position) {

        return null;
    }

    @Override
    public long getItemId(int position) {

        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        TextView textView=new TextView(MainActivity.this);
        textView.setPadding(20, 20, 20, 20);
        textView.setTextSize(18);
        textView.setText(list.get(position));
        return textView;
    }
}

}

package com.example.pull.view;

import java.text.SimpleDateFormat;
import java.util.Date;
import com.example.pull.R;
import android.widget.AbsListView.OnScrollListener;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.RotateAnimation;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

public class MyListView extends ListView implements OnScrollListener{

private View headerView,footerView;
private ImageView iv;
private ProgressBar pb;
private TextView tv1,tv2;


private int headerHeight,footerHeight;//headerView的高度
private int downY;//获取当前y的高度
private final int PULL_REFRESH=0;    //下拉刷新的状态
private final int RELEASE_REFRESH=1;//松开刷新的状态
private final int REFRESHING=2;        //正在刷新的状态
private int currentState=PULL_REFRESH;//默认下拉刷新状态
private RotateAnimation upAnimation,downAnimation;

private boolean isLoadingMore=false;//当前是否处于加载更多


public MyListView(Context context) {
    super(context);
    init();

}

public MyListView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
}
public void init(){
    setOnScrollListener(this);
    initHeaderView();
    initRotateAnimation();
    initFooterView();
}
//初始化headerView
private void initHeaderView() {
    headerView=View.inflate(getContext(), R.layout.layout_header, null);
    iv=(ImageView) headerView.findViewById(R.id.imageView);
    pb= (ProgressBar) headerView.findViewById(R.id.progressBar);
    tv1= (TextView) headerView.findViewById(R.id.textView1);
    tv2= (TextView) headerView.findViewById(R.id.textView2);
    
    
    headerView.measure(0, 0);//通知系统主动测量
    headerHeight=headerView.getMeasuredHeight();
    headerView.setPadding(0, -headerHeight, 0, 0);
    addHeaderView(headerView);
    
}

private void initRotateAnimation() {
    upAnimation=new RotateAnimation(0, -180,
            RotateAnimation.RELATIVE_TO_SELF, 0.5f,
            RotateAnimation.RELATIVE_TO_SELF, 0.5f);
    upAnimation.setDuration(300);
    upAnimation.setFillAfter(true);
    
    downAnimation=new RotateAnimation(-180, -360,
            RotateAnimation.RELATIVE_TO_SELF, 0.5f,
            RotateAnimation.RELATIVE_TO_SELF, 0.5f);
    downAnimation.setDuration(300);
    downAnimation.setFillAfter(true);
}


@Override
public boolean onTouchEvent(MotionEvent ev) {
    switch (ev.getAction()) {
    case MotionEvent.ACTION_DOWN:
        downY=(int) ev.getY();
        break;
    case MotionEvent.ACTION_MOVE:
        
        if(currentState==REFRESHING){
            break;
        }
        
        
        int deltaY=(int) (ev.getY()-downY);
        int paddingTop=-headerHeight+deltaY;
        if(paddingTop>-headerHeight && getFirstVisiblePosition()==0){
        headerView.setPadding(0, paddingTop, 0, 0);
        if(paddingTop>=0 && currentState==PULL_REFRESH){
            //下拉刷新状态进入松开刷新状态
            currentState=RELEASE_REFRESH;
            refreshHeaderView();
        }else if (paddingTop<0 && currentState==RELEASE_REFRESH) {
            //松开刷新状态进入下拉刷新状态
            currentState=PULL_REFRESH;
            refreshHeaderView();
        }
        
        return true;
        }
        //return true;//拦截TouchMove,不让listView处理,会造成listView无法滑动
        
        break;
    case MotionEvent.ACTION_UP:
        if(currentState==PULL_REFRESH){
            headerView.setPadding(0, -headerHeight, 0, 0);
        }else if (currentState==RELEASE_REFRESH) {
            headerView.setPadding(0, 0, 0, 0);
            currentState=REFRESHING;
            refreshHeaderView();
            
            if(listener!=null){
                listener.onPullRefresh();
            }
        }
        break;

    default:
        break;
    }
    return super.onTouchEvent(ev);
}

 // 根据currentState来更新headerView

private void refreshHeaderView(){
    switch (currentState) {
    case PULL_REFRESH:
        tv1.setText("下拉刷新");
        iv.startAnimation(downAnimation);
        
        
        
        break;
    case RELEASE_REFRESH:
        tv1.setText("松开刷新");
        iv.startAnimation(upAnimation);
        break;
    case REFRESHING:
        tv1.setText("正在刷新...");
        iv.clearAnimation();//向上的旋转动画可能没有执行完
        iv.setVisibility(View.INVISIBLE);
        pb.setVisibility(View.VISIBLE);
        break;

    default:
        break;
    }
}

 // 完成刷新操作,重置状态,在获取完数据并更新完adapter之后,去在UI线程中调用
public void completeRefresh(){
    if(isLoadingMore){
        //重置footerView状态
        footerView.setPadding(0, -footerHeight, 0, 0);
        isLoadingMore=false;
    }else{
        //重置headerView状态
        headerView.setPadding(0, -headerHeight, 0, 0);
        currentState=PULL_REFRESH;
        
        iv.setVisibility(View.INVISIBLE);
        pb.setVisibility(View.VISIBLE);
        tv1.setText("下拉刷新");
        tv2.setText("最后刷新:"+getCurrentTime());
        
        pb.setVisibility(View.INVISIBLE);
        iv.setVisibility(View.VISIBLE);
    }
    
}
private String getCurrentTime(){
    SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    return format.format(new Date());
}

private OnRefreshListener listener;
public void setOnRefreshListener(OnRefreshListener listener){
    this.listener=listener;
}
public interface OnRefreshListener{
    void onPullRefresh();
    void onLoadingMore();
}

//尾部上拉加载
private void initFooterView() {
    footerView=View.inflate(getContext(), R.layout.layout_footer, null);
    footerView.measure(0, 0);//通知系统主动测量
    footerHeight=footerView.getMeasuredHeight();
    footerView.setPadding(0, -footerHeight, 0, 0);
    
    addFooterView(footerView);
    
}

@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
    if(scrollState==OnScrollListener.SCROLL_STATE_IDLE
        && getLastVisiblePosition()==(getCount()-1) && !isLoadingMore){
        isLoadingMore=true;
        footerView.setPadding(0, 0, 0, 0);//显示出加载更多
        setSelection(getCount());//让listView最后一条显示出来
        
        if(listener!=null){
            listener.onLoadingMore();
        }
        
    }
}

@Override
public void onScroll(AbsListView view, int firstVisibleItem,
        int visibleItemCount, int totalItemCount) {
    
}

}

回复
阅读 6.7k
7 个回答

删除 case MotionEvent.ACTION_MOVE:中return true;

在case MotionEvent.ACTION_DOWN:中增加

    if(currentState==PULL_REFRESH){
     return true;
    }
    downY=(int) ev.getY();
    break;

重写你的ListView的onInterceptTouchEvent方法, 判断状态是REFRESHING状态就return true

根据移动判断调用:
list.setEnabled(false);

魔法炒勺
  • 2
新手上路,请多包涵

上面说的删除return true。可以

第二个回答完全可用

宣传栏