简体   繁体   English

瞬间点击 android

[英]Momentary click in android

i have an adapter with RecyclerView that show some items and everything is ok for showing, in my issue i want click Momentary via OnTouch Method in android我有一个带有 RecyclerView 的适配器,它显示了一些项目,一切都可以显示,在我的问题中,我想通过 android 中的 OnTouch 方法单击 Momentary

            holder.objectImage.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {


                    if(event.getAction() == MotionEvent.ACTION_DOWN){

                        Log.e("test", "ACTION_DOWN");

                        // Do what you want
                        return true;
                    }

                    if(event.getAction() == MotionEvent.ACTION_UP){


                        Log.e("test", "ACTION_UP");


                        // Do what you want
                        return true;
                    }

                    return false;
                }
            });

in this code ACTION_DOWN is working perfect every time but ACTION_UP not working every time在此代码中, ACTION_DOWN每次都运行良好,但ACTION_UP每次都无法正常运行

How i can handle this problem?我该如何处理这个问题?

You can try like the following.您可以尝试如下。

holder.objectImage.setOnTouchListener(new View.OnTouchListener() {
     @Override
     public boolean onTouch(View v, MotionEvent event) {
          if(event.getAction() == MotionEvent.ACTION_DOWN){

             Log.e("test", "ACTION_DOWN");

             // Do what you want
             // return true; remove this line
          }

          else if(event.getAction() == MotionEvent.ACTION_UP){

             Log.e("test", "ACTION_UP");

             // Do what you want
             // return true; remove this line
         }
         return true; // need to return true.
    }
});

Try like this below code:试试下面的代码:

 holder.objectImage.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
                   case MotionEvent.ACTION_DOWN:
                     Log.i("TAG", "touched down");
                   break;
                   case MotionEvent.ACTION_MOVE:
                     Log.i("TAG", "moving: (" + x + ", " + y + ")");                
                   break;
                   case MotionEvent.ACTION_UP:
                     Log.i("TAG", "touched up");
                   break;
             }

             return true;
            }
        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM