滚动列表视图列表项不可点击时

时间:2011-10-19 16:01:45

标签: android listview scroll

我遇到了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中加载的Row项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方法,甚至不会获得焦点(橙色背景)。

任何人都可以找出问题所在吗?

2 个答案:

答案 0 :(得分:0)

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

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

如果您使用notifyDataSetChanged更新ListView它将无法正常使用CustomAdapter。请参阅this帖子。

答案 1 :(得分:0)

你需要设置listview的xml,如下所示:      机器人:descendantFocusability = “blocksDescendants” 然后你可以点击。这是因为切换按钮存在于列表项中。

相关问题