Android ListView's item background color Settings and item clicks unresponsive to the solution
- 2020-05-19 05:46:39
- OfStack
Here are some of the most common problems with listview.
1. How to change the background color of item and press the color
By default, listview's background is black and yellow when the user clicks. If you need to change it to a custom background color, there are three ways to do this in general:
1) set listSelector
2) set background of item in the layout file
3) set in getview of adapter
All three methods can change the default background color of item and press the color, which are described below, but you need to write the selector.xml file first.
<?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>
selector can be used when changing the item default background color of button or listview. drawable can be set as either a color resource or an image resource.
1) set listSelector of listview
<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) set the background property in the layout file of listitem. The following is the layout file of listitem
<?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=" The historical record "
android:textColor="#ffffff"
android:textSize="20sp"
android:layout_centerInParent="true">
</TextView>
</RelativeLayout>
3) set in the getView method of adapter
if(convertView ==null)
{
convertView = LayoutInflater.from(context).inflate(R.layout.listitem, null);
}
convertView.setBackgroundResource(R.drawable.selector);
All the above methods can achieve the same effect, which is to change the default background color of item and the background color when clicked. The third method is the most flexible. If the odd and even lines of listview need to be set to different selector, only the third method can be used.
2. Click no response when button, checkbox and other controls are included.
If listitem contains controls such as button or checkbox, listitem will lose focus by default, resulting in failure to respond to item events. The most common solution is to set descendantFocusability properties in listitem's layout file.
<?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=" The editor "
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>