How to achieve highlighted display in listview

Source: Internet
Author: User

The Renren client has a good navigation bar, as shown in. When you click ListView on the left side, the selected row is highlighted and the color of the selected row is blue (note: is to highlight a row after it is selected, rather than just highlighting it when it is clicked). If you click another row again, the new row will be highlighted, and the highlighted effect will be displayed below;

At the beginning of implementation, I plan to use the getChildAt (int
Pos) method, the result is found very cao egg, because of the ListView itself, when you View
View = listView. getChildAt (pos). When you change the View status, you will find that the highlighted view is often not yours.
The selected row is the other row. This is determined by the ListView itself. When designing the ListView, Google uses a shared method to reduce memory consumption, that is to say, multiple rows share one View, so the click will jump randomly (this is a very clever method, but it also suffers from programmers );
So another method is selected. ListView has an Adapter to display data, while getView () in this Adapter can obtain accurate rows, here we need to customize an Adapter that inherits from BaseAdapter to implement this function (if ArrayAdapter and other built-in adapters are used, this function will not be implemented); in addition, this ListView must set an attribute: listview. setChoiceMode (ListView. CHOICE_MODE_SINGLE );
That is, set to single-choice mode. When you click a row, the ListView will refresh the interface, and set the OnItemClickListener listener for the ListView. When you click a row, the subscript of the current row will be updated, all the code is as follows: Copy codeThe Code is as follows: public class ListViewDemo extends Activity {
Private ListView listview;
Private int cur_pos = 0; // The row currently displayed
Private String [] items_text = {"option 1", "option 2", "option 3", "option 4", "option 5 "};
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); // you must set this attribute; otherwise, the ListView will not refresh
Listview. setOnItemClickListener (new OnItemClickListener (){
@ Override
Public void onItemClick (AdapterView <?> Arg0, View arg1,
Int position, long id ){
Cur_pos = position; // update the current row
}
});
}
Private class MyAdapter extends BaseAdapter {
Private LayoutInflater inflater;
Public MyAdapter (Context context ){
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); // used to display the image
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 row selected in the ListView, change the display style
ConvertView. setBackgroundColor (Color. LTGRAY); // you can change the background Color of the entire row.
TV. setTextColor (Color. RED); // you can change the font Color.
}
Return convertView;
}
}
}
Public class ListViewDemo extends Activity {
Private ListView listview;
Private int cur_pos = 0; // The row currently displayed
Private String [] items_text = {"option 1", "option 2", "option 3", "option 4", "option 5 "};
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); // you must set this attribute; otherwise, the ListView will not refresh
Listview. setOnItemClickListener (new OnItemClickListener (){
@ Override
Public void onItemClick (AdapterView <?> Arg0, View arg1,
Int position, long id ){
Cur_pos = position; // update the current row
}
});
}
Private class MyAdapter extends BaseAdapter {
Private LayoutInflater inflater;
Public MyAdapter (Context context ){
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); // used to display the image
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 row selected in the ListView, change the display style
ConvertView. setBackgroundColor (Color. LTGRAY); // you can change the background Color of the entire row.
TV. setTextColor (Color. RED); // you can change the 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.