Android 列表视图所选项目样式在用户滚动之前不会显示

发布于 2024-12-29 22:27:30 字数 1511 浏览 5 评论 0原文

我目前正在开发一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寂寞笑我太脆弱 2025-01-05 22:27:30

我终于设法解决了这个问题。

所需要的只是以下两行。

listView = getListView();
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

它现在的工作方式类似于平板电脑 ICS 中的邮件和人员。

I have finally managed to solve this problem.

All that was required was the following two lines

listView = getListView();
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

It now works similar to mail and people in ICS for tablets.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文