List items with alternating colors

后端 未结 2 616
渐次进展
渐次进展 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: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:)

    
        
        
        
    
    
    

    (res/drawable/alterselector2.xml:)

    
        
        
        
    
    

    (res/values/colors.xml:)

    
        #00ffffff
        #ffffffff
        #ff000000
    
    

    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.

提交回复
热议问题