【问题标题】:android- How to detect double tap on imageview and start new fragmentandroid-如何检测图像视图上的双击并启动新片段
【发布时间】:2014-08-30 05:15:49
【问题描述】:

我对该主题进行了大量搜索,甚至参考了 android 开发者网站的官方文档。但仍然不清楚这个概念。

我已经读过,为了实现触摸手势,我需要使用 GestureDetector 和 MotionEvent 包。但它的实现让我对这些事情感到困惑。

我想要的是,我的布局包括许多文本视图以及两个图像视图。我想检测对我的图像的双击,并想开始一个新的片段活动。在新的片段活动中,我想以横向模式全屏显示相同的图像。

我读了很多书,但这让我很困惑。 请帮忙。 谢谢

【问题讨论】:

  • 请展示您尝试过的内容。那里有几个可用的示例。
  • 参考这个链接:stackoverflow.com/questions/937313/…你会得到它。
  • @SilentKiller 我还没有尝试过它。我只是想先学习它
  • @user3794646 那么你应该先做谷歌搜索然后应该在 SO 上发布问题。 :)

标签: android android-fragments imageview motionevent gesturedetector


【解决方案1】:

这里是双击手势 ImageView。

public class CustomImageView extends ImageView {
    private Context context;
    private GestureListener mGestureListener;
    private GestureDetector mGestureDetector;

    public CustomImageView(Context context) {
        super(context);
        sharedConstructing(context);
    }

    public CustomImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        sharedConstructing(context);
    }

    public CustomImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        sharedConstructing(context);
    }

    private void sharedConstructing(Context context) {
        super.setClickable(true);
        this.context = context;
        mGestureListener=new GestureListener();
        Log.e("Adding", "Listener:::");
        mGestureDetector = new GestureDetector(context, mGestureListener, null, true);
        setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                mGestureDetector.onTouchEvent(event);
                //..my other code logic
                invalidate();
                return true; // indicate event was handled
            }

        });
    }

    public class GestureListener extends GestureDetector.SimpleOnGestureListener {

        @Override
        public boolean onDoubleTap( MotionEvent e ) {
            // TODO DoubleTap Comparison 
            Log.e("onDoubleTap","onDoubleTap");
            return true;
        }
    }

}

Reference Link

【讨论】:

  • 我想将它添加到特定的 imageview 对象上。上面的代码会朝着期望的方向工作吗?
  • 我想做一些像 android 在设置中做的事情来进入开发者模式。如何实现。如果用户在图像视图或任何视图上点击 7 次。然后只做某些事情,否则什么都不做。
猜你喜欢
  • 2011-02-08
  • 1970-01-01
  • 1970-01-01
  • 2015-03-01
  • 2013-05-03
  • 2017-10-25
  • 1970-01-01
  • 2011-01-14
  • 2015-11-09
相关资源
最近更新 更多