Learn about Android custom controls display the ability to like a user and access the user's home page by user name (40)

Source: Internet
Author: User


The effect is similar to displaying the user-like interface, and we can access each individual's profile by clicking on a different nickname.


For common controls, click the Git address below the article.


The first step:

We build a class for each person who likes to represent a personal message:

Person:

public class Person {public String name;public int age;}

It's simple, just a name and age.

Step Two:

Customizing the TextView control

Create Personlistview and inherit Baseontextview this abstract class:

public class Personlistview extends baseontextview<person> {public Personlistview (context context, AttributeSet attrs, int defstyle) {Super (context, attrs, Defstyle);} Public Personlistview (context context, AttributeSet Attrs) {Super (context, attrs);} Public Personlistview (Context context) {super (context);} public void Setvotename (arraylist<person> list, int index) {this.getinfo (list); Setvotelist (list, index);} /** * Set like name */@Overridepublic String getvotename (person data) {return data.name;} /** * Get the message like * * * @Overridepublic list<person> getInfo (list<person> list) {return list;}


Step Three:

Put the custom TextView in the XML.

Person_item.xml:

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:layout_width=" match_parent "    android:layout_height=" match_parent "    android:o rientation= "vertical" >    <com.example.ontextview.personlistview        android:id= "@+id/tv_vote_names"        android:layout_width= "fill_parent"        android:layout_height= "wrap_content"        android:background= "@ Android:color/white "        android:textscalex=" 2 "        android:textsize=" 14sp ">    </ Com.example.ontextview.personlistview></linearlayout>

Fourth Step:

Create our activity.

Mainactivity:

public class Mainactivity extends Activity {private ListView lv_lsit;private arraylist<person> personlist=new Arraylist<person> ();p rivate personlistadapter mpersonlistadapter=new personlistadapter (); @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_ Main); Initview ();} private void Initview () {string[] STRs = {"Naruto", "Kaka West", "Whirlpool Naruto", "Yu zhi bo", "Yu Uchiha Help", "Sakura", "Li Loch", "Big Snake pill", "take a name So hard", "Please Don't hurt me Again"}; for (int. i=0;i<strs.length;i++) {person obj=new person (); Obj.name=strs[i];p ersonlist.add (obj);} lv_lsit= (ListView) Findviewbyid (r.id.lv_lsit); Lv_lsit.setadapter (Mpersonlistadapter); Mpersonlistadapter.notifydatasetchanged ();} Class Personlistadapter extends baseadapter{@Overridepublic int GetCount () {return 1;} @Overridepublic Object getItem (int position) {return position;} @Overridepublic long Getitemid (int position) {return position;} @Overridepublic view GetView (int position, view Convertview, ViewGroup parent) {ViewholdEr viewholder;if (convertview==null) {viewholder=new viewholder (); Convertview=layoutinflater.from ( Mainactivity.this). Inflate (R.layout.person_item, null); viewholder.tv_vote_names= (Personlistview) Convertview.findviewbyid (R.id.tv_vote_names); Convertview.settag (Viewholder);} Else{viewholder= (Viewholder) Convertview.gettag ();} ViewHolder.tv_vote_names.setVoteList (personlist, 0); return convertview;}} Static class Viewholder{personlistview tv_vote_names;}}


Finally, add an event to the OnClick method in the Textviewspan class.


public class Textviewspan<t> extends Clickablespan {private String clickstring;private Context mcontext;private int selectclick;private T voteperson;public textviewspan (String clickstring, context context, int selectclick) { this.clickstring = Clickstring;this.mcontext = Context;this.selectclick = Selectclick;} /** * Set the message like people *  * @param t */public void SetInfo (T t) {Voteperson = t;} @Overridepublic void Updatedrawstate (Textpaint ds) {Ds.setcolor (Mcontext.getresources (). GetColor (R.color.main_link ));d S.setunderlinetext (false); } @Overridepublic void OnClick (View widget) {switch (Selectclick) {case 0://opens the personal home interface person man = Voteperson; Toast.maketext (Mcontext, Person.name, Toast.length_short). Show (); Break;case 1:break;default:break;}}}




Personal GitHub Project Address: Https://github.com/LinhaiGu/OnTextView

Reprint Please specify source:http://blog.csdn.net/hai_qing_xu_kong/article/details/46225697 Emotional Control _

Learn about Android custom controls display the ability to like a user and access the user's home page by user name (40)

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.