Android Click on a ListView (or GridView) item to make its inside TextView discolored, click on another to restore the original color

Source: Internet
Author: User

Today is a project, is to do video app, if the TV show has many episodes, click on an item, play different episodes, to have a click effect, and the default is to choose the first one. It took a while, and I felt a little difficult.

Degrees, and now share with you, here is the effect shows:

This class is an example of the main class:

<span style= "FONT-SIZE:14PX;"    >public Final class Mainactivity extends Activity {private GridView GridView;    Private arraylist< string> numberlist=new arraylist<string> ();        public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main);        "Mmediaitem.mdirector---------------" +videodetailactivity_phone.mediaitem.mdes);                GridView = (GridView) Findviewbyid (R.id.video_detail_series_gridview);        for (int j = 0; J <; J + +) {Numberlist.add ("" + (j+1));        } final Seriesfragmentdataadapter seriesadapter = new Seriesfragmentdataadapter (this,numberlist);                Gridview.setadapter (Seriesadapter); Gridview.setonitemclicklistener (New Onitemclicklistener () {@Override public void Onitemclick (adapte Rview<?> Parent, view view, int position, long ID) {//TODO Auto-generated method stub//This sentence is to inform adapter change the selected position seriesadapter.clearselection (position);//The key is this sentence,            Passion, it can let the ListView changed the data reloaded again, to achieve the effect you want seriesadapter.notifydatasetchanged ();    }        });    }} This class is an example of Adapter:public class Seriesfragmentdataadapter extends Baseadapter {private Layoutinflater inflater;    Private arraylist<string> numberlist=new arraylist<string> ();    private int selectedposition=0; Public Seriesfragmentdataadapter (context context, arraylist<string> numberlist) {inflater = Layou        Tinflater.from (context);    This.numberlist = numberlist;    } @Override public int getcount () {//TODO auto-generated Method stub return Numberlist.size (); } @Override public Object getItem (int position) {//TODO auto-generated method stub return NumberList    . get (position); } @Override public long getitemid (int position) {//TODO Auto-generated method stub return position;    }//This sentence is the click of the ListView position, passed over public void clearselection (int position) {selectedposition = position; } @Override public View getView (int position, view Convertview, ViewGroup parent) {//TODO auto-generated Me                Thod stub viewholder holder;            if (Convertview = = null) {holder = new Viewholder ();            Convertview = inflater.inflate (R.layout.global_series_item, NULL);            Holder.textview = (TextView) convertview. Findviewbyid (R.id.video_detail_series_item_text);        Convertview.settag (holder);        } holder = (Viewholder) convertview.gettag ();        if (Numberlist.get (position). Length () > 0) {holder.textview.setText (Numberlist.get (position)); }//Decide which item to click on, then judge, let his textview discoloration if (selectedposition==position) {Holder.textview.setTextColor (C                Olor.parsecolor ("#ff6501")); }else{            Holder.textview.setTextColor (Color.parsecolor ("#404040"));    } return Convertview;    } public class Viewholder {public TextView TextView; }}</span>

Android Click on a ListView (or GridView) item to make its inside TextView discolored, click on another to restore the original color

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.