ListFragment Item Selected Background

前端 未结 1 1437
借酒劲吻你
借酒劲吻你 2020-12-28 20:28

So I have a ListFragment set up with a few selections that open new Fragments. Part of me is wanting to make each item in the ListFragment (I have around 6) have a different

1条回答
  •  甜味超标
    2020-12-28 20:56

    Yes you can have them use a different background. For each of those you will need to build a StateListDrawable that selects the desired background based on the state of the item.

    If you look at the Layout fragment demo, the list items use this layout:

    setListAdapter(new ArrayAdapter(getActivity(),
            android.R.layout.simple_list_item_activated_1, Shakespeare.TITLES));
    

    That layout is:

    
    

    And the android:background being set here boils down to (for the dark non-holo theme):

    
        
        
    
    

    So just write your own drawables that use different drawables for their activated state.

    (Note I am assuming you are working with Honeycomb where the activated state was introduced. For previous platform versions, this is not as clean but not too hard -- you need to write a layout subclass that implements Checkable and changes its background based on the checked state.)

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