簡體   English   中英

ListView的自定義布局中斷OnItemClickListener

[英]Custom Layouts for ListView Breaks OnItemClickListener

當對列表視圖中的項目使用自定義布局時,事實證明不會觸發OnItemClickListener。

以前,我使用的是android.R.layout.two_line_list_item。 我所做的就是將這個不推薦使用的布局換成我自己的自定義布局。

我努力了:

-將選擇模式設置為單一

-在父級列表和列表項中啟用長時和短時可點擊性

-使焦點和子孫可聚焦性

-請求焦點

任何反饋將不勝感激!!!

謝謝!

我猜您可以嘗試在customlayout的xml文件中設置android:clickable =“ true” 但是布局文件的代碼可能會更有幫助。

這是自定義布局...

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:focusable="false"
    android:focusableInTouchMode="false" >

    <TextView
        android:id="@+id/itemSummary"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:textIsSelectable="true" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toLeftOf="@id/itemSummary"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/item"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textIsSelectable="true" />

        <TextView
            android:id="@+id/subItem"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/item"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textIsSelectable="true" />
    </RelativeLayout>

</RelativeLayout>

這是一些查看項創建代碼...

adapter = new ArrayAdapter<ComplexObject>(this, R.layout.item_row, 0,
                al) {
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                if (convertView == null) {
                    convertView = inflater.inflate(R.layout.item_row, parent,
                            false);
                }

                ComplexObject item = adapter.getItem(position);

                ((TextView) convertView.findViewById(R.id.item)).setText(item
                        .getShape());

                ((TextView) convertView.findViewById(R.id.subItem))
                        .setText(item.getWeight() + " lbs");

                ((TextView) convertView.findViewById(R.id.itemSummary))
                        .setText(item.getAge() + " years");

                return convertView;
            }

        };

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM