Android自定义ArrayAdapter |在ImageView上单击更改图标

时间:2017-07-02 21:57:24

标签: android listview imageview onclicklistener

我有一个列表,其中的元素使用自定义布局,其中包含文本视图和图像视图。为此,我遵循ViewHolder模式,如解释here。图像视图显示两个图标之一,我想更改单击图像视图的图标。

所以我的第一个方法是在我的适配器类的重写 getView 函数中定义ImageViews的on click侦听器。问题是,当第一个ImageView的图标发生变化时,我向下滚动到最后一个图标也发生了变化。 This question here没有帮助。

Here我发现这不是处理 getView 函数中点击的最佳方法,但最好在 listView.setOnItemClickListener 中执行此操作。我尝试了但是我无法确定是否单击了ImageView,因为对象保存了列表项,而 view 参数是ImageView中的LinearLayout包含(即使我直接在ImageView中单击)。按照建议here设置外部LinearLayout的january没有帮助。

我确定有人必须有这个问题/用例,但我无法找到解决方案。那么,在我的客户列表项视图中处理ImageView点击的最佳方法是什么?

2 个答案:

答案 0 :(得分:0)

如果您有两个图标,则可以只设置自定义复选框而不是图像视图:

<CheckBox

            android:id="@+id/customchechecbox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/checkbox_selector"
            android:button="@android:color/transparent"  />


checkbox_selector :

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

答案 1 :(得分:0)

尝试将以下属性添加到可点击的ImageView而不是其ViewGroup,即LinearLayout:

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

...并且就AdapterView / ListView / RecyclerView中的UI更新而言,您应该充分利用跟踪单击的列表项位置的int标志,然后将其设置为条件语句在调用getView()之前notifyDataSetChanged(),因为“每隔一行”都会更新,特别是在100行的列表中。

我实际上在类似的问题herehere中回答了这个问题。