Android List View selected item style not showing until user scrolls

浪尽此生 提交于 2019-12-24 22:14:20

问题


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.


回答1:


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.




回答2:


Disable the cache color hint optimization? http://developer.android.com/resources/articles/listview-backgrounds.html



来源:https://stackoverflow.com/questions/9089269/android-list-view-selected-item-style-not-showing-until-user-scrolls

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!