OnItemCLickListener在listview中不起作用

hie*_*iei 230 android listview

Activity 班级代码:

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");
    }
});
Run Code Online (Sandbox Code Playgroud)

类中的getView函数Adapter:

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

在充气时使用两个xmls有问题吗?

Bha*_*ara 714

我刚从这里找到了解决方案......但是通过深度点击......

如果列表中的任何行项包含可聚焦或可单击的视图,则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>
Run Code Online (Sandbox Code Playgroud)

  • 您也可以使用`listView.setDescendantFocusability(int focus);`来``focus`是`ViewGroup.FOCUS_BEFORE_DESCENDANTS`,`ViewGroup.FOCUS_AFTER_DESCENDANTS`或`ViewGroup.FOCUS_BLOCK_DESCENDANTS`之一. (2认同)

bal*_*azs 87

问题是您的布局包含可聚焦或可点击的项目.如果视图包含可聚焦或可单击的项,则不会调用OnItemCLickListener.

点击此处获取更多信息.

如果不是这样,请发布一个布局xmls.

  • 这是我的解决方案,我声明我的视图是可点击的。谢谢! (2认同)

Jan*_*pas 57

对于我的列表,我的行还有其他可以点击的东西,比如按钮,所以做一个毯子的块后代不起作用.相反,我在按钮的xml中添加一行:

android:focusable="false"
Run Code Online (Sandbox Code Playgroud)

这样可以防止按钮阻止行上的点击,但仍然可以让按钮获取点击次数.


Edd*_*ddy 30

你需要在listview_item.xml中执行两个步骤

  1. 设置根布局: android:descendantFocusability="blocksDescendants"
  2. 使用以下方法设置此项目中的任何可聚焦或可点击的视图:
    android:clickable="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>
Run Code Online (Sandbox Code Playgroud)


Mil*_*kla 19

在listview的自定义行布局中使用以下代码内部的按钮标记

 android:focusable="false"
 android:clickable="false"
Run Code Online (Sandbox Code Playgroud)


小智 17

我有同样的问题,我刚看到我不小心设置了:

@Override
public boolean isEnabled(int position)
{
    return false;
}
Run Code Online (Sandbox Code Playgroud)

在我的CustomListViewAdapter类上.

通过将其更改为:

return true;
Run Code Online (Sandbox Code Playgroud)

我设法解决了这个问题.以防万一有人犯了同样的错误......

  • 我也有一个`@Override public void onApplicationStart {ExplodeThisDevice(); }` (6认同)

小智 17

使用android:descendantFocusability

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="80dip"
    android:background="@color/light_green"
    android:descendantFocusability="blocksDescendants" >
Run Code Online (Sandbox Code Playgroud)

在根布局中添加以上内容

  • 添加详细信息可能会使这个答案更好,而且+1也是 (2认同)

RAJ*_*GAM 11

我在这个答案的帮助下解决了这个问题

1.在list_items.xml的Linear Layout中添加以下内容 android:descendantFocusability="blocksDescendants"

2. list_items.xml中的LinearLayout的Child视图

 android:focusable="false" 
Run Code Online (Sandbox Code Playgroud)


sav*_*ion 10

如果您的文本视图,按钮或stg仅可在行视图中单击或选择

android:descendantFocusability="blocksDescendants"
Run Code Online (Sandbox Code Playgroud)

是不足够的.你必须设置

android:textIsSelectable="false"
Run Code Online (Sandbox Code Playgroud)

到您的textviews和

android:focusable="false"
Run Code Online (Sandbox Code Playgroud)

到你的按钮和其他可聚焦的项目.


Gok*_*rni 10

即使我有同样的问题,我有复选框,做了以下掩蔽itemClickListener工作,

在复选框中添加了以下属性:

android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false"
Run Code Online (Sandbox Code Playgroud)

和ItemClickListner开始工作.

有关详细示例,您可以浏览链接,

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

希望它能帮助干杯!


u2t*_*all 5

我有同样的问题,并尝试了所有提到的解决方案无济于事.通过测试我发现使文本可选择阻止了调用者的调用.因此,通过将其切换为false或删除它,我的侦听器再次被调用.

android:textIsSelectable="false"
Run Code Online (Sandbox Code Playgroud)

希望这有助于像我一样被困的人.


Waj*_*han 5

  1. 在主布局中添加这个

    android:descendantFocusability="blocksDescendants"
    
    Run Code Online (Sandbox Code Playgroud)
  2. 将此代码写入每个具有 onClick 的按钮、Textview、ImageView 等

    android:focusable="false"
    
    android:clickable="false"
    
    Run Code Online (Sandbox Code Playgroud)

希望它会起作用。