Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/kubernetes/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android MotionEvent.ACTION_DOWN赢得';t在方法onTouchEvent(MotionEvent ev)中执行_Android_Android Custom View_Motionevent - Fatal编程技术网

Android MotionEvent.ACTION_DOWN赢得';t在方法onTouchEvent(MotionEvent ev)中执行

Android MotionEvent.ACTION_DOWN赢得';t在方法onTouchEvent(MotionEvent ev)中执行,android,android-custom-view,motionevent,Android,Android Custom View,Motionevent,我正在自定义一个pull来刷新listview,我覆盖了方法“onTouchEvent(MotionEvent ev)”。但是当我在屏幕上下拉时,MotionEvent.ACTION\u down将不会执行。这是我的密码 public class ExListView extends ListView implements AbsListView.OnScrollListener { private int firstVisibleItemPosition; private int do

我正在自定义一个pull来刷新listview,我覆盖了方法“onTouchEvent(MotionEvent ev)”。但是当我在屏幕上下拉时,MotionEvent.ACTION\u down将不会执行。这是我的密码

  public class ExListView extends ListView implements AbsListView.OnScrollListener {

private int firstVisibleItemPosition; 
private int downY; // delta Y when pulling down

private final int DOWN_PULL_REFRESH = 0; 

private final int RELEASE_REFRESH = 1; 
private final int REFRESHING = 2; 
private int currentState = DOWN_PULL_REFRESH; 
private OnRefreshListener mOnRefreshListener;

private Animation upAnimation;

private Animation downAnimation; 
private View headerView; 

private ImageView ivArrow;
private ProgressBar mProgressBar; 
private TextView tvState; //  state of header
private int headerViewHeight; // header height

private View footerView; // footer
private int footerViewHeight; // footer height

private boolean isScrollToBottom; 
private boolean isLoadingMore = false; 

public ExListView(Context context) {
    super(context);
    init();
}

public ExListView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
}

public ExListView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init();
}

@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
    if (scrollState == SCROLL_STATE_IDLE || scrollState == SCROLL_STATE_FLING) {

        if (isScrollToBottom && !isLoadingMore) {
            isLoadingMore = true;

            footerView.setPadding(0, 0, 0, 0);
            this.setSelection(this.getCount());

            if (mOnRefreshListener != null) {
                mOnRefreshListener.onLoadMore();
            }
        }
    }
}

@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
    firstVisibleItemPosition = firstVisibleItem;

    if (getLastVisiblePosition() == (totalItemCount - 1)) {
        isScrollToBottom = true;
    } else {
        isScrollToBottom = false;
    }
}

@Override
public boolean onTouchEvent(MotionEvent ev) {
    switch (ev.getAction()) {
        case MotionEvent.ACTION_DOWN:
            downY = (int) ev.getY();
            break;
        case MotionEvent.ACTION_MOVE:
            int moveY = (int) ev.getY();
            int diff = (moveY - downY) / 2;
            int paddingTop = -headerViewHeight + diff;
           super.onTouchEvent(ev);
            if (firstVisibleItemPosition == 0 && -headerViewHeight < paddingTop) {
                if (paddingTop > 0 && currentState == DOWN_PULL_REFRESH) {
                    currentState = RELEASE_REFRESH;
                    refreshHeaderView();
                } else if (paddingTop < 0 && currentState == RELEASE_REFRESH) {
                    currentState = DOWN_PULL_REFRESH;
                    refreshHeaderView();
                }

                headerView.setPadding(0, paddingTop, 0, 0);
                return true;
            }
            break;
        case MotionEvent.ACTION_UP:
            if (currentState == RELEASE_REFRESH) {
                headerView.setPadding(0, 0, 0, 0);
                currentState = REFRESHING;
                refreshHeaderView();

                if (mOnRefreshListener != null) {
                    mOnRefreshListener.onRefresh(); 
                }
            } else if (currentState == DOWN_PULL_REFRESH) {
                // hide header
                headerView.setPadding(0, -headerViewHeight, 0, 0);
            }
            break;
        default:
            break;
    }
    return super.onTouchEvent(ev);
}

