Android ListView Item Background Color setting and item click Unresponsive Solution _android

Source: Internet
Author: User

The following are some of the most common problems when using ListView.
1. How to change the background color of the item and press the color
ListView By default, the background color of item is black, which is yellow when clicked by the user. If you need to modify a custom background color, there are generally three methods:
1) Set Listselector
2 Set the item's background in the layout file
3) set in the GetView of adapter
These three methods can achieve change item default background color and press the color, below to explain separately, but before this need to write good selector.xml file;

Copy Code code as follows:
<?xml version= "1.0" encoding= "Utf-8"?>
<selector
Xmlns:android= "Http://schemas.android.com/apk/res/android" >
<item android:state_pressed= "true" android:drawable= "@color/light_blue" ></item>
<item android:state_pressed= "false" android:drawable= "@color/sgray" ></item>
</selector>

To change the button or ListView item default background color, you can use the selector. Drawable can be set as a color resource or as a picture resource.
1) Setting up the Listselector of ListView
Copy Code code as follows:

<listview
Android:id= "@+id/history_list"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
Android:divider= "#565C5D"
android:dividerheight= "3DP"
Android:listselector= "@drawable/selector"
Android:cachecolorhint= "@android: Color/transparent" >
</ListView>

2 The Background property is set in the ListItem layout file, and the following is the ListItem layout file
Copy Code code as follows:

<?xml version= "1.0" encoding= "Utf-8"?>
<relativelayout
Xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
android:background= "@drawable/selector" >
<textview
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:text= "Historical Records"
Android:textcolor= "#ffffff"
Android:textsize= "20SP"
Android:layout_centerinparent= "true" >
</TextView>
</RelativeLayout>

3) Set in the GetView method of adapter
Copy Code code as follows:

if (Convertview ==null)
{
Convertview = Layoutinflater.from (context). Inflate (R.layout.listitem, NULL);
}
Convertview.setbackgroundresource (R.drawable.selector);

The above method can achieve the same effect, is to change the item default background color and click on the background color, the third method is the most flexible, if ListView odd rows and even rows need to be set to different selector, can only use a third method.
2. When containing Button,checkbox control, click No response problem.
If the ListItem includes a button or a checkbox, the ListItem loses focus by default, causing the event that cannot respond to the item. The most common solution is to set the Descendantfocusability property in the ListItem layout file.
Copy Code code as follows:

<?xml version= "1.0" encoding= "Utf-8"?>
<relativelayout
Xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:paddingtop= "10DP"
Android:paddingbottom= "10DP"
android:paddingleft= "5DP"
android:paddingright= "5DP"
android:descendantfocusability= "Blocksdescendants" >

<checkbox
Android:id= "@+id/history_item_checkbt"
android:layout_height= "30DP"
Android:layout_width= "Wrap_content"
Android:layout_centervertical= "true"
Android:layout_alignparentleft= "true"
Android:checked= "false"
>
</CheckBox>

<imageview
Android:id= "@+id/history_item_image"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:layout_centervertical= "true"
android:layout_torightof= "@id/HISTORY_ITEM_CHECKBT"
android:background= "@drawable/item_icon" >
</ImageView>


<button
Android:id= "@+id/history_item_edit_bt"
Android:layout_alignparentright= "true"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:layout_centervertical= "true"
android:text= "Edit"
Android:textcolor= "#ffffff"
Android:textsize= "14SP"
android:background= "@drawable/BUTTON_BG" >
</Button>

<textview
Android:id= "@+id/history_item_time_tv"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:layout_centervertical= "true"
Android:textcolor= "#565C5D"
Android:textsize= "14SP"
android:text= "10-01 10:20"
android:layout_marginright= "5DP"
android:layout_toleftof= "@id/history_item_edit_bt" >
</TextView>

<textview
Android:id= "@+id/history_item_title_tv"
android:layout_height= "Wrap_content"
Android:layout_width= "Fill_parent"
Android:layout_centervertical= "true"
Android:textcolor= "#565C5D"
Android:textsize= "14SP"
android:text= "Xxxxxxxxxxxxxxxxxxxxxxxx"
Android:ellipsize= "End"
android:maxlines= "1"
android:layout_torightof= "@id/history_item_image"
android:layout_toleftof= "@id/history_item_time_tv"
android:layout_marginleft= "3DP" >
</TextView>
</RelativeLayout>

Related Article

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.