imageview上的setOnClickListener需要单击两次才能工作

时间:2017-07-13 08:04:30

标签: android onclick imageview listener

我在我的活动中ImageView使用了以下属性我已经对imageview进行了初始化,并在OnClickListener上设置了ImageView,但是当我第一次点击Imageview时,onlick事件不起作用但是当我第二次点击Imageview时它可以工作

                  <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" />

我的活动类文件中有以下代码:

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事件

 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,但它也没有帮助我,请告诉我可以执行哪些操作来解决此问题。

2 个答案:

答案 0 :(得分:0)

我认为问题在于您的条件,每次点击都会调用点击,尝试按照代码(点击后显示吐司):

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

                    if (switchStatus.equals("video")) {
                            Toast.makeText(getApplicationContext(), "switchStatus changed to video", Toast.LENGTH_LONG).show();

                        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")) {

                            Toast.makeText(getApplicationContext(), "switchStatus image has clicked", Toast.LENGTH_LONG).show();

                        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 {

                            Toast.makeText(getApplicationContext(), "switchStatus was blank, changed to image.", Toast.LENGTH_LONG).show();

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

答案 1 :(得分:0)

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