【问题标题】:Android List Item with Image View not clickable带有图像视图的 Android 列表项不可点击
【发布时间】:2015-12-02 15:41:44
【问题描述】:

我有一个带有ImageView's、TextView's 的列表项。问题是整个列表项不可点击,只有当我点击任何ImageView 时它才能点击。我为每个孩子设置了 android:descendantFocusability="blocksDescendants" 为 root 及以下

android:focusable="false"
android:focusableInTouchMode="false"

仍然只有ImageViews 是可点击的。下面是我的列表项 xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://splashurl.com/m22ydvb
    android:id="@+id/itemdisplaylist_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:descendantFocusability="blocksDescendants"
    android:minHeight="@dimen/loclist_item_minumum_height"
    android:orientation="horizontal"
    android:weightSum="1" >

    <ImageView
        android:id="@+id/listitem_pic"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="0.2"
        android:contentDescription="@null"
        android:focusable="false"
        android:focusableInTouchMode="false" />

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="0.7"
        android:descendantFocusability="blocksDescendants"
        android:orientation="vertical"
        android:weightSum="1" >

        <TextView
            android:id="@+id/listitem_name"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="0.3"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:textColor="@color/loclistitem_text"
            android:textIsSelectable="true" >
        </TextView>

        <TextView
            android:id="@+id/listitem_address"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="0.5"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:textIsSelectable="true" >
        </TextView>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="0.2"
            android:descendantFocusability="blocksDescendants"
            android:orientation="horizontal" >

            <TextView
                android:id="@+id/listitem_text2"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="0.4"
                android:focusable="false"
                android:focusableInTouchMode="false"
                android:textColor="@color/loclistitem_text"
                android:textIsSelectable="true" />

            <ImageView
                android:id="@+id/listitem_prop1"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="0.2"
                android:contentDescription="@null"
                android:focusable="false"
                android:focusableInTouchMode="false" />

            <ImageView
                android:id="@+id/listitem_prop2"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="0.2"
                android:contentDescription="@null"
                android:focusable="false"
                android:focusableInTouchMode="false" />

            <ImageView
                android:id="@+id/listitem_prop3"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="0.2"
                android:contentDescription="@null"
                android:focusable="false"
                android:focusableInTouchMode="false" />
        </LinearLayout>
    </LinearLayout>

    <ImageView
        android:id="@+id/listitem_img3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.1"
        android:contentDescription="@null"
        android:focusable="false"
        android:focusableInTouchMode="false" 
        android:clickable="false"/>

</LinearLayout>

我不确定我哪里出错了,但在过去的 6 个小时里一直在苦苦挣扎。

自定义适配器代码(部分 Coe):

LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (convertView == null) {
            holder = new ViewHolder();

            convertView = inflater.inflate(R.layout.list_listitem, parent,false);

            holder.locName = (TextView) convertView
                    .findViewById(R.id.listitem_name);
            holder.locDistance = (TextView) convertView
                    .findViewById(R.id.listitem_text2);

            holder.locRating = (ImageView) convertView
                    .findViewById(R.id.listitem_img3);

            convertView.setTag(holder);
        } else {

            holder = (ViewHolder) convertView.getTag();

        }
        convertView.setOnClickListener(this);

        holder.locName.setText(data.get(position).getLocationName());

        return convertView;

请指教。

Solution

最后我能够通过在列表项布局中使用相对布局而不是线性布局来做到这一点。

【问题讨论】:

  • getView 方法上尝试convertView.setOnClickListener(....) 并删除setOnItemClickListener 表单活动
  • 感谢您的回复。我之前曾尝试过,现在又按照建议进行了尝试。还是同样的问题。当我单击 ImageView 时,它会执行 onClick 方法。在 TextView 点击无响应。
  • 你在使用视图持有者模式吗?
  • 发布您的自定义适配器代码。
  • @Tareq : 是的,我正在使用支架模式

标签: android android-listview


【解决方案1】:

仅在膨胀视图时添加点击侦听器。试试这个:

convertView = inflater.inflate(R.layout.list_listitem, null);

convertView.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {



                }

            });
  convertView.setTag(holder);

【讨论】:

  • 感谢您的回复。按照建议尝试过,仍然是同样的问题。只有图像查看点击有效。
  • 在删除时尝试代码:android:descendantFocusability="blocksDescendants"
  • 也尝试在 holder.locName 上设置 OnClickListener
  • 也试过了。仍然没有运气。今天好像不是我的一天:)
  • 感谢您的光临并提供宝贵的建议。也试过了,但还是卡住了。
【解决方案2】:

尝试在您的 Activity 类上使用它:

listView.setItemsCanFocus(true);

listView.setItemsCanFocus(false);

这对你有用。

【讨论】:

  • 感谢您的回复。按照建议尝试。仍然没有运气。同样的问题
【解决方案3】:

我知道它很晚,但可能有人觉得它有帮助。为了使整个项目成为可点击而不是连续的不同项目,请使用此

list.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        }
    });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多