简体   繁体   English

滚动列表视图时,列表项不可单击

[英]When scrolling a listview List Items not clickable

I have a problem with a ListView that happens only when I have scrolled down to see my list items. 我有一个ListView问题,仅当我向下滚动以查看列表项时才会发生。

Here is my main xml file: 这是我的主要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>

And this is my Row item xml loaded in the above ListView: 这是我在上面的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>    

The first LinearLayout is clickable (AddAlarm) and it simply adds another item to the ListView. 第一个LinearLayout是可单击的(AddAlarm),它只是将另一个项目添加到ListView。 Every item in the ListView is composed by a ToggleButton and 2 TextViews. ListView中的每个项目都由一个ToggleButton和2个TextViews组成。 At first the ToggleButton wasn't clickable, but I solved by adding: 最初,ToggleButton不可点击,但我通过添加以下内容解决了:

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

So, everything works fine, until there are so many items in the list that I need to scroll down to view them. 因此,一切正常,直到列表中有太多项目需要向下滚动才能查看它们。 In fact, it works fine until I actually scroll down. 实际上,它可以正常工作,直到我真正向下滚动为止。 It works fine even when there are more items in the ListView but I don't scroll down to see them. 即使ListView中有更多项目,它也可以正常工作,但我不会向下滚动以查看它们。

The problem is when scrolling, the items on the ListView are not clickable any more. 问题是滚动时,ListView上的项目不再可单击。 It won't use the setOnItemClickListener method, and even won't get focus (the orange background). 它不会使用setOnItemClickListener方法,甚至不会获得焦点(橙色背景)。

Can anyone figure out what is the problem? 谁能找出问题所在?

you'll need to set the listview's xml as follows: android:descendantFocusability="blocksDescendants" then you can click. 您需要按如下所示设置列表视图的xml:android:descendantFocusability =“ blocksDescendants”,然后单击即可。 this is because the toggle button exists in your list item. 这是因为切换按钮存在于您的列表项中。

This may solve your problem or may not be the answer. 这可能会解决您的问题,也可能无法解决问题。 You can try this in your custom adapter This will disable the focus (Orange Highlighting). 您可以在自定义适配器中尝试此操作。这将禁用焦点(橙色突出显示)。

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

If you are using notifyDataSetChanged to update the ListView It will not properly work with CustomAdapter. 如果您使用notifyDataSetChanged来更新ListView,它将无法与CustomAdapter一起正常使用。 Refer this post. 请参阅这篇文章。

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

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