The function of displaying a like user in a custom control of android and entering the user's homepage through the user name (40). The name of the android user
:
The above effect is similar to the interface for displaying a like user. We can click different nicknames to enter the personal homepage of each person.
For public controls, click the git address below the article.
Step 1:
We create a class for each person who likes to represent personal information:
Person:
public class Person {public String name;public int age;}
It's easy, just a name and age
Step 2:
Custom TextView Control
Create a PersonListView and inherit the abstract class BaseOnTextView:
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 the like name */@ Overridepublic String getVoteName (Person data) {return data. name;}/*** get the likes */@ Overridepublic List <Person> getInfo (List <Person> list) {return list ;}}
Step 3:
Put the custom TextView in 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:orientation="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>
Step 4:
Create an Activity.
MainActivity:
Public class MainActivity extends Activity {private ListView lv_lsit; private ArrayList <Person> personList = new ArrayList <Person> (); private PersonListAdapter mPersonListAdapter = new PersonListAdapter (); @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); initView ();} private void initView () {String [] strs = {"Naruto", "Kakashi", "vortex Naruto", "Yuzhi bo ", "Yu zhibo sas", "Sakura", "Li locks", "da shewan", "it's hard to get a name", "Please do not hurt me again "}; for (int I = 0; I <strs. length; I ++) {Person obj = new Person (); obj. name = strs [I]; personList. add (obj);} lv_lsit = (ListView) findViewById (R. id. lv_lsit); lv_lsit.setAdapter (mPersonListAdapter); mPersonListAdapter. policydatasetchanged ();} 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;}/*** sets the liked person information ** @ param t */public void setInfo (T t) {votePerson = t ;} @ Overridepublic void updateDrawState (TextPaint ds) {ds. setColor (mContext. getResources (). getColor (R. color. main_link); ds. setUnderlineText (false) ;}@ Overridepublic void onClick (View widget) {switch (selectClick) {case 0: // open the personal main interface Person person = (Person) 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
For more information, see http://blog.csdn.net/hai_qing_xu_kong/article/details/46225697 _