Android 列表视图所选项目样式在用户滚动之前不会显示
我目前正在开发一个 Android 平板电脑应用程序,它有两个片段,一个列表片段和一个详细信息片段。
我试图使我的列表类似于“人员”和“电子邮件”,其中选择项目背景会发生变化,并且三角形位于列表的右侧。
我目前在设置列表中所选项目的背景时遇到困难。 据我了解,在列表视图中,我应该将 android:listSelector 设置为可绘制对象,并且所有操作都应该有效。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="8dp"
>
<ListView android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:listSelector="@drawable/selected_background"
/>
<TextView android:id="@id/android:empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="No data. Please check your internet connection." android:gravity="center"/>
</LinearLayout>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="@color/hukd_orange_faded" />
</shape>
当我这样做时,彩色项目的背景不会改变,直到我开始滚动列表。不仅如此,背景还会闪烁,并且经常会回到标准背景颜色。
我在网上查看过,似乎 Holo.light 有问题,如下所示 使用 Theme.Light 时 Android 列表选择器卡住 我尝试了两种状态,但问题仍然存在。
I am currently working on an Android tablet application which has two fragments, a list fragment and a details fragment.
I am trying to make my list similar to People and Email where when selected the item background changes and a triangle is positioned on the right hand side of the list.
I am currently having difficulties setting the background of the item selected in the list.
As I understand in the list view I should set android:listSelector to a drawable and all should work.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="8dp"
>
<ListView android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:listSelector="@drawable/selected_background"
/>
<TextView android:id="@id/android:empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="No data. Please check your internet connection." android:gravity="center"/>
</LinearLayout>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="@color/hukd_orange_faded" />
</shape>
When I do this the background of the coloured item does not change until I start to scroll the list. Not only this but the background flickers and often goes back to the standard background colour.
I have looked online and it seems there is a problem with Holo.light as shown here Android list selector gets stuck when using Theme.Light
I have tried two states but the problems still exist.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我终于设法解决了这个问题。
所需要的只是以下两行。
它现在的工作方式类似于平板电脑 ICS 中的邮件和人员。
I have finally managed to solve this problem.
All that was required was the following two lines
It now works similar to mail and people in ICS for tablets.
禁用缓存颜色提示优化? http://developer.android.com/resources/articles/listview-backgrounds.html
Disable the cache color hint optimization? http://developer.android.com/resources/articles/listview-backgrounds.html