Change the background image of listview on Focus

后端 未结 3 1265
隐瞒了意图╮
隐瞒了意图╮ 2021-01-16 13:03

\"enter

My code is :

ListContacts.java

publi         


        
相关标签:
3条回答
  • 2021-01-16 13:30

    try this

        listViewContactList.setOnFocusChangeListener(new OnFocusChangeListener(){
    
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
        v.setBackgroundColor(Color.BLACK);
    
       ImageView img=(ImageView)v.findViewById(R.id.callimage);
       img.setImageResource(R.drawable.yourimage);
       TextView tv=(TextView)v.findViewById(R.id.your_textview_id);
       tv.setBackgroundResource(R.drawable.your_back);      
        }
    
        });
    
    0 讨论(0)
  • 2021-01-16 13:33

    in getView

    if(position == focusedPosition) {
        textView.setColor(context.getResources().getColor(R.color.color_for_focused_item);
    } else {
        textView.setColor(context.getResources().getColor(R.color.color_for_normal_item);
    }
    
    0 讨论(0)
  • 2021-01-16 13:45

    Add an xml file to manage the background of the LinearLayout.

    list_bg_selector.xml :

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    
        <!-- Active tab -->
        <item android:state_selected="true" android:state_focused="false"
            android:state_pressed="false" android:drawable="@drawable/list_selected_bgimage" />
        <!-- Inactive tab -->
        <item android:state_selected="false" android:state_focused="false"
            android:state_pressed="false" android:drawable="@drawable/list_default_bgimage" />
        <!-- Pressed tab -->
        <item android:state_pressed="true" android:drawable="@drawable/list_selected_bgimage" />
        <!-- Selected tab (using d-pad) -->
        <item android:state_focused="true" android:state_selected="true"
            android:state_pressed="false" android:drawable="@drawable/list_selected_bgimage" />
    </selector> 
    

    To manage the text color, set the text color to another xml.

    text_selector.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    
        <item android:state_selected="true" android:color="@android:color/black"/>
        <item android:state_focused="true" android:color="@android:color/black"/>
        <item android:state_pressed="true" android:color="@android:color/black"/>
        <item android:color="@android:color/white"/>
    
    </selector>
    
    0 讨论(0)
提交回复
热议问题