2011-04-06 103 views
1

我有一个全屏画廊与setOnItemClickListenersetOnItemSelectedListener。在这个画廊我有一个线性布局3个ImageViews。我已经准备好了可以点击的图像浏览器,但是当我点击它们时,gallery.setOnItemClickListener就是被激活的。我怎样才能让他ImageViews点击?ImageView不在画廊中可点击

塔布局完整的layot是非常复杂的,所以我把相关部分的图像。

LayoutDescription

在代码中,我有:

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

我也有画廊的听众:

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

的XML是这样的(我拿出一些非相关部件使其更小以供实际使用):

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout android:orientation="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"/> 

这一切都因为某些原因没有beine显示相对视图中。

回答

0

您的图库位于布局XML文件中的图像下方。这意味着画廊在点击方面处于您的图像前方。

您需要将图库组件放置在任何图像之前。考虑使用RelativeLayout作为外部元素。将图库组件放在LinearLayout之前,并将图像添加到图库中。将android:layout_alignParentTop="true"添加到图库中,并将android:layout_alignParentBottom="true"添加到LinearLayout容器中。

此外,不需要将每个ImageView都包含在LinearLayout之内 - 这只会让您的应用程序的UI变慢。

+0

比你,让我试试看,看看它是怎么回事,但我已经移动,所以如果它解决了我要杀死自己的问题。关于额外的线性布局,在我的应用程序中,我有5个图像(问题背景中的代码比需要的更大),所以我简化了实际使用,布局需要以必要的间距显示图像。 – 2011-04-07 00:44:05

+0

它的工作。谢谢。现在我必须自杀(开玩笑)。奇怪的是,我最初在相对布局的按钮上有图像(我甚至打开应用程序的最后一个版本,只是为了让它在问之前像这样)。 Eclipse必须有一个解析问题(这个问题已经多次发生在我身上)。 – 2011-04-07 00:54:43

1

我认为,因为您致电findViewById只与外部父视图相关,即您的画廊,您点击任何地方都会触发画廊的itemClickListener。

另外我认为将每个ImageView包装在自己的LinearLayout中会导致问题。您应该考虑在其中包含一个带有ImageView的LinearLayout。

你需要做什么然后我认为是膨胀的布局,然后从该视图检索ImageView的添加监听器。

View v = LayoutInflater.from(context).inflate(R.id.layoutToScrollDown,null); 

或膨胀的观点如下:

View v = View.inflate(context,R.id.layoutToScrollDown,null); 

,或者甚至尝试findviewbyid:

View v = findViewById(R.id.layoutToScrollDown); 

,然后得到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); 

然后,您可以继续跟着你的代码正常。

我希望这有助于!

+0

这是一个很好的主意,我会在尝试@Joseph opton之后尝试。它不添加代码,使应用程序更快。我不会想到这一点,所以非常感谢你,可能会对未来有所帮助。 – 2011-04-07 00:47:29