Android ImageView's onClickListener does not work

前端 未结 15 1750
情话喂你
情话喂你 2020-12-01 13:30

I have an ImageView for which I wanted to implement the onClickListener. But when I click on the image, nothing happens. Event the Logcat does not show any errors.

F

相关标签:
15条回答
  • 2020-12-01 14:07

    The same thing is happening for me.

    The reason is: I have used a list view with margin Top so the data is starting from the bottom of the image, but the actual list view is overlapping on the image which is not visible. So even if we click on the image, the action is not performed. To fix this, I have made the list view start from the below the image so that it is not overlapping on the image itself.

    0 讨论(0)
  • 2020-12-01 14:09

    can you Try this and tell me what happens ?? :

    ImageView imgFavorite = (ImageView) findViewById(R.id.favorite_icon);
    imgFavorite.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(YourActivityName.this,
                    "The favorite list would appear on clicking this icon",
                    Toast.LENGTH_LONG).show();
        }
    });
    

    or you should add this :

    imgFavorite.setClickable(true); 
    
    0 讨论(0)
  • 2020-12-01 14:11

    Actually I just used imgView.bringToFront(); and it helped.

    0 讨论(0)
  • 2020-12-01 14:12

    Had the same problem, thanks for the Framelayout tip! I was using two overlapped images in a framelayout (the one at top was an alpha mask, to give the effect of soft borders)

    I set in the xml android:clickable="true" for the image I wanted to launch the onClickListener, and android:clickable="false" to the alpha mask.

    0 讨论(0)
  • 2020-12-01 14:15

    For inner classes to pass the reference of activity, I usually create a reference of activity in onCreate() and make it as a member. As inner classes have access to members of parent class you can get hold of your activity and thus its context:

    class MyClass extends Activity{
        private Activity thisActivity;
        @Override
    protected void onCreate(Bundle savedInstanceState) {
                thisActivity = this;
        }
    }
    

    Hope that helps.

    0 讨论(0)
  • 2020-12-01 14:17

    Ok,

    I managed to solve this tricky issue. The thing was like I was using FrameLayout. Don't know why but it came to my mind that may be the icon would be getting hidden behind some other view.

    I tried putting the icon at the end of my layout and now I am able to see the Toast as well as the Log.

    Thank you everybody for taking time to solve the issue.. Was surely tricky..

    0 讨论(0)
提交回复
热议问题