簡體   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