private void init() {
    this.setOnScrollListener(this);

    //animation
    upAnimation = new RotateAnimation(0f, -180f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
    upAnimation.setDuration(500);
    upAnimation.setFillAfter(true); 

    downAnimation = new RotateAnimation(-180f, -360f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
    downAnimation.setDuration(500);
    downAnimation.setFillAfter(true); 

    //header
    headerView = View.inflate(getContext(), R.layout.listview_header, null);
    ivArrow = (ImageView) headerView.findViewById(R.id.iv_listview_header_arrow);
    mProgressBar = (ProgressBar) headerView.findViewById(R.id.pb_listview_header);
    tvState = (TextView) headerView.findViewById(R.id.tv_listview_header_state);

    headerView.measure(0, 0); 
    headerViewHeight = headerView.getMeasuredHeight();
    headerView.setPadding(0, -headerViewHeight, 0, 0);
    this.addHeaderView(headerView); 

    //footer
    footerView = View.inflate(getContext(), R.layout.listview_footer, null);
    footerView.measure(0, 0);
    footerViewHeight = footerView.getMeasuredHeight();
    footerView.setPadding(0, -footerViewHeight, 0, 0);
    this.addFooterView(footerView);
}


private void refreshHeaderView() {
    switch (currentState) {
        case DOWN_PULL_REFRESH: // pull down
            tvState.setText("Pull to refresh");
            ivArrow.startAnimation(downAnimation); 
            break;
        case RELEASE_REFRESH: // release
            tvState.setText("Release to refresh");
            ivArrow.startAnimation(upAnimation); 
            break;
        case REFRESHING: // freshing
            ivArrow.clearAnimation();
            ivArrow.setVisibility(View.GONE);
            mProgressBar.setVisibility(View.VISIBLE);
            tvState.setText("Refreshing...");
            break;
        default:
            break;
    }
}

public void hideHeaderView() {
    postDelayed(new Runnable() {
        @Override
        public void run() {
            headerView.setPadding(0, -headerViewHeight, 0, 0);
            ivArrow.setVisibility(View.VISIBLE);
            mProgressBar.setVisibility(View.GONE);
            tvState.setText("Pull to refresh");
            currentState = DOWN_PULL_REFRESH;
        }
    }, 1000);

}

public void hideFooterView() {
    postDelayed(new Runnable() {
        @Override
        public void run() {
            footerView.setPadding(0, -footerViewHeight, 0, 0);
            isLoadingMore = false;
        }
    }, 1000);
}

public interface OnRefreshListener {

    void onRefresh();

    void onLoadMore();
}

public void setOnRefreshListener(OnRefreshListener listener) {
    mOnRefreshListener = listener;
}
}
公共类ExListView扩展ListView实现AbsListView.OnScrollListener{
private int firstVisibleItemPosition;
private int downY;//下拉时的增量Y
私有最终整型下拉刷新=0;
私有最终int版本_REFRESH=1;
私有最终整数=2;
private int currentState=DOWN\u PULL\u REFRESH;
私有OnRefreshListener mOnRefreshListener;
私人动画;
私人动画;
私有视图headerView;
私有图像视图ivArrow;
私人ProgressBar mProgressBar;
私有TextView tvState;//头的状态
private int headerViewHeight;//收割台高度
私有视图页脚视图;//页脚
private int footerViewHeight;//页脚高度
私有布尔IsCrollToBottom;
私有布尔值isLoadingMore=false;
公共ExListView(上下文){
超级(上下文);
init();
}
公共ExListView(上下文、属性集属性){
超级(上下文,attrs);
init();
}
公共ExListView(上下文、属性集属性、int-defStyle){
超级(上下文、属性、定义样式);
init();
}
@凌驾
公共无效onScrollStateChanged(AbsListView视图,int scrollState){
如果(scrollState==SCROLL_STATE_IDLE | | scrollState==SCROLL_STATE_FLING){
如果(IsCrollToBottom&!isLoadingMore){
isLoadingMore=true;
setPadding(0,0,0,0);
this.setSelection(this.getCount());
if(mOnRefreshListener!=null){
mOnRefreshListener.onLoadMore();
}
}
}
}
@凌驾
public void onScroll(AbsListView视图、int firstVisibleItem、int visibleItemCount、int totalItemCount){
firstVisibleItemPosition=firstVisibleItem;
如果(getLastVisiblePosition()==(totalItemCount-1)){
isScrollToBottom=真;
}否则{
isScrollToBottom=假;
}
}
@凌驾
公共事件(MotionEvent ev){
开关(ev.getAction()){
case MotionEvent.ACTION\u DOWN:
downY=(int)ev.getY();
打破
case MotionEvent.ACTION\u移动:
int moveY=(int)ev.getY();
int-diff=(moveY-downY)/2;
int paddingTop=-headerViewHeight+diff;
超级火山口(ev);
if(firstVisibleItemPosition==0&&headerViewHeight0&¤tState==DOWN\u PULL\u REFRESH){
当前状态=释放\刷新;
refreshHeaderView();
}else if(paddingTop<0&¤tState==RELEASE\u REFRESH){
currentState=下拉\刷新;
refreshHeaderView();
}
设置填充(0,paddingTop,0,0);
返回true;
}
打破
case MotionEvent.ACTION\u UP:
如果(当前状态==释放\刷新){
设置填充(0,0,0,0);
当前状态=刷新;
refreshHeaderView();
if(mOnRefreshListener!=null){
mOnRefreshListener.onRefresh();
}
}else if(当前状态==向下拉\u刷新){
//隐藏标题
设置填充(0,-headerViewHeight,0,0);
}
打破
违约:
打破
}
返回super.onTouchEvent(ev);
}
私有void init(){
this.setOnScrollListener(this);
//动画
upAnimation=新旋转动画(0f,-180f,Animation.RELATIVE\u TO\u SELF,0.5f,Animation.RELATIVE\u TO\u SELF,0.5f);
upAnimation.setDuration(500);
upAnimation.setFillAfter(true);
downAnimation=新旋转动画(-180f,-360f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
downAnimation.setDuration(500);
downAnimation.setFillAfter(true);
//标题
headerView=View.inflate(getContext(),R.layout.listview_header,null);
ivArrow=(ImageView)headerView.findViewById(R.id.iv\u列表视图\u标题\u箭头);
mProgressBar=(ProgressBar)headerView.findviewbyd(R.id.pb\u listview\u header);
tvState=(TextView)headerView.findViewById(R.id.tv\u列表视图\u标题\u状态);
headerView.measure(0,0);
headerView=headerView.getMeasuredHeight();
设置填充(0,-headerViewHeight,0,0);
这是addHeaderView(headerView);
//页脚
footerView=View.inflate(getContext(),R.layout.listview\u footer,null);
footerView.measure(0,0);
footerViewHeight=footerView.getMeasuredHeight();
setPadding(0,-footerViewHeight,0,0);
此.addFooterView(footerView);
}
私有void refreshHeaderView(){
开关(当前状态){
案例向下\u拉动\u刷新://下拉
setText(“拉到刷新”);
ivArrow.开始动画(向下动画);
打破
案例发布\刷新://RELEASE
setText(“发布以刷新”);
ivArrow.startAnimation(upAnimation);
打破
案例刷新/刷新
ivArrow.clearAnimation();
ivArrow.setVisibility(View.GONE);
mProgressBar.setVisibility(View.VISIBLE);
tvState.setText(“刷新…”);
打破
违约:
打破
}
}
public void hideHeaderView(){
postDelayed(新的Runnable(){
@凌驾
公开募捐{
设置填充(0,-headerViewHeight,0,0);
ivArrow.setVisibility(View.VISIBLE);
mProgressBar.setVisibility(View.GONE);
setText(“拉到刷新”);
currentState=下拉\刷新;
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<include layout="@layout/navi_bar" />

<LinearLayout
    android:id="@+id/page_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <my.widgets.ExListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <include layout="@layout/empty_view" />
</LinearLayout>
    private boolean touch = false;

    @Override public boolean onTouchEvent (MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                downY = (int) ev.getY();
                touch = true;
                return true;
            case MotionEvent.ACTION_UP:
                if (touch) {
                    touch = false;
                    moveY = (int) ev.getY();
                    diff = (moveY - downY) / 2;
                    //do the rest
                }
                return true;
        }
        return true;
    }
}