Listview click to highlight a row

Source: Internet
Author: User
Tags getcolor

Listview click to highlight a row

Sometimes, when you click an item in listview, in order to let the user know that the row is clicked, the background color of the row to be clicked should be distinguished from other items not clicked, if you click an item, the color of the item background will change. How can this problem be solved? It is actually very simple. When you click an event in listview, you can get a position, it indicates that the item in the listview is clicked, and then the position in the getView () method in the adapter is compared. All operations on the interface must be performed in the getView () method,

Package com. example. listview; import java. util. arrayList; import android. app. activity; import android. OS. bundle; import android. view. layoutInflater; import android. view. view; import android. view. viewGroup; import android. widget. adapterView; import android. widget. adapterView. onItemClickListener; import android. widget. baseAdapter; import android. widget. listView; import android. widget. relativeLayout; import android. widget. textView; public class MainActivity extends Activity {private ListView listview; private ArrayList
 
  
Datas; private LayoutInflater inflater; private MyAdapter adapter; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); listview = (ListView) findViewById (R. id. listview); inflater = LayoutInflater. from (this); initData (); adapter = new MyAdapter (); listview. setAdapter (adapter); listview. setOnItemClickListener (new OnItemClickListener () {@ Overridepublic void onItemClick (AdapterView
  Arg0, View arg1, int position, long arg3) {adapter. setClickPosition (position); adapter. notifyDataSetChanged () ;}});} private void initData () {datas = new ArrayList
  
   
(); For (int I = 0; I <100; I ++) {datas. add ("I am item -------") ;}} class MyAdapter extends BaseAdapter {private int clickPosition =-1; public void setClickPosition (int clickPosition) {this. clickPosition = clickPosition ;}@ Overridepublic int getCount () {return datas. size () ;}@ Overridepublic Object getItem (int arg0) {return datas. get (arg0) ;}@ Overridepublic long getItemId (int arg0) {return arg0 ;}@ Overridepublic View getView (int position, View converView, ViewGroup arg2) {ViewHolder holder = null; if (converView = null) {converView = inflater. inflate (R. layout. item, null); holder = new ViewHolder (); holder. tvContent = (TextView) converView. findViewById (R. id. tvContent); holder. rlRoot = (RelativeLayout) converView. findViewById (R. id. rlRoot); converView. setTag (holder);} else {holder = (ViewHolder) converView. getTag () ;}if (clickPosition = position) {holder. rlRoot. setBackgroundColor (getResources (). getColor (R. color. click_chang_color);} else {holder. rlRoot. setBackgroundColor (getResources (). getColor (R. color. white);} holder. tvContent. setText (datas. get (position); return converView;} class ViewHolder {TextView tvContent; RelativeLayout rlRoot ;}}}
  
 

Effect:


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.