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: