0

我有一个带有 OnItemClickListener 和 OnItemLongClickListener 的列表视图。

在我的 listadapter 的 getView 方法中,我设置了一个带有 TransitionDrawable 作为背景的视图,接下来我将一个 OnTouchListener 应用到该视图,这样当用户按住视图并按住它时,他将看到转换发生。

问题是 OnTouchListener 必须返回 true,否则只会捕获 ACTION_DOWN 运动事件。但是通过返回 true,我的 OnItemClickListener 和 OnItemLongClickListener 不会被触发。

问题是如何既显示 TransitionDrawable 的动画又触发点击事件?

数组适配器的 getView(..):

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    ...

    convertView.setOnTouchListener(touchListener);
    return convertView;
}

OnTouch监听器:

OnTouchListener touchListener = new OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {         
        TransitionDrawable transition = (TransitionDrawable)v.getBackground();

        if(event.getAction() == MotionEvent.ACTION_DOWN) {
            transition.startTransition(ViewConfiguration.getLongPressTimeout());
            return true;
        }
        else {
            transition.resetTransition();
        }

        return false;
    }
};
4

1 回答 1

0

我最终自己创建了一个单击和长按的界面,并在 onTouchListener 中处理它们:

private class TransitionView implements OnTouchListener {
    private Object object;
    TransitionDrawable transition;
    CountDownTimer timer;
    boolean running;
    View v;

    public TransitionView(Object object) {
        this.object = object;

        int time = ViewConfiguration.getLongPressTimeout();
        timer = new CountDownTimer(time, time) {

            @Override
            public void onTick(long millisUntilFinished) { }

            @Override
            public void onFinish() {
                running = false;
                transition.resetTransition();
                longClickListener.onItemLongClick(TransitionView.this.object, v);
            }
        };
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        this.v = v;

        transition = (TransitionDrawable)v.getBackground();

        if(event.getAction() == MotionEvent.ACTION_DOWN) {
            transition.startTransition(ViewConfiguration.getLongPressTimeout());
            timer.start();
            running = true;
            return true;
        }
        else if(event.getAction() == MotionEvent.ACTION_UP) {
            if(running) {
                timer.cancel();
                running = false;
                itemClickListener.onListItemClick(object, v);
            }
            transition.resetTransition();
        }
        else if(event.getAction() == MotionEvent.ACTION_CANCEL) {
            transition.resetTransition();
        }

        return false;
    }
}
于 2013-08-13T07:40:40.043 回答