The Android implementation sets the background for both the click and the release

  • 2020-06-15 10:10:04
  • OfStack

This article illustrates how the Android implementation sets the background for ListView both when clicking and after clicking. Share to everybody for everybody reference. The specific analysis is as follows:

So the effect here is,

(1) When clicking item of ListView, there will be a specified background.

(2) After letting go, the item click will also have the specified background

Implementation (1) is simple: set listSelector for ListView in xml.

<ListView  
android:id="@+id/pop_listview_left" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:scrollbars="none" 
android:divider="@color/popup_left_bg" 
android:dividerHeight="1dp" 
android:listSelector="@color/popup_right_bg" 
android:scrollingCache="false" 
/>

Implementation (2) is also simple, dynamically changing the background in adapter:

if (position == selectedPosition){  
    convertView.setBackgroundResource(R.color.left_selected);
}else{ 
    convertView.setBackgroundResource(R.color.left_normal);
}

And update selectedPosition in the click event of ListView:
leftLV.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override 
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
 // Update background color  
 FirstClassAdapter adapter = (FirstClassAdapter) (parent.getAdapter()); 
 adapter.setSelectedPosition(position); 
 adapter.notifyDataSetChanged(); 
    } 
});

However, here comes the problem: after setting (2), the effect of (1) is gone!!
That's because, in Settings

convertView.setBackgroundResource(R.color.left_selected);

, the color specified in listSelector in (1) will be overwritten.

There are two solutions:

(1)

Change the background of 1 solid color of convertView to 1 solid color of selector and set its color to be transparent when clicked (so that the color of listSelector below is exposed). The following are selector_ES46en_normal.xml and ES49en_ES50en_selected.xml.

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

Then change the code in (2) to:

if (position == selectedPosition){  
    convertView.setBackgroundResource(R.drawable.selector_left_selected); 
}else{ 
    convertView.setBackgroundResource(R.drawable.selector_left_normal); 
}

(2)

Refer to (1), remove the listSelector attribute of ListView and copy its color to the above two selector to replace the color of transparent.

That is, every time you click on the ListView entry and set the background color,

(a) If the item is now selected, set to a certain color

(b) Otherwise, set the color to 1 selector and specify the color in selector when clicked and when not clicked, respectively.

The problem was solved satisfactorily.

I hope this article has been helpful for your Android programming.


Related articles: