【问题标题】:Android List View selected item style not showing until user scrollsAndroid List View selected item style 不显示,直到用户滚动
【发布时间】:2012-02-01 00:50:11
【问题描述】:

我目前正在开发一个 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 有问题,如下所示Android list selector gets stuck when using Theme.Light 两种状态都试过了,问题依然存在。

【问题讨论】:

  • selected_background drawable 引用了底部的形状。

标签: android listview android-layout android-3.0-honeycomb android-4.0-ice-cream-sandwich


【解决方案1】:

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

只需要以下两行

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

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

【讨论】:

    【解决方案2】:

    【讨论】:

    • 刚刚尝试将 android:cacheColorHint="#00000000" 添加到列表视图中。不幸的是,这对解决任何一个问题都没有帮助。
    猜你喜欢
    • 1970-01-01
    • 2019-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-30
    • 2021-07-11
    • 2013-05-08
    相关资源
    最近更新 更多