ListView Highlight Implementation Method _android

Source: Internet
Author: User
Everyone client has a good navigation bar, as shown in the following figure, when you click on the left side of the ListView, the selected line will always be highlighted, the color of selected lines in the picture is shown as blue (note: The selection is always highlighted after the line, rather than just click on the highlight), if you click on another line, Then the new line is highlighted, the following to achieve the display of the highlight effect;

When I first realized it, I was going to use ListView's Getchildat (int
POS) method to achieve, the results found very Cao eggs, because ListView itself the reason, when you view
View=listview.getchildat (POS), and changing the state of this view, you will find that the highlight is often not your
The selected line, instead, is the other line, because ListView itself determines that Google, in designing ListView, uses a common way to reduce memory consumption by using multiple rows to share a view, So there will be click after the phenomenon of disorderly jump (this is a very clever method, but also bitter the program apes);
So I chose a different way, ListView has a adapter to display the data, and the GetView in this adapter can get the exact line, Here we need to customize an inherited from Baseadapter adapter to implement (if the use of Arrayadapter and other adapter, do not implement such a function); This listview must set an attribute: Listview.setchoicemode (listview.choice_mode_single);
That is, set to the radio mode, so that when you click a line, ListView will refresh the interface, there is to ListView set Onitemclicklistener Listener, when clicked on a line, update the current line subscript, all the code as follows:
Copy Code code as follows:

public class Listviewdemo extends activity {
Private ListView ListView;
private int cur_pos = 0;//the currently displayed row
Private string[] Items_text = {"Option one", "option two", "option three", "option Four", "option Five"};
Private int[] Items_img = {r.drawable.ic_launcher, r.drawable.ic_launcher,
R.drawable.ic_launcher, R.drawable.ic_launcher,
R.drawable.ic_launcher};
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_list_view_demo);
ListView = (ListView) Findviewbyid (R.id.listview);
Final Myadapter adapter = new Myadapter (this);
Listview.setadapter (adapter);
Listview.setchoicemode (listview.choice_mode_single);/Be sure to set this property, otherwise ListView will not refresh
Listview.setonitemclicklistener (New Onitemclicklistener () {
@Override
public void Onitemclick (adapterview<?> arg0, View arg1,
int position, long ID) {
Cur_pos = position;//Update when moving forward
}
});
}
Private class Myadapter extends Baseadapter {
Private Layoutinflater Inflater;
Public Myadapter {
Inflater = (layoutinflater) context
. Getsystemservice (Context.layout_inflater_service);
}
@Override
public int GetCount () {
return items_text.length;
}
@Override
Public Object getitem (int position) {
return items_text[position];
}
@Override
public long getitemid (int position) {
return position;
}
@Override
Public View GetView (int position, View Convertview, ViewGroup parent) {
LOG.E ("TEST", "refresh once");
Convertview = inflater.inflate (R.layout.list_child, NULL, FALSE);
ImageView img = (imageview) convertview
. Findviewbyid (r.id.list_child_img);//for displaying pictures
TextView TV = (TextView) convertview
. Findviewbyid (R.id.list_child_text);//Display text
Tv.settext (Items_text[position]);
Img.setimageresource (Items_img[position]);
if (position = = Cur_pos) {//If the current row is the selected row in ListView, change the display style
Convertview.setbackgroundcolor (Color.ltgray)//change the background color of the entire row
Tv.settextcolor (color.red);//Change font Color
}
return convertview;
}
}
}
public class Listviewdemo extends activity {
Private ListView ListView;
private int cur_pos = 0;//the currently displayed row
Private string[] Items_text = {"Option one", "option two", "option three", "option Four", "option Five"};
Private int[] Items_img = {r.drawable.ic_launcher, r.drawable.ic_launcher,
R.drawable.ic_launcher, R.drawable.ic_launcher,
R.drawable.ic_launcher};
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_list_view_demo);
ListView = (ListView) Findviewbyid (R.id.listview);
Final Myadapter adapter = new Myadapter (this);
Listview.setadapter (adapter);
Listview.setchoicemode (listview.choice_mode_single);/Be sure to set this property, otherwise ListView will not refresh
Listview.setonitemclicklistener (New Onitemclicklistener () {
@Override
public void Onitemclick (adapterview<?> arg0, View arg1,
int position, long ID) {
Cur_pos = position;//Update when moving forward
}
});
}
Private class Myadapter extends Baseadapter {
Private Layoutinflater Inflater;
Public Myadapter {
Inflater = (layoutinflater) context
. Getsystemservice (Context.layout_inflater_service);
}
@Override
public int GetCount () {
return items_text.length;
}
@Override
Public Object getitem (int position) {
return items_text[position];
}
@Override
public long getitemid (int position) {
return position;
}
@Override
Public View GetView (int position, View Convertview, ViewGroup parent) {
LOG.E ("TEST", "refresh once");
Convertview = inflater.inflate (R.layout.list_child, NULL, FALSE);
ImageView img = (imageview) convertview
. Findviewbyid (r.id.list_child_img);//for displaying pictures
TextView TV = (TextView) convertview
. Findviewbyid (R.id.list_child_text);//Display text
Tv.settext (Items_text[position]);
Img.setimageresource (Items_img[position]);
if (position = = Cur_pos) {//If the current row is the selected row in ListView, change the display style
Convertview.setbackgroundcolor (Color.ltgray)//change the background color of the entire row
Tv.settextcolor (color.red);//Change font Color
}
return convertview;
}
}
}

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.