setOnClickListener on imageview needs to click two times in order to work

我是研究僧i 提交于 2020-01-05 03:54:06

问题


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

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

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.


回答1:


I think the issue is with your conditions and click is getting called on each click, try following code (show toast on click) :

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



回答2:


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.



来源:https://stackoverflow.com/questions/45074852/setonclicklistener-on-imageview-needs-to-click-two-times-in-order-to-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!