OnItemCLickLIstener 不适用于 ListView

2024-04-19

我有一个带有 ListView 的活动。具有自定义视图的 ListView。我将 OnItemClickLIstener 添加到 ListView 中。当我点击项目时,结果我什么也没看到。 ListView 的活动:

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@color/silver_conv">
<FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="40dp" android:layout_alignParentTop="true" android:id="@+id/topcontainer"
        android:background="@color/black">
</FrameLayout>
<ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/chat_list" android:layout_below="@+id/topcontainer"
        android:layout_above="@+id/last_action"
        android:cacheColorHint="#00000000"
        android:layout_marginRight="2dp" android:clickable="true"/>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
          android:text="last action was at time" android:id="@+id/last_action"
          android:longClickable="false"
          android:layout_centerHorizontal="true"
          android:layout_alignParentBottom="false" android:layout_above="@+id/action_container"
          android:layout_marginBottom="5dp" android:layout_alignParentLeft="false" android:layout_marginLeft="5dp"/>
<RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="43dp" android:layout_alignParentBottom="true" android:id="@+id/action_container"
        android:background="@drawable/conv_botom_action_gradient">
    <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/send"
            android:id="@+id/send_message_btn" android:layout_alignParentRight="true"
            android:layout_alignParentBottom="true"
            android:background="@drawable/blue_button_selector" android:textColor="@color/white"
            android:layout_marginLeft="3dp" android:layout_marginTop="3dp"/>
    <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/add_attach_btn"
            android:layout_alignParentLeft="true" android:layout_alignParentBottom="true"
            android:background="@drawable/add_attach_button_selector"
            android:layout_marginRight="2dp" android:layout_marginBottom="3dp" android:layout_marginTop="3dp"/>
    <EditText android:layout_width="40dp" android:layout_height="wrap_content" android:id="@+id/message_et"
              android:layout_toRightOf="@+id/add_attach_btn" android:layout_toLeftOf="@+id/send_message_btn"
              android:singleLine="true" android:layout_alignParentBottom="true" android:hint="Type message here"
              android:background="@drawable/message_input" android:layout_marginBottom="3dp"
              android:gravity="center_vertical" android:layout_marginTop="3dp"/>
</RelativeLayout>

查看项目:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="horizontal"
          android:layout_width="match_parent"
          android:layout_height="wrap_content" android:gravity="center_vertical|left" android:focusable="false">

<LinearLayout
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:background="@drawable/incoming_message"
        android:id="@+id/container" android:layout_marginTop="5dp" android:layout_marginBottom="5dp"
        android:focusable="false">
    <FrameLayout android:layout_width="wrap_content" android:layout_height="wrap_content"
                 android:focusableInTouchMode="true" android:id="@+id/attach_container" android:focusable="false"/>
    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="saa"
            android:id="@+id/message_text" android:textSize="17sp" android:textColor="@color/black"
            android:focusable="false"/>
</LinearLayout>
<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Text"
        android:id="@+id/date" android:textColor="@color/black" android:singleLine="true" android:lines="1"
        android:maxLines="1" android:ellipsize="none" android:layout_marginLeft="5dp" android:focusable="false"/>

最后点击监听器:

private AdapterView.OnItemClickListener clickLister = new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int pos, long l) {
        try {
            LinearLayout container = (LinearLayout)view.findViewById(R.id.container);
            TextView message = (TextView)container.findViewById(R.id.message_text);
            message.setTextColor(mContext.getResources().getColor(R.color.white));
            Log.e("My item is", "" + pos);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
};

这是 ListView 的初始化:

mConvListView = (ListView)findViewById(R.id.chat_list);
    mConvListView.setDivider(null);
    mConvListView.setDividerHeight(0);
mConvListView.setItemsCanFocus(false);
    mConvListView.setOnItemClickListener(clickLister);
    mConvListView.setTranscriptMode(ListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
    mConvListView.setStackFromBottom(true);

附注抱歉有很多代码。但第二天我找不到任何建议。


通过在行布局中设置可聚焦对象,可以防止 ListView 获取触摸事件。

此 FrameLayout 正在消耗触摸事件:

<FrameLayout 
    android:id="@+id/attach_container" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:focusableInTouchMode="true" 
    android:focusable="false"/>

删除可聚焦设置,使其看起来像这样:

<FrameLayout 
    android:id="@+id/attach_container" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" />

(You really should organize your XML so that is is readable, in Eclipse use Ctrl+Shift+F.)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

OnItemCLickLIstener 不适用于 ListView 的相关文章

  • Android 日历视图字体大小

    我正在使用 Xamarin 构建 Android 应用程序 这是我的第一个应用程序 我有一个CalendarView我需要更改当天的字体大小和样式 我尝试了下面的代码 字体颜色改变了 但尺寸太小了 最低 API 级别为 11 目标 API
  • HttpURLConnection 发送图像、音频和视频文件,参数可以是(String 或 Json String)Android

    我正在分享solution发送一个image audio or a video带有参数的文件使用HttpURL连接 参数可以是 纯字符串或 JSON Android 客户端到 PHP 后端 设想 必须上传媒体文件 带参数的音频 视频和图像
  • 如何在 AndroidEnvironment 构建文件的环境变量中指定下载目录的路径

    使用 Visual Studio 2022 17 2 0 Preview 1 0 我目前正在尝试创建一个 MAUI 应用程序 该应用程序已经在 Windows 上运行良好 也可以在 Android 下运行 该应用程序使用一个库 SaxonC
  • 运行时异常无法在未调用 Looper.prepare 的线程内创建处理程序错误

    我正在尝试上传带有其他一些 EditText 的照片 我从在线示例中获取了示例代码并对其进行了一些编辑 但是 我收到此错误 08 29 21 36 46 000 E AndroidRuntime 4566 FATAL EXCEPTION A
  • 使用 Android NDK 使用 -fsigned-char 进行构建安全吗?

    为了与其他平台保持一致 我需要使用signed char在我正在处理的一些本机代码中 但默认情况下在Android NDK上char类型是unsigned 我尝试明确使用signed char类型 但它生成太多警告differ in sig
  • 在 Android 中向下滚动列表视图时强制关闭

    当我尝试在片段活动中向下滚动列表视图时 出现强制关闭错误 其中有 1 个图像视图和 2 个文本视图 我是android的初学者 所以除了android最常用组件的基本场景之外没有太多的知识 没有位图 OOM 错误 因为我也没有使用图像进行了
  • 使用 ActionBar 选项卡进行导航时菜单会折叠

    我已经使用支持库中的 ActionBar 来将我的应用程序构建为选项卡式导航栏 我的应用程序中有两个选项卡 这两个片段都有菜单 并且有一个菜单项 我想将其显示为操作栏中的一项操作 但由于某种原因 显示了溢出图标 而不是分配给这些项目的图标
  • Urban Airship:默认状态栏通知的自定义图标

    Urban Airship 建议创建自定义通知CustomPushNotificationBuilder如果您想对状态栏通知进行任何修改 包括简单地更改图标 不幸的是 使用RemoteView因为通知会带来许多与定制制造商和 或特定于平台的
  • 在Android市场发布测试版

    我想要publish a 测试版我在 Android 市场上的一些应用程序 面临着我无法两次上传具有相同包名的应用程序的问题 即使使用不同的证书也是如此 但是发布版和测试版有 2 个包名会带来很多麻烦 例如每次都必须更改对 R 不同位置的引
  • 动态添加导航抽屉中的项目

    我创建了抽屉 但是我想动态设置抽屉的项目列表 意味着从数据库获取数据并设置为抽屉列表 是否可以 是的 比如何 我也知道静态抽屉 尝试这个 final Menu menu navigationView getMenu for int i 1
  • 如何在android中播放音频文件

    我的 Android 手机中有一个 mp3 文件 让它在我的 SD 卡中的某个位置成为 xyz mp3 如何通过我的应用程序播放它 只需您就可以使用MediaPlayer并播放音频文件 查看这个很好的例子 http www helloand
  • android 如何延迟执行

    我正在构建一款具有人工智能功能的安卓棋盘游戏 AI 轮流执行 必须调用一系列操作 然后将无效信息发布到我的自定义视图中进行更新 我需要放慢这些动作的速度 以便用户能够看到人工智能正在轮到它 而不是它一闪而过 我已经尝试过一些类似的事情 tr
  • Android ImageView未加载

    我正在使用 android imageView 并将图像放入可绘制文件夹中 并将 imageView 源更改为该图像 但它没有在预览面板中显示图像 当我在 android studio 中打开图片时 它显示这样的错误 但我可以在电脑桌面上打
  • 为什么我无法在 Android 上从串口打开/写入?

    我编写了一个 Android 应用程序 它在 Android 4 4 Kitkat 设备上的自定义内核上运行 该设备使用 Android 串行端口 API https code google com p android serialport
  • 在处理器生成的类中使用库

    我正在开发一个库来使用注释和处理器生成类 生成的类应该使用Gson来自谷歌的图书馆 我的问题是 我应该在哪里添加 Gson 依赖项 我目前正在将其添加到处理器 build gradle 中 但是当生成类时 找不到 Gson Android
  • Android sqlite插入记录如果不存在

    我想将一个新项目 Cheese 添加到 sqlite 表中 但前提是它不存在 我的表中只有两列 id KEY ROWID PR 和product name KEY NAME PR 我一直在尝试使用这些代码 但它给了我一个错误 public
  • 使用“adb devices”命令无法找到 Android 设备

    我正在开发Android申请于macOS我的应用程序在模拟器上运行良好 我想在设备上运行它 但是当我运行时adb devices我什么也没得到 localhost platform tools BF adb devices List of
  • 各种 Android 设备的应用程序背景大小

    我正在为所有 Android 设备的应用程序设计背景 我在想图像的大小 以像素为单位 是多少 从开发者网站我发现了以下等式 px dp dpi 160 那么 px 取决于两个变量 首先 dp 我们有 xlarge screens are a
  • 从 Android 应用程序调用 Google 地图应用程序以获取行车方向

    我需要使用外部谷歌地图应用程序显示行车方向我找到了这个链接http developer android com guide appendix g app intents html http developer android com gui
  • 如何移动随 Visual Studio 2017 安装的 Android SDK 文件夹?

    我已将 VS 2017 安装在其默认路径 C 驱动器 中 但由于 Android 文件夹较大 它耗尽了我的大部分驱动器空间C Users USERNAME Local Android 该文件夹是由 VS 2017 安装程序自动创建的 如何将

随机推荐