一起学android之自定义控件显示点赞用户并通过用户名称进入该用户主页的功能 (40)

来源:互联网 发布:js字符串转正则表达式 编辑:程序博客网 时间:2024/05/16 17:10

效果图:


以上效果类似于显示点赞用户的界面,我们可以通过点击不同的昵称进入每个人的个人主页。


关于公共控件,请点击文章下方的git地址。


第一步:

我们为每个点赞的人建一个类用来代表个人的信息:

Person:

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

很简单,就一个名字和年龄

第二步:

自定义TextView控件

创建PersonListView并继承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);}/** * 设置点赞姓名 */@Overridepublic String getVoteName(Person data) {return data.name;}/** * 获取点赞人的信息 */@Overridepublic List<Person> getInfo(List<Person> list) {return list;}}


第三步:

将自定义的TextView放入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>

第四步:

创建我们的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 = {"火影忍者", "卡卡西", "漩涡鸣人", "宇智波鼬" ,"宇智波佐助","小樱","李洛克","大蛇丸","取个名字好难啊","请不要再来伤害我"};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.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;}}


最后在TextViewSpan类中的onClick方法中添加事件。


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;}/** * 设置点赞人的信息 *  * @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:// 打开个人主界面Person person = (Person) votePerson;Toast.makeText(mContext, person.name, Toast.LENGTH_SHORT).show();break;case 1:break;default:break;}}}




                                                         个人GitHub项目地址:https://github.com/LinhaiGu/OnTextView

                                                          转载请注明出处:http://blog.csdn.net/hai_qing_xu_kong/article/details/46225697情绪控_

2 0
原创粉丝点击