滚动列表视图时列表项不可点击
我的 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 方法,甚至不会获得焦点(橙色背景)。
谁能找出问题所在吗?
I have a problem with a ListView that happens only when I have scrolled down to see my list items.
Here is my main xml file:
<?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:
<?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. Every item in the ListView is composed by a ToggleButton and 2 TextViews. At first the ToggleButton wasn't clickable, but I solved by adding:
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.
The problem is when scrolling, the items on the ListView are not clickable any more. It won't use the setOnItemClickListener method, and even won't get focus (the orange background).
Can anyone figure out what is the problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这可能会解决您的问题,也可能不是答案。
您可以在自定义适配器中尝试此操作
这将禁用焦点(橙色突出显示)。
如果您使用notifyDataSetChanged来更新ListView,它将无法与CustomAdapter正常工作。请参阅这篇文章。
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).
If you are using notifyDataSetChanged to update the ListView It will not properly work with CustomAdapter. Refer this post.
您需要按如下方式设置列表视图的 xml:
机器人:descendantFocusability =“块后代”
然后你就可以点击。这是因为切换按钮存在于您的列表项中。
you'll need to set the listview's xml as follows:
android:descendantFocusability="blocksDescendants"
then you can click. this is because the toggle button exists in your list item.