简体   繁体   English

CheckedTextView不响应点击

[英]CheckedTextView doesn't respond to click

I have a listview that is dynamically populated with CheckedTextViews. 我有一个用CheckedTextViews动态填充的列表视图。 The listview is set to multiple choice mode. 列表视图设置为多选模式。 I'm using OnItemClickListener to respond to clicks on my listview. 我正在使用OnItemClickListener来响应列表视图上的点击。 Also I made an XML file with CheckedTextView's layout (actually it is just a copy of standard android.R.layout.simple_list_item_multiple_choice). 我也用CheckedTextView的布局制作了一个XML文件(实际上,它只是标准android.R.layout.simple_list_item_multiple_choice的副本)。 So in this case all works fine: when I'm clicking an item in the listview, the appropriate checkedtextview becomes checked. 因此,在这种情况下,所有工作都很好:当我单击列表视图中的项目时,相应的checkedtextview就会被选中。 But when I'm trying to use the following layout, the checkedtextview doesn't respond to the click and still unchecked. 但是,当我尝试使用以下布局时,checkedtextview不响应单击,并且仍未选中。

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ll"
    android:layout_height="wrap_content"   
    android:layout_width="wrap_content"  
    android:orientation="vertical"  
    android:padding="5px">
    <CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@android:id/text1"
        android:layout_width="fill_parent"
        android:layout_height="?android:attr/listPreferredItemHeight"
        android:gravity="center_vertical"
        android:drawableLeft="@drawable/checkbox_selector"
        android:paddingLeft="6dip"
        android:paddingRight="6dip"
        android:background="#00ffffff"
        android:textSize="15sp"
        android:singleLine="true"/>
</LinearLayout>    

I guess that it because CheckedTextViews are put into LinearLayout and they don't receive the click event from list items of the Listview. 我猜是因为CheckedTextViews被放入LinearLayout中,并且它们没有从Listview的列表项中接收到click事件。

我遇到了同样的问题,因此我将布局更改为android.R.layout.simple_list_item_multiple_choice,而不是使用带有CheckedTextView的自定义布局。

I guess different. 我猜是不同的。 If you look at the source code of simple_list_item_multiple_choice.xml , you'll see an android:checkMark attribute set to ?android:attr/listChoiceIndicatorMultiple . 如果您查看simple_list_item_multiple_choice.xml源代码 ,则会看到android:checkMark属性设置为?android:attr/listChoiceIndicatorMultiple This is what the CheckedTextView internally uses to draw the check box in whatever state it happens to be, as you may be able to see from its source code . 这就是CheckedTextView内部用来以任何状态绘制复选框的方式,您可能可以从其源代码中看到

But the definition of CheckedTextView in your layout lacks this attribute. 但是布局中CheckedTextView的定义缺少此属性。 This I would blame, rather than the use of CheckedTextView in a LinearLayout element. 我要怪,而不是在LinearLayout元素中使用CheckedTextView

I was wrong and you guessed right. 我错了,你猜对了。 It seems that the row for the custom ListView needs to implement Checkable , which CheckedTextView does but LinearLayout does not. 看来,自定义行ListView需要实现Checkable ,这CheckedTextView做,但LinearLayout没有。 See this answer in another StackOverflow thread for details. 有关详细信息,请参见另一个StackOverflow线程中的此答案

There are two easy solution for this problem: 有两个简单的解决方案可以解决此问题:

  1. Remove the LinearLayout and user only CheckedTextView (Yes, it is possible). 删除LinearLayout,仅允许用户使用CheckedTextView(是的,可以)。 So the main element of the layout is checkable and it will be marked. 因此,布局的主要元素是可检查的,并将被标记。

  2. If your minSDK is 11 of above, customize the android:checkMarke and set a state to Activated. 如果您的minSDK为11以上,请自定义android:checkMarke并将状态设置为已激活。 Here is an example: 这是一个例子:

Example code: 示例代码:

<?xml version="1.0" encoding="utf-8"?>
<selector  xmlns:android="http://schemas.android.com/apk/res/android" >
   <item android:state_activated="true" android:drawable="@drawable/checkbox_selected"/>  
   <item android:state_activated="false" android:drawable="@drawable/checkbox_deselected" />  
   <item android:state_checked="false" android:drawable="@drawable/checkbox_deselected" />  
   <item android:state_checked="true" android:drawable="@drawable/checkbox_selected" /> 
</selector>

Source: http://www.jiahaoliuliu.com/2013/08/customizing-checkbox-in-android.html 来源: http//www.jiahaoliuliu.com/2013/08/customizing-checkbox-in-android.html

The middle level solution is customize the onItemClickListener of the list view, as shown on this code: https://github.com/jiahaoliuliu/CustomizedListRow 中间层解决方案是自定义列表视图的onItemClickListener,如以下代码所示: https : //github.com/jiahaoliuliu/CustomizedListRow

The hard but the correct solution is the one proposed by MarvinLab: http://www.marvinlabs.com/2010/10/29/custom-listview-ability-check-items/ 难而正确的解决方案是MarvinLab提出的解决方案: http : //www.marvinlabs.com/2010/10/29/custom-listview-ability-check-items/

Related issue: What is the difference between the states selected, checked and activated in Android? 相关问题: 在Android中选择,检查和激活的状态之间有什么区别?

This issue is caused by the attr singleLine = true . 此问题是由attr singleLine = true引起的。 If you delete this or replace with maxLines = 1 , it will work fine. 如果删除它或将其替换为maxLines = 1 ,它将正常工作。 Actually, the status is checked when you click on the item — I think it's an Android bug. 实际上,当您单击该项目时会检查状态-我认为这是一个Android错误。

Well even if they don't receive the OnClickEvent you can maintain the state of these checkboxes using a boolean ArrayList of same size of List Items. 好吧,即使它们没有收到OnClickEvent,您也可以使用相同大小的列表项的布尔ArrayList来维护这些复选框的状态。 Here, I have just answrered something like that. 在这里,我只是想这样。 I hope it will give you a better idea. 我希望它能给您一个更好的主意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM