【问题标题】:When scrolling a listview List Items not clickable滚动列表视图时列表项不可点击
【发布时间】:2011-10-19 16:01:44
【问题描述】:

我的 ListView 存在问题,该问题仅在我向下滚动查看列表项时才会发生。

这是我的主要 xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
    android:id="@+id/AddAlarm"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:clickable="true"
    android:layout_margin="10dp" >
<ImageView
    android:src="@drawable/add"
    android:gravity="right"
    android:layout_width="30dp"
    android:layout_height="30dp" 
    android:layout_marginRight="0dp"/>            
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="20dp"
    android:gravity="left"
    android:textSize="25dp"
    android:text="@string/add_alarm" />
</LinearLayout>     
<ListView
    android:id="@+id/ListaAlarmas"
    android:scrollbars="vertical"
    android:focusable="false" android:clickable="false"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />
</LinearLayout>

这是我在上面 ListView 中加载的行项 xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ToggleButton
    android:id="@+id/CheckBox"
    android:textOn=""
    android:textOff=""
    android:focusableInTouchMode="false"
    android:focusable="false"
    android:layout_gravity="center"
    android:background="@drawable/clock_off"
    android:layout_margin="10dp"
    android:layout_height="45dp"
    android:layout_width="45dp" />  
<View
    android:id="@+id/firstDivider"
    android:layout_height="fill_parent"
    android:focusableInTouchMode="false"
    android:focusable="false"
    android:layout_width="2dp"
    android:background="#999999" />       
<LinearLayout
    android:orientation="vertical"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_gravity="center_vertical"
    android:focusableInTouchMode="false"
    android:focusable="false"
    android:layout_weight="3"
    android:layout_marginLeft="30dp" >
    <TextView
        android:id="@+id/TextoHora"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="25dp"/>
    <TextView
        android:id="@+id/TextoRepetir"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
 </LinearLayout>    

第一个 LinearLayout 是可点击的 (AddAlarm),它只是将另一个项目添加到 ListView。 ListView 中的每个项目都由一个 ToggleButton 和 2 个 TextView 组成。起初 ToggleButton 不可点击,但我通过添加解决了:

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

所以,一切正常,直到列表中有这么多项目,我需要向下滚动才能查看它们。事实上,在我真正向下滚动之前它工作得很好。 即使 ListView 中有更多项目,它也能正常工作,但我没有向下滚动查看它们。

问题是滚动时,ListView 上的项目不再可点击。它不会使用 setOnItemClickListener 方法,甚至不会获得焦点(橙色背景)。

谁能找出问题所在?

【问题讨论】:

    标签: android listview scroll


    【解决方案1】:

    这可能会解决您的问题,也可能不是答案。 您可以在自定义适配器中尝试此操作 这将禁用焦点(橙色突出显示)。

    public CustomAdapterView(Context context,YourModel data)
    {
        super( context );
        setOnClickListener((OnClickListener) context);
        setClickable(true);
        setFocusable(false);
    }
    

    如果您使用 notifyDataSetChanged 更新 ListView 它将无法与 CustomAdapter 一起正常工作。参考this帖子。

    【讨论】:

    • mahe madhi,谢谢你的回答。只是一个问题,你的例子是不是就像我在xml文件中将clickable和focusable设置为true和false一样?我试过了,但没有用。我认为这不是 notifyDataSetChanged 的​​问题,因为在我移动滚动之前它可以正常工作。无论如何,非常感谢。
    【解决方案2】:

    您需要将列表视图的 xml 设置如下: android:descendantFocusability="blocksDescendants" 然后你可以点击。这是因为切换按钮存在于您的列表项中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-21
      • 1970-01-01
      相关资源
      最近更新 更多