繁体   English   中英

ListView项不可单击(具有focusable = false的事件)

[英]ListView items not clickable (event with focusable=false)

我有一个ListView,其中包含一个imageview,一个textview和一个复选框。 当我添加imageview时,行变得不可单击。 我尝试了很多解决方案,包括android:focusable="false"android:descendantFocusability="blocksDescendants"android:clickable=true

这是我的ListView:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:clickable="true"
android:descendantFocusability="blocksDescendants"
android:orientation="vertical" >
<ImageView
    android:id="@+id/imgListView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:focusable="false"
    android:src="@drawable/ic_launcher" />
<TextView
    android:id="@+id/txtListView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_toRightOf="@id/imgListView"
    android:focusable="false"
    android:textColor="#FFFFFF" />
<CheckBox
    android:id="@+id/chkListView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    android:focusable="false" />
</RelativeLayout>

这是使用它的活动:

<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:background="#000000"
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=".FolderActivity" >
<ListView
    android:id="@+id/lstFolders"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true" 
    android:dividerHeight="1dp"
    android:divider="#FFFFFF">
</ListView>
</RelativeLayout>

我只需要像以前一样再次单击这些行即可。

PS:当我第一次添加复选框并使用android:focusable="false"解决它时,我已经遇到了相同的问题。

为什么不为适配器的getview方法中的每个视图编写on click侦听器。 也参考这个问题
Android:具有多个可单击按钮的ListView元素
就连我也遇到过同样的问题,根据我的经验,如果您只有一个可在列表视图中单击的元素,那么xml参数会起作用,对于多个元素,我只在适配器内部编写了它。

我们需要进行以下更改,以通过避免单击/关注列表视图项目的组件来使列表视图上的项目单击有效。

  1. 在项目布局上添加android:descendantFocusability =“ blocksDescendants”。
  2. 在列表视图项内的组件上添加android:focusableInTouchMode =“ false”(示例,图像视图,按钮,复选框,文本视图,自定义视图,画布)
  3. 在自定义视图的事件列表器上返回false(例如:扩展View的类实现了View.OnTouchListener,并且方法public boolean onTouch(View v,MotionEvent event)应当返回false。

暂无
暂无

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

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