android listview:使用longclicklistener进行单元格转换

时间:2013-08-12 15:32:26

标签: android listview transitiondrawable

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

在我的listadapter的getView方法中,我设置了一个以TransitionDrawable为背景的视图,然后我将OnTouchListener应用于视图,这样当用户按下视图并保持它时,他将看到转换发生。

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

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

arrayadapter的getView(..):

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

    ...

    convertView.setOnTouchListener(touchListener);
    return convertView;
}

OnTouchLIstener:

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;
    }
};

1 个答案:

答案 0 :(得分: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;
    }
}