본문 바로가기
안드로이드

[안드로이드] ListView의 Selector에 관해서...

by 호군 2011. 8. 31.
반응형
ListView에서 selector를 설정하는 메소드는 AbsListView에 구현되어 있습니다.
메소드의 이름은 setSelector() 이고, 사용방법은 API 문서를 보시기 바랍니다.
안드로이드에서 selector의 xml파일은 list_selector_background.xml 입니다.
selector는 Drawable를 상속받아 구현된 StateListDrawable 객체입니다.

<?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>


selector는 6가지의 상태에 대해서 동작하도록 되어있습니다.
@color/transparent은 ColorDrawable 타입이고,
@drawable/list_selector_background_transition은 TransitionDrawable로 되어있고,
@drawable/list_selector_background_disabled과 @drawable/list_selector_background_focus은 NinePatchDrawable 타입으로 되어있습니다.

자신만의 selector를 만들 경우, 위의 xml의 구조를 따라 만들면, 쉽게 만들 수가 있습니다.
반응형