活动类代码:

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会有问题吗?


当前回答

我借助这个答案解决了它

1.在“list_items.xml的线性布局”中添加如下内容 android: descendantFocusability = " blocksDescendants "

2.list_items.xml中线性布局的子视图

 android:focusable="false" 

其他回答

问题是你的布局包含可聚焦或可点击的项目。 如果视图包含可聚焦或可点击的项,OnItemCLickListener将不会被调用。

点击这里了解更多信息。

如果不是这样的话,请发布一个你的布局xml。

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

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

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

ItemClickListner开始工作。

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

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

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

对我有用的是将下面的代码添加到我的row.xml文件布局中的每个子视图中:

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

在我的例子中:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <TextView
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:id="@+id/testingId"
        android:text="Name"
       //other stuff 
        />

    <TextView
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:id="@+id/dummyId"
        android:text="icon"
        //other stuff 
        />

    <TextView
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:id="@+id/assignmentColor"
       //other stuff 
       />

    <TextView
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:id="@+id/testID"
        //other stuff 
        />

    <TextView
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:text="TextView"
        //other stuff 
        />

</android.support.constraint.ConstraintLayout>

这是我在Fragment子类中的setOnItemClickListener调用:

CustomListView = (PullToRefreshListCustomView) layout.findViewById(getListResourceID());
        CustomListView.setAdapter(customAdapter);
        CustomListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Log.d("Testing", "onitem click working");
              //  other code
            }

        });

我从这里得到了答案!

两个很棒的解决方案是,如果你从一个片段扩展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);
} 

面对同样的问题,试了几个小时。如果你已经尝试了以上所有的,尝试改变Listview的layout_width和列表项的match_parent从wrap_content。