imageview上的setOnClickListener需要单击两次才能工作 [英] setOnClickListener on imageview needs to click two times in order to work

查看:100
本文介绍了imageview上的setOnClickListener需要单击两次才能工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的活动中具有ImageView,具有以下属性,我已经初始化了imageview并还在ImageView上设置了OnClickListener,但是当我第一次单击Imageview时,onlick事件不起作用,但是当我单击时在Imageview上第二次使用

I have ImageView in my activity with the following properties i have innitialized the imageview and also set OnClickListener on ImageView, but when i click on Imageview for the first time, onlick event doesnot work but when i click on Imageview for the second time it works

                  <ImageView
                        android:clickable="true"
                        android:focusable="false"
                        android:id="@+id/switchIcon"
                        android:layout_width="50dp"
                        android:layout_height="45dp"
                        android:layout_alignParentRight="true"
                        android:layout_below="@+id/title"
                        android:layout_weight="0.1"
                        android:scaleType="fitXY"
                        android:src="@drawable/ic_play_circle_filled_black_24dp" />

我的活动课程文件中包含以下代码:

i have following code in my activity class file :

ImageView switchIcon;
switchIcon = (ImageView) findViewById(R.id.switchIcon);


 switchIcon.setOnClickListener(new View.OnClickListener() {
            @Override
            public boolean onClick(View v) {

                    if (switchStatus.equals("video")) {
                        switchStatus = "image";
                        LayoutImage.setVisibility(View.VISIBLE);
                        LayoutVideo.setVisibility(View.GONE);
                        mPlayer.pause();
                        Glide.with(getApplicationContext()).load(R.drawable.ic_play_circle_filled_black_24dp).into(switchIcon);
                    } else if (switchStatus.equals("image")) {
                        if (requestData.get("video_link").equals("")) {
                            Toast.makeText(getApplicationContext(), "No Video Found for this Recipe", Toast.LENGTH_LONG).show();
                        } else {
                            switchStatus = "video";
                            LayoutImage.setVisibility(View.GONE);
                            LayoutVideo.setVisibility(View.VISIBLE);
                            mPlayer.play();
                            Glide.with(getApplicationContext()).load(R.drawable.ic_photo_size_select_actual_black_24dp).into(switchIcon);
                        }
                    } else {
                        switchStatus = "image";
                        LayoutImage.setVisibility(View.VISIBLE);
                        LayoutVideo.setVisibility(View.GONE);
                        mPlayer.pause();
                        Glide.with(getApplicationContext()).load(R.drawable.ic_play_circle_filled_black_24dp).into(switchIcon);
                    }
            }
        });

我还尝试添加OnTouchListener(),因为OnClickListener()对此不起作用,但是OnTouchListener()也不与ImageView一起使用,它还需要两次单击才能获得onclick事件

I also tried adding the OnTouchListener() as OnClickListener() was not working with this, but OnTouchListener() is also not working with ImageView it also needs two clicks in order to get onclick event

 switchIcon.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if(event.getAction() == MotionEvent.ACTION_UP){
                    if (switchStatus.equals("video")) {
                        switchStatus = "image";
                        LayoutImage.setVisibility(View.VISIBLE);
                        LayoutVideo.setVisibility(View.GONE);
                        mPlayer.pause();
                        Glide.with(getApplicationContext()).load(R.drawable.ic_play_circle_filled_black_24dp).into(switchIcon);
                    } else if (switchStatus.equals("image")) {
                        if (requestData.get("video_link").equals("")) {
                            Toast.makeText(getApplicationContext(), "No Video Found for this Recipe", Toast.LENGTH_LONG).show();
                        } else {
                            switchStatus = "video";
                            LayoutImage.setVisibility(View.GONE);
                            LayoutVideo.setVisibility(View.VISIBLE);
                            mPlayer.play();
                            Glide.with(getApplicationContext()).load(R.drawable.ic_photo_size_select_actual_black_24dp).into(switchIcon);
                        }
                    } else {
                        switchStatus = "image";
                        LayoutImage.setVisibility(View.VISIBLE);
                        LayoutVideo.setVisibility(View.GONE);
                        mPlayer.pause();
                        Glide.with(getApplicationContext()).load(R.drawable.ic_play_circle_filled_black_24dp).into(switchIcon);
                    }

                    return true;
                }
                return false;
            }
        });

我也尝试将ImageView更改为ImageButton,但这也无济于事,请告诉我我可以采取什么行动来解决此问题.

I also tried changing ImageView to ImageButton but it also does not help me out, please tell me what action can i perform to solve this problem.

推荐答案

我将这个答案放在这里,以防某些人陷入同一问题并且起因与我相同. 我的原因是布局中的另一个视图,该视图在发生触摸事件后仍保持焦点,因为在实现其触摸监听程序时,我忘记将返回值从false更改为true.结果是我必须在触摸另一个视图后单击两次图像视图,以便在调用图像视图上的onclick事件之前将焦点返回到布局. 因此,通常应确保在布局上没有其他视图可能在其发生on touch事件后就保持焦点,在这种情况下,其onTouch方法的实现应返回true而不是默认false值.

I put this answer here in case some one lands on this same problem and has the same cause as mine. The cause of mine was another view in my layout which retained focus after an on touch event on it because I forgot to change the return value from false to true when implementing its on touch listener. The consequence was that I had to click my image view twice after touching the other view for focus to be returned to the layout before the onclick event on the image view is called. So in general ensure that no other view in layout retains focus perhaps after an on touch event on it in which case the implementation of its onTouch method should return true and not the default false value.

这篇关于imageview上的setOnClickListener需要单击两次才能工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