[英]When scrolling a listview List Items not clickable
我有一个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个TextViews组成。 最初,ToggleButton不可点击,但我通过添加以下内容解决了:
android:focusableInTouchMode="false"
android:focusable="false"
因此,一切正常,直到列表中有太多项目需要向下滚动才能查看它们。 实际上,它可以正常工作,直到我真正向下滚动为止。 即使ListView中有更多项目,它也可以正常工作,但我不会向下滚动以查看它们。
问题是滚动时,ListView上的项目不再可单击。 它不会使用setOnItemClickListener方法,甚至不会获得焦点(橙色背景)。
谁能找出问题所在?
您需要按如下所示设置列表视图的xml:android:descendantFocusability =“ blocksDescendants”,然后单击即可。 这是因为切换按钮存在于您的列表项中。
这可能会解决您的问题,也可能无法解决问题。 您可以在自定义适配器中尝试此操作。这将禁用焦点(橙色突出显示)。
public CustomAdapterView(Context context,YourModel data)
{
super( context );
setOnClickListener((OnClickListener) context);
setClickable(true);
setFocusable(false);
}
如果您使用notifyDataSetChanged来更新ListView,它将无法与CustomAdapter一起正常使用。 请参阅这篇文章。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.