我正在尝试为我的 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 的 relativelayout 和 linearlayout 上都尝试了 focusable = false 和 clickable = false,但没有效果。
最佳答案
所以,我让它起作用了,我的怀疑是正确的。从预期的 gridview 元素窃取触摸事件的其他元素。确保从您的布局中删除按钮和复选框等可点击对象以找到罪魁祸首。就我而言,我必须删除 ImageButton 和 ThreeStateCheckbox。另一个错误是我将 onclicklisteners 添加到 ImageButton 而不是 gridview (thumbnail.xml) 的直接子级。推荐的方法是使用 gridview.setOnItemClickListener(listener))。 现在,这有效,但我仍然需要我的图像和复选框。 ImageButton 被 ImageView 取代,我在复选框中添加了 android:focusable="false"以防止它消耗点击事件。一切正常。
如果您无法弄清楚这些东西,hierarchyviewer 可能会很有启发性。也许您忘记了一些可能会窃取您的触摸事件的微小元素。祝你好运!
关于android - 为 android gridview 注册上下文菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10984243/