一起学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
- 一起学android之自定义控件显示点赞用户并通过用户名称进入该用户主页的功能 (40)
- [一起学Hive]之二十-自定义HiveServer2的用户安全认证
- 用户控件&自定义控件----.Net再学
- servlet初步,简单的登录验证并显示用户名称
- 显示 用户名称
- 用户控件中控件名称的提示
- 用户自定义控件(评论的级别)
- 用户自定义控件的应用。
- 用户自定义控件的使用
- 用户自定义控件的应用
- 用户自定义控件的使用方法
- 自定义用户控件的使用
- 创建用户并授予该用户的独立环境变量权限
- 用户自定义控件 界面 不显示问题
- GridView 分页显示用户自定义控件遇到的问题
- 用户自定义控件的背景图片无法正常显示
- 通过获取用户ip地址定位到用户的城市并显示
- 自定义控件之-一个简单的用户登录控件
- xcode 报错xcrun: error: active developer path ("/Applications/Xcode.app/Contents/Developer/") does not
- boost graph --- 有向图中两点间所有路径(可处理有环情况)
- POJ 1942-Paths on a Grid(组合数学)
- C#解析 json
- C# 基础学习第一天
- 一起学android之自定义控件显示点赞用户并通过用户名称进入该用户主页的功能 (40)
- debian安装 Mongodb
- Chapter12. Foundation Classes and selector in Objective-C
- 天声人語 20150529
- [C语言] 对于scanf()函数使用%d格式说明符无法读取字符的个人理解
- 日经春秋 20150529
- ABAP性能优化之使用 “for all entries”
- JSON详解
- 可以运用到android项目中的一些小技巧