List items with alternating colors

后端 未结 2 605
渐次进展
渐次进展 2020-12-17 00:32

I have a list view and an adapter that sets alternating background colors to the list items (\"zebra\" list style):

public View getView(final int position, V         


        
相关标签:
2条回答
  • 2020-12-17 01:00

    You need to change your list highlight color if you do it via styles

     <style name="Widget.AbsListView">
            <item name="android:listSelector">@drawable/my_selector</item>
     </style>
    

    or you can set same attribute in code my_selector is a state drawable - look for examples in the SDK directory :

    <?xml version="1.0" encoding="utf-8"?>
    <!-- Copyright (C) 2008 The Android Open Source Project
    
         Licensed under the Apache License, Version 2.0 (the "License");
         you may not use this file except in compliance with the License.
         You may obtain a copy of the License at
    
              http://www.apache.org/licenses/LICENSE-2.0
    
         Unless required by applicable law or agreed to in writing, software
         distributed under the License is distributed on an "AS IS" BASIS,
         WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
         See the License for the specific language governing permissions and
         limitations under the License.
    -->
    
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    
        <item android:state_window_focused="false"
            android:drawable="@color/transparent" />
    
        <!-- Even though these two point to the same resource, have two states so the drawable will invalidate itself when coming out of pressed state. -->
        <item android:state_focused="true" android:state_enabled="false"
            android:state_pressed="true"
            android:drawable="@drawable/list_selector_background_disabled" />
        <item android:state_focused="true" android:state_enabled="false"
            android:drawable="@drawable/list_selector_background_disabled" />
    
        <item android:state_focused="true" android:state_pressed="true"
            android:drawable="@drawable/list_selector_background_transition" />
        <item android:state_focused="false" android:state_pressed="true"
            android:drawable="@drawable/list_selector_background_transition" />
    
        <item android:state_focused="true"
            android:drawable="@drawable/list_selector_background_focus" />
    
    </selector>
    
    0 讨论(0)
  • 2020-12-17 01:10

    I think the easiest way is to create two selectors which are used as the background resources, with transparent color in the state_selected mode: (res/drawable/alterselector1.xml:)

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_selected="true"
            android:drawable="@drawable/transparent" />
        <item android:state_pressed="true"
            android:drawable="@drawable/transparent" />
        <item android:state_selected="false"
            android:drawable="@drawable/altercolor1"/>
    
    </selector>
    

    (res/drawable/alterselector2.xml:)

    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_selected="true"
            android:drawable="@drawable/transparent" />
        <item android:state_pressed="true"
            android:drawable="@drawable/transparent" />
        <item android:state_selected="false"
            android:drawable="@drawable/altercolor2"/>
    </selector>
    

    (res/values/colors.xml:)

    <resources>
        <drawable name="transparent">#00ffffff</drawable>
        <drawable name="altercolor1">#ffffffff</drawable>
        <drawable name="altercolor2">#ff000000</drawable>
    </resources>
    

    Then you set the backgrounds in the getView method of the adapter with the setBackgroundResource method:

    if (position % 2 == 0){
        reusableView.setBackgroundResource(R.drawable.alterselector1);
    } else {
        reusableView.setBackgroundResource(R.drawable.alterselector2);
    }
    

    Now when you select a row, your background don't hide the original selector behind.

    0 讨论(0)
提交回复
热议问题