带有图像视图的Android列表项无法单击

时间:2014-06-08 10:56:49

标签: android android-listview

我的列表项目包含ImageViewTextView。问题是列表项目整体不可点击,只有当我点击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

最后,我可以通过在列表项布局中使用相对布局而不是线性布局来实现此目的。

3 个答案:

答案 0 :(得分:0)

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

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

convertView.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {



                }

            });
  convertView.setTag(holder);

答案 1 :(得分:0)

尝试在Activity类上使用它:

listView.setItemsCanFocus(true);

listView.setItemsCanFocus(false);

对你有用。

答案 2 :(得分:0)

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

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

        }
    });
相关问题