2

グリッドビューのコンテキストメニューを登録しようとしています。現時点では何も起こりません。驚いたことに、ここで答えを見つけることができませんでした。

私のgridviewはmain.xmlrelativelayout内にあります:

<?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"  
/>

グリッドビューは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>

問題を単純化するために、画像ボタンのみが保持されることに注意してください

コンテキストメニューは次のように登録されます。

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

android:focusableまたはandroid:clickableと関係があるのではないかと思います。しかし、どちらを試すべきかわからない。main.xmlのrelativelayoutとlinearlayoutの両方でfocusable=falseとclickable=falseを試しましたが、効果はありませんでした。

4

1 に答える 1

0

それで、私はこれを機能させました、そして私の疑いは正しかったです。意図したgridview要素からタッチイベントを盗む他の要素。犯人を見つけるために、レイアウトからボタンやチェックボックスなどのクリック可能なオブジェクトを必ず削除してください。私の場合、ImageButtonとThreeStateCheckboxの両方を削除する必要がありました。もう1つの間違いは、gridview(thumbnail.xml)の直接の子ではなく、onclicklistenersをImageButtonに追加したことです。推奨されるアプローチは、gridview.setOnItemClickListener(listener))を使用することです。これで機能しますが、画像とチェックボックスを戻す必要があります。ImageButtonはImageViewに置き換えられ、クリックイベントを消費しないようにチェックボックスにandroid:focusable="false"を追加しました。そして、すべてが正常に動作します。

これを理解できない場合は、hierarchyviewerが啓蒙的である可能性があります。タッチイベントを盗む可能性のある小さな要素を忘れたのかもしれません。幸運を!

于 2012-06-12T07:37:53.017 に答える