2013-03-20 106 views
2

在MainActivity中有onItemClick,而activity iplements OnItemClickListener。 一切都非常简单,这个监听器的结果是当用户触摸ListView的一个项目时,在屏幕上用Toas打印一条消息。 但onItemClick函数不起作用,为什么? [logCat没有错误] 在此先感谢。实现OnItemClickListener,但onItemClick不起作用

这是我的主要活动代码:

​​

我activity_main.xml中:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".MainActivity"> 

    <Button 
     android:id="@+id/add_nota" 
     android:onClick="addNote" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentTop="true" 
     android:text="@string/add_nota_label" /> 

    <ListView 
     android:id="@+id/listaNote" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_below="@id/add_nota" > 

    </ListView> 

</RelativeLayout> 

和用于BT适配器每一个项目我row.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 



     <!--*** PARTE SUPERIORE ***--> 
     <RelativeLayout 
      android:id="@+id/button_row_item" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="#FFFEC9"> 

      <ImageButton 
       android:id="@+id/button_row_delete" 
       android:onClick="crudClick" 
       android:layout_width="35dp" 
       android:layout_height="30dp" 
       android:layout_alignParentRight="true" 
       android:layout_alignParentTop="true" 
       android:contentDescription="@string/icon_delete" 
       android:src="@android:drawable/ic_menu_delete"/> 

      <ImageButton 
       android:id="@+id/button_row_edit" 
       android:onClick="crudClick" 
       android:layout_width="35dp" 
       android:layout_height="30dp" 
       android:layout_alignParentTop="true" 
       android:layout_toLeftOf="@+id/button_row_delete" 
       android:contentDescription="@string/icon_edit" 
       android:src="@android:drawable/ic_menu_edit" /> 

      <TextView 
       android:id="@+id/item_data" 
       android:layout_width="20dp" 
       android:layout_height="wrap_content" 
       android:layout_alignParentLeft="true" 
       android:layout_alignParentTop="true" 
       android:textIsSelectable="true" 
       android:layout_toLeftOf="@+id/button_row_edit" 
       android:padding="10dip" 
       android:textSize="12sp" /> 

     </RelativeLayout> 
     <!--*** /PARTE SUPERIORE ***--> 


     <!--*** PARTE INFERIORE ***--> 
     <RelativeLayout 
      android:id="@+id/oggetto_row_item" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="#FCFBB5"> 
      <TextView 
       android:id="@+id/item_oggetto" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:padding="10dip" 
       android:textSize="18sp" 
       android:paddingTop="5dp"/> 
     </RelativeLayout> 
     <!--*** /PARTE INFERIORE ***--> 



</LinearLayout> 
+0

发表您的列表视图Xml – 2013-03-20 16:01:10

+0

什么包含你的列表视图?哪些小部件? – Sajmon 2013-03-20 16:01:13

+0

我在原始主题中添加了xml代码,谢谢。 – tune 2013-03-20 16:28:40

回答

5

如果列表视图中的视图可以聚焦,OnItemClick方法将不会触发。

+0

我没有指定focusable是true还是false,只是我所有xml中的指令都不存在.. 。我把它设置为假? – tune 2013-03-20 16:39:50

+0

默认情况下,某些视图是可以关注的 - 例如Button,ImageButton等... 编辑:现在我查看了你的xml - 问题在于你正在使用ImageButton。在适配器的getView方法中分别设置每个项目的监听器,而不是使用'OnItemSelectedListener'或尝试将ImageButton设置为false。 – asenovm 2013-03-20 18:08:14

+0

所以如果我使用imageButton,没有什么可以做项目上使用监听器? – tune 2013-04-05 14:20:15

0

尝试下面的代码,看看它是否有助于

listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE); 
+0

谢谢你,我试过你的代码,但没有。 – tune 2013-03-21 10:24:28

3

使文本的可选(android:textIsSelectable)允许集中TextView的,并防止获得的OnItemClickListener任何(短)点击事件。 OnItemLongClickListener仍然有效。

这代表了Android库(如果无意)或Android文档(如果有意)的严重错误。无论如何,只要将android:textIsSelectable更改为false,一切都会好起来的。

+0

嗨Canuck,我尝试你的解决方案,但没有。 我有一个行布局(列表项)与2 textView和2 imageButton ...尝试设置所有视图focusable:false和textIsSelectable:false ...但不要做。 我使用ListFragment扩展而不是ListActivity。 – tune 2013-04-05 14:13:35

+0

我特意选择了ListActivity,这样我就可以更好地控制这个过程,所以,不幸的是,我的经历与您的经历有些不同。 – Canuck 2013-04-08 11:23:13

2

我知道这是一个有点晚了,但我有同样的问题,并通过设置onFocusable=false编程方式,即在我的自定义ListViewAdapter,在getView(...)解决了,我找回了ImageButton调用findViewById,然后通过调用setFocusable(false)改变可聚焦价值, setFocusableInTouchMode(false)

public View getView(final int position, View convertView, ViewGroup parent) { 

    if (convertView == null) 
     convertView = LayoutInflater.from(context).inflate(R.layout.alvappliances, parent, false); 

    ImageButton delete = (ImageButton)convertView.findViewById(R.id.alvaDeleteIB); 

    delete.setFocusable(false); 
    delete.setFocusableInTouchMode(false); 

    return convertView; 
} 
+0

非常感谢!你为我节省了一堆时间。你知道为什么xml被忽略吗? – Gonzo 2014-01-16 05:22:17

1

在你row.xml添加下面一行在你的线性布局标签

机器人:descendantFocusability = “blocksDescendants”

相关问题