活动类代码:

conversationList = (ListView)findViewById(android.R.id.list);
ConversationArrayAdapter conversationArrayAdapter=new  ConversationArrayAdapter(this, R.layout.conversation_list_item_format_left, conversationDetails);
conversationList.setAdapter(conversationArrayAdapter);
conversationList.setOnItemClickListener(new AdapterView.OnItemClickListener(){ 
    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
        Log.d("test","clicked");
    }
});

Adapter类中的getView函数:

if (v == null) {                                
    LayoutInflater vi = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if(leftSideMessageNumber.equals(m.getTo())) {
        v = vi.inflate(R.layout.conversation_list_item_format_left, null);
    } else {
        v = vi.inflate(R.layout.conversation_list_item_format_right, null);
    }
}

在膨胀时使用两个xml会有问题吗?


当前回答

您需要在listview_item.xml中执行2步

设置根布局:android:descendantFocusability="blocksDescendants" 在这个项目中设置任何可聚焦或可点击的视图: android:点击= " false " android: focusable = " false " android: focusableInTouchMode = " false "

下面是一个例子:listview_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="10dp"
    android:layout_marginTop="10dp"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:gravity="center_vertical"
    android:orientation="vertical"
    android:descendantFocusability="blocksDescendants">

    <RadioButton
        android:id="@+id/script_name_radio_btn"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:textColor="#000"
        android:padding="5dp"
        android:clickable="false"
        android:focusable="false"
        android:focusableInTouchMode="false"
        />

</LinearLayout>

其他回答

对于我的列表,我的行有其他可以点击的东西,比如按钮,所以做一个毯子blocksDescendants是行不通的。相反,我在按钮的xml中添加了一行:

    android:focusable="false"

这样可以防止按钮阻塞对行的单击,但仍然允许按钮接受单击。

我刚从这里找到了一个解决方案,不过是通过深度点击。

如果列表的任何行项包含可聚焦或可点击的视图,那么OnItemClickListener将不起作用。

行项必须具有类似的参数 android:descendantFocusability = "blocksDescendants"。

在这里您可以看到列表项应该是什么样的示例。 你的列表项xml应该是… row_item.xml (your_xml_file.xml)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:baselineAligned="false"
    android:descendantFocusability="blocksDescendants"
    android:gravity="center_vertical" >

    // your other widgets here

</LinearLayout>

两个很棒的解决方案是,如果你从一个片段扩展ListFragment,知道mListView。setOnItemClickListener不会在你的活动创建之前被调用,这确保了它是在活动创建时设置的

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    mListView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long rowId) {
            // Do the onItemClick action

            Log.d("ROWSELECT", "" + rowId);
        }
    });
}

在查看ListFragment的源代码时,我发现了这个

public class ListFragment extends Fragment {
    ...........................................
    ................................................

    final private AdapterView.OnItemClickListener mOnClickListener
                = new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
            onListItemClick((ListView)parent, v, position, id);
        }
    };

    ................................................................
    ................................................................

    public void onListItemClick(ListView l, View v, int position, long id) {
    }
}

附加了一个onItemClickListener对象,它调用onListItemClick() 因此,其他类似的解决方案,以完全相同的方式工作是重写onListItemClick()

@Override
public void onListItemClick(ListView l, View v, int position, long rowId) {
    super.onListItemClick(l, v, position, id);
   // Do the onItemClick action

   Log.d("ROWSELECT", "" + rowId);
} 

在列表视图的自定义行布局中使用下面的代码

 android:focusable="false"
 android:clickable="false"

即使我有同样的问题,我有复选框,做下面的掩码itemClickListener工作,

在复选框中添加以下属性,

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

ItemClickListner开始工作。

详细的例子你可以点击链接,

http://knowledge-cess.com/android-itemclicklistner-with-checkbox-or-radiobutton/

希望能有所帮助,干杯!!