ImageView over gallery not clickable
I have a full screen gallery with setOnItemClickListener
and setOnItemSelectedListener
. Over that gallery I have a linear Layout with 3 ImageViews. I have allready mde the imageviews clickable, but when I click on them, the gallery.setOnItemClickListener
is what is activated. How can I get he ImageViews clickable??
Tha layout complete layot is verry complex so I'm putting an image of the relevant part.
In the code I have:
ImageView prev = (ImageView) findViewById(R.id.previousImage);
ImageView play = (ImageView) findViewById(R.id.playAllImages);
ImageView next = (ImageView) findViewById(R.id.nextImage);
prev.setClickable(true);
prev.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
YADA YADA YADA ...
}
});
next.setClickable(true);
next.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
YADA YADA YADA ...
}
});
play.setClickable(true);
play.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
YADA YADA YADA ...
}
});
I also have the listeners of the gallery:
_horizGallery.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
YADA YADA YADA...
}
});
_horizGallery.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView parent, View view,
int position, long id) {
YADA YADA YADA...
}
public void onNothingSelected(AdapterView parent) {
YADA YADA YADA...
}
});
The xml is something like this (I took out some non relevant parts to make it smaller for practical use):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:orien开发者_JAVA百科tation="horizontal"
android:layout_height="wrap_content" android:layout_width="match_parent"
android:background="#11000000" android:layout_alignParentBottom="true"
android:weightSum="1.0" android:id="@+id/layoutToScrollDown">
<LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content"
android:orientation="horizontal" android:layout_weight="0.1" android:gravity="center">
<ImageView android:layout_width="30dp" android:layout_height="30dp" android:src="@drawable/icon"
android:padding="1px" android:id="@+id/previousImage"> </ImageView>
</LinearLayout>
<LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content"
android:orientation="horizontal" android:layout_weight="0.4" android:gravity="center">
<ImageView android:layout_width="30dp" android:layout_height="30dp" android:src="@drawable/icon"
android:padding="1px" android:id="@+id/playAllImages"> </ImageView>
</LinearLayout>
<LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content"
android:orientation="horizontal" android:layout_weight="0.1" android:gravity="center">
<ImageView android:layout_width="30dp" android:layout_height="30dp" android:src="@drawable/icon"
android:padding="1px" android:id="@+id/nextImage"> </ImageView>
</LinearLayout>
</LinearLayout>
<com.pixable.android.ModifiedGallery xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/com.pixable.android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:id="@+id/HorizontalGallery"
android:gravity="center_vertical" android:spacing="2px"/>
It's all inside a relative view that for some reason is not beine shown.
I think because your call to findViewById
only relates to the outer parent view i.e. your gallery, anywhere you click on it will trigger off the gallery's itemClickListener.
Also I think that wrapping each ImageView in its own LinearLayout is causing problems. You should consider to just have 1 LinearLayout with the ImageViews inside of it.
What you need to do then i think is to inflate the layout and then retrieve the ImageView's from that View to add listeners to it.
View v = LayoutInflater.from(context).inflate(R.id.layoutToScrollDown,null);
or inflate the view as follows:
View v = View.inflate(context,R.id.layoutToScrollDown,null);
or maybe even try findviewbyid:
View v = findViewById(R.id.layoutToScrollDown);
and then get ImageViews:
ImageView prev = (ImageView) v.findViewById(R.id.previousImage);
ImageView play = (ImageView) v.findViewById(R.id.playAllImages);
ImageView next = (ImageView) v.findViewById(R.id.nextImage);
You can then continue following your code as normal.
I hope this helps!
You gallery is below your images in your layout XML file. This means the gallery is in front of your images in terms of clicking.
You need to place the gallery component before any of the images. Consider using a RelativeLayout
as the outer element. Place the gallery component before the LinearLayout
with the images in. Add android:layout_alignParentTop="true"
to the gallery, and add android:layout_alignParentBottom="true"
to the LinearLayout
container.
Also there's no need to wrap each ImageView
inside a LinearLayout
- that will just make drawing the UI of your app slower.
精彩评论