Android应用如何在视频视图中触发长按事件

时间:2013-07-24 10:16:51

标签: java android android-layout android-imageview android-view

我在一天内对这个问题进行了调查。 我的意思是如何在点击Videoview一段时间后显示吐司。

以下是我发现的内容,

Android: Why can't I give an onClickListener to a VideoView?

detect double tap (Double click) or long click in a videoview

但这些真的无法解决我的问题。我真的不知道发生了什么事? 是否有任何功能可以在视频视图中启动长按事件?

这是我的代码

这两个事件真的无法奏效。

    mVideoView.setOnLongClickListener(new OnLongClickListener() {

        public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                final int arg2, long arg3) {
            Log.e("devon","onitemlongclick");
            return true;
        }

        @Override
        public boolean onLongClick(View v) {
            Log.e("devon","onLongClick");
            return true;
        }

    });

需要帮助!!!谢谢!

2 个答案:

答案 0 :(得分:3)

  • setupViewComponent来电
  • 中添加OnLongClickListener
  • 尝试使用onTouch
  • 尝试将OnLongClickListener附加到视频视图的表面
  • 尝试使用透明的imageview /抓取焦点的东西包装视频,并将其用作“触摸板”
  • post logcat。

答案 1 :(得分:0)

这是一个示例示例,说明如何创建自己的TouchListsners来管理VideoView上的Click和LongClick。在此示例中,我将单击的数据的idex和视图的索引传递给侦听器(在后面,我在数组中有多个VideoView映射到数据列表,例如在Adapter中)

/**
 * Simple OnTouchListenerIndexed with Indexes for VideoView ClickListeners 
 * You have to handle longClick and click by yourself 
 */
private abstract class OnTouchListenerIndexed implements OnTouchListener {
    private static final int LONG_CLICK_DURATION=600;//in millis
    int dataIndex = INDEX_NOT_DEFINED;
    int imageViewIndex = INDEX_NOT_DEFINED;
    long timeActionDown;
    AtomicBoolean stillNotConsumed=new AtomicBoolean(true);
    AtomicBoolean actionDone=new AtomicBoolean(false);

    public OnTouchListenerIndexed(int dataIndex, int imageViewIndex) {
        this.dataIndex = dataIndex;
        this.imageViewIndex = imageViewIndex;
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if(event.getAction() == MotionEvent.ACTION_DOWN){
            timeActionDown=System.currentTimeMillis();
            stillNotConsumed.set(true);
            actionDone.set(false);
            //launch LongClick in 1s
            v.postDelayed(new Runnable() {
                @Override
                public void run() {
                    if(stillNotConsumed.get()){
                        stillNotConsumed.set(false);
                        actionDone.set(true);
                        onLongTouch(dataIndex, imageViewIndex);
                    }
                }
            },LONG_CLICK_DURATION);
            //consumed
            return true;
        }else if(event.getAction() == MotionEvent.ACTION_UP){
            long timeActionUp=System.currentTimeMillis();
            stillNotConsumed.set(false);
            if(actionDone.get()){
                //do nothing
                return true;//you have consumed it
            }else {
                actionDone.set(true);
                //Check Click or LongClick
                if (timeActionUp - timeActionDown > LONG_CLICK_DURATION) {
                    //une seconde plus tard
                    return onLongTouch(dataIndex, imageViewIndex);
                } else {
                    return onTouch(dataIndex, imageViewIndex);
                }
            }

        }else{
            //don't consume it
            return false;
        }
    }


    public abstract boolean onTouch(int dataIndex, int imageViewIndex);
    public abstract boolean onLongTouch(int dataIndex, int imageViewIndex);
}