2012-06-11 82 views
2

我想爲我的gridview註冊一個上下文菜單。目前沒有任何反應。 令人驚訝的是,我無法在這裏找到答案。爲android gridview註冊上下文菜單

我的gridview的是main.xml中的RelativeLayout內:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 
<LinearLayout 
    android:id="@+id/bottom_bar" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:gravity="center" 
    android:orientation="horizontal"> 
    <ImageButton 
     android:id="@+id/cameraButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@android:drawable/ic_menu_camera" 
     android:onClick="cameraButtonOnClick"/> 

    <ImageButton 
     android:id="@+id/addGalleryButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/add_gallery" 
     android:onClick="addGalleryButtonOnClick"/> 

    <ImageButton 
     android:id="@+id/addDirButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/add_dir" 
     android:onClick="addDirButtonOnClick"/> 
</LinearLayout> 
<GridView 
    android:id="@+id/gridview" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 

    android:numColumns="auto_fit" 
    android:verticalSpacing="10dp" 
    android:horizontalSpacing="10dp" 
    android:columnWidth="90dp" 
    android:stretchMode="spacingWidthUniform" 
    android:gravity="center" 
    android:layout_above="@id/bottom_bar" 
/> 

在GridView充滿thmbnail.xml:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  xmlns:app="http://schemas.android.com/apk/res/com.ninovanhooff.projectv" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" > 

<ImageButton 
    android:id="@+id/thumbview" 
    android:layout_width="90dp" 
    android:layout_height="90dp" 
    android:layout_centerHorizontal="true" 
    android:layout_centerVertical="true" 


    android:scaleType="fitCenter" 
    android:background="@drawable/thumb_selector" 
    android:src="@drawable/sample_0" /> 

<!-- <TextView 
    android:id="@+id/label" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerInParent="true" 
    android:focusable="false" 
    android:paddingLeft="3dp" 
    android:paddingRight="3dp" 
    android:background="@drawable/label_bg"/> 

<com.ninovanhooff.projectv.ThreeStateCheckBox 
    android:id="@+id/tricheckbox" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:focusable="false" 
    app:label="" 
    app:shadow="false" 
    app:state="0" 
    android:layout_alignParentBottom="true"> 
</com.ninovanhooff.projectv.ThreeStateCheckBox> 
--> 

</RelativeLayout> 

請注意,只有ImageButton的被保留,以簡化問題

上下文菜單註冊如下:

adapter = new ThumbnailAdapter(this); 
    gridView = (GridView) findViewById(R.id.gridview); 
    gridView.setAdapter(adapter); 
    registerForContextMenu(gridView); 

我懷疑它有一些關於android:focusable或android:clickable。但不知道該去哪試。我在main.xml的相關佈局和線性佈局上嘗試了focusable = false和clickable = false,但沒有效果。

回答

0

所以,我得到了這個工作,我的懷疑是對的。其他元素可以將觸摸事件從預定的gridview元素中移走。確保從佈局中移除可點擊的對象,例如按鈕和複選框,以查找罪魁禍首。在我的情況下,我不得不刪除ImageButton和ThreeStateCheckbox。另一個錯誤是我將onclicklisteners添加到ImageButton,而不是gridview的直接子節點(thumbnail.xml)。推薦的方法是使用gridview.setOnItemClickListener(listener))。 現在,這個工作,但我仍然需要我的形象和複選框回來。 ImageButton被一個ImageView所取代,並且我在該複選框中添加了android:focusable =「false」以防止它消耗click事件。一切正常。

如果你不能弄清楚這些東西,那麼層次結構查看器可以很有啓發性。也許你忘記了一些可能會竊取你的觸摸事件的微小元素。祝你好運!