2011-04-06 109 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