Set the background when a click is clicked and the background after a click is released for the listview.

Source: Internet
Author: User
Tags transparent color

In this example,

(1) the specified background is displayed when you click the item of listview,

(2) After the ticket is released, the item you just clicked will have a 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 very simple, and the background is changed dynamically in the adapter:

if (position == selectedPosition){            convertView.setBackgroundResource(R.color.left_selected);        }else{            convertView.setBackgroundResource(R.color.left_normal);        }
In addition, the selectedposition is updated in time in the Click Event of the listview:

Leftlv. setonitemclicklistener (New adapterview. onitemclicklistener () {@ override public void onitemclick (adapterview <?> Parent, view, int position, long ID) {// update the background color firstclassadapter = (firstclassadapter) (parent. getadapter (); adapter. setselectedposition (position); adapter. notifydatasetchanged ();}});

However, the problem arises: After (2) is set, the effect of (1) is gone !!!

This is because, when setting

convertView.setBackgroundResource(R.color.left_selected);

The color specified in listselector in (1) will be overwritten.

There are two solutions:

(1)

Change a solid color background of convertview to a selector and set the color to transparent when you click it (the listselector color is displayed below ). The following are selector_left_normal.xml and selector_left_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):

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

(2)

Refer to (1) to remove the listselector attribute of listview and copy its color to the two selectors above to replace the transparent color.

That is to say, when you set the background color after clicking the listview entry,

(A) If the entry is selected, it is set to a color.

(B) Otherwise, set the color to a selector, and specify the color when clicking or not clicking in the selector.

The problem is solved successfully.


Set the background when a click is clicked and the background after a click is released for the listview.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.