Question about clicking listview to change the text color of the Child Control

Source: Internet
Author: User

The problem of changing the color of text in listview is tricky. In fact, many people have encountered problems, but there are not many solutions. This post aims to help you

To solve this problem, let's take a look at it in two ways.

1. Use selector and settextcolor

This is a method with high execution efficiency.

1. Find the control to be discolored In the adapter XML of listview and use settextcolor or style to change the color, as shown below:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent" android:layout_height="wrap_content"android:orientation="horizontal" android:paddingLeft="5dip"android:descendantFocusability="blocksDescendants" android:background="@drawable/listview_state_bg"android:paddingRight="5dip"><ImageButton android:id="@+id/headphoto" android:layout_width="45dip"android:layout_height="45dip" android:layout_marginTop="10dip"android:layout_marginLeft="10dip" android:focusable="false"android:background="@drawable/recfriend_pic_box" android:scaleType="fitCenter"android:src="@drawable/pic_bg" android:layout_marginBottom="10dip"/><LinearLayout android:layout_width="fill_parent"    android:id="@+id/text_layout"android:layout_height="wrap_content" android:orientation="vertical"android:paddingLeft="10dip" android:paddingTop="8dip" android:focusable="false"android:layout_weight="1"><LinearLayout android:orientation="horizontal" android:focusable="false"android:layout_width="fill_parent" android:layout_height="wrap_content"><TextView android:id="@+id/name" android:layout_width="wrap_content"android:layout_height="wrap_content" android:textSize="16dip"style="@style/textview_title"android:focusable="false"/><ImageView android:id="@+id/pageicon" android:layout_width="wrap_content"android:layout_height="wrap_content" android:src="@drawable/page_icon_new"android:focusable="false"android:visibility="gone"/><LinearLayout android:layout_width="wrap_content" android:focusable="false"android:layout_height="wrap_content" android:layout_weight="1"/></LinearLayout><TextView android:id="@+id/text1" android:layout_width="wrap_content"     android:layout_marginTop="4dip" android:focusable="false"android:layout_height="wrap_content" android:textColor="@drawable/rec_button_color"android:textSize="14sp" android:visibility="gone"/><TextView android:id="@+id/text2" android:layout_width="wrap_content"    android:focusable="false"android:layout_height="wrap_content" android:textColor="@drawable/rec_button_color"android:textSize="14sp" android:visibility="gone"/></LinearLayout><CheckBox android:id="@+id/checkbox" android:layout_width="wrap_content"    android:layout_height="wrap_content" android:layout_marginRight="5dip"     android:focusable="false" android:button="@drawable/checkbox"       android:layout_gravity="center_vertical"/><ImageButton android:id="@+id/arrow" android:layout_width="wrap_content"    android:focusable="false"android:layout_height="wrap_content" android:background="@drawable/profile_icon_arrow"android:layout_gravity="center_vertical" android:layout_marginRight="5dip" android:visibility="gone"/></LinearLayout>

Note that there is a sentence in linearlayout, the outermost layer of your adapter.

android:descendantFocusability="blocksDescendants"

It is required. Otherwise, the click color cannot be changed. Its function is to overwrite the onclick event of the Child control. Explanation:

Android: descendantfocusability

Defines the relationship between the viewgroup and its descendants when looking for a view to take focus.

Must be one of the following constant values.

Constant Value Description
beforeDescendants 0 The viewgroup will get focus before any of its descendants.
afterDescendants 1 The viewgroup will get focus only if none of its descendants want it.
blocksDescendants 2 The viewgroup will block its descendants from grouping focus.

The ID name, text1, and text2 are all textviews that need to be discolored. Style and textcolor are used respectively. rec_button_color.xml is as follows:

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android" >    <item android:state_pressed="false" android:color="@color/profile_color"/>    <item android:state_pressed="true" android:color="@color/white"/></selector>

Be sure to write it in the drawable folder.

Textview_title pointed to by style (not Android: style) is written in values/style. XML, as follows:

<?xml version= "1.0" encoding= "utf-8"?><resources xmlns:adnroid="http://schemas.android.com/apk/res/android">    <style name="textview_title">  <item name="android:textColor">@color/title_color</item>   <item name="android:duplicateParentState">true</item>  </style></resources>

@ Color/title_color is an XML file written in the Res/color folder. It is similar to rec_button_color.xml:

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">  <item android:state_pressed="true" android:color="@color/white" />   <item android:color="@color/black" /> </selector>

2. Now the settings of the XML file are complete. Under normal circumstances, click to change color. The problem is often found here,

Sometimes our listeview item requires a click event. For example, the following functions are implemented in getview () in the listview adapter class:

view.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {if(holder.checkbox.isChecked()){holder.checkbox.setChecked(false);}else {holder.checkbox.setChecked(true);} } }});

That is, click item to select the checkbox. If this code is added, the click color will not be successful, because our external view will overwrite the click events of the internal control and will not execute

To the click color section of the Child control.

A work und is required, that is, to add setonitemclicklistener to the listview in the activity that contains the listview, instead of using view. setonclicklistener:

mListView.setOnItemClickListener(new OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> parent, View view,int position, long id) {RecommendFriendsAdapter.ViewHolder holder = (RecommendFriendsAdapter.ViewHolder)view.getTag();if(holder.checkbox!= null){if(holder.checkbox.isChecked()){holder.checkbox.setChecked(false);}else {holder.checkbox.setChecked(true);} }}});

In this way, the click color is changed, and other features required for the click are also implemented.

If you need to change the color of the checkbox when you click the parent control, add it to the checkbox at the top.

android:button="@drawable/checkbox"

Here, checkbox. XML is:

<?xml version= "1.0" encoding= "UTF-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <item android:drawable="@drawable/btn_checkbox_normal" android:state_checked="false" android:state_pressed="false"/>    <item android:drawable="@drawable/btn_checkbox_pressed" android:state_checked="false" android:state_pressed="true"/>    <item android:drawable="@drawable/bth_checkbox_normal" android:state_checked="true" android:state_pressed="false"/>    <item android:drawable="@drawable/bth_checkbox_pressed" android:state_checked="true" android:state_pressed="true"/></selector>

In this way, the selected color is changed.

LZ is exhausted. This solution has been running for at least half a month before and after, because the setonclicklistener problem has been blocked, and now it has finally come up!

I hope to help myself and everyone.

2. The following method does not find an alternative to the above method, which can be used to achieve results in extreme situations,

Its disadvantage is that it sometimes triggers the click effect when sliding, so that the sliding becomes relatively slow, but it is acceptable as a temporary method,

The first method is preferred in the same case.

Add the following to the getview () method of the adapter:

view.setOnTouchListener(new View.OnTouchListener() {@Overridepublic boolean onTouch(View v, MotionEvent event) {// TODO Auto-generated method stubLog.d("meng", "action + "+event.getAction());if(event.getAction() == 0){view.setBackgroundColor(Color.parseColor("#2cb1e1"));holder.name.setTextColor(Color.WHITE);holder.text1.setTextColor(Color.WHITE);holder.text2.setTextColor(Color.WHITE);}else if(event.getAction() == 2){view.setBackgroundColor(Color.parseColor("#2cb1e1"));holder.name.setTextColor(Color.WHITE);holder.text1.setTextColor(Color.WHITE);holder.text2.setTextColor(Color.WHITE);}else if(event.getAction() == 3){view.setBackgroundColor(Color.TRANSPARENT);holder.name.setTextColor(Color.BLACK);holder.text1.setTextColor(R.color.profile_color);holder.text2.setTextColor(R.color.profile_color);}else if(event.getAction() == 1){view.setBackgroundColor(Color.TRANSPARENT);holder.name.setTextColor(Color.BLACK);holder.text1.setTextColor(R.color.profile_color);holder.text2.setTextColor(R.color.profile_color);}else{Log.d("meng", "else + "+event.getAction());view.setBackgroundColor(Color.TRANSPARENT);holder.name.setTextColor(Color.BLACK);holder.text1.setTextColor(R.color.profile_color);holder.text2.setTextColor(R.color.profile_color);}return false;}

Note: If you want to change the background at the same time, it is best to use the color value. Using images will cause layout confusion and must be used.

View. setbackgroundcolor (color. parsecolor ("#2cb1e1 "));

Do not use your own id r. color. XXX. The color will change and it will not work.

View. setbackgroundresource (R. color. item_click_color );

Because some mobile phones, especially those in the 4.0 system, will not change color after clicking!

In fact, else is dispensable for insurance.

Reference http://gundumw100.iteye.com/blog/1169065

PS: Today, I found a website has transferred my article, but it does not indicate the author or even write it. I hope you will repost it.

Enter at least one name from mengweiqi33. Thank you!

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.