android - 为 android gridview 注册上下文菜单

标签 android gridview contextmenu

我正在尝试为我的 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/

相关文章:

android - Android WebView HTML 会影响键盘或键盘功能吗?

ASP.net gridview 图像按钮替代文本

javascript - Yii2:在单击按钮时更改 Gridviews 的 DataProvider

c# - 使用 GridView 中的分页在 UWP 中加载图像

c# - 如何在 Winforms 中更改 ToolStripItem 的选中状态?

android - 在 Android 上,图书馆是否能够收到应用程序后台/前台的通知?

Android:android 可搜索与自定义搜索 Activity

android - 有没有办法在 Android API 中播放原始音频/视频流?

c# - 窗口中的深色系统上下文菜单

c++ - 如何通过Qt中的菜单锁定所有工具栏?