仿QQ控件,朋友圈
2015-11-30 21:29
423 查看
仿QQ空间列表显示,只要原理是自定义一个控件继承ClickableSpan,在Adapter里面动态添加内容,注释在代码里已经写的很清楚了
遗留了一个问题,希望有人看到解决了也分享一下解决办法:
就是人名和整个TextView的点击冲突,点人名两个点击事件都响应。。。。
代码下载地址:http://download.csdn.net/detail/qq55214/9313721
Activity:
Adapter:
自定义控件:
遗留了一个问题,希望有人看到解决了也分享一下解决办法:
就是人名和整个TextView的点击冲突,点人名两个点击事件都响应。。。。
代码下载地址:http://download.csdn.net/detail/qq55214/9313721
Activity:
public class MyActivity extends Activity { /** * Called when the activity is first created. */ private ListView listView; private MyAdapter mAdapter; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); listView = (ListView) findViewById(R.id.my_list); mAdapter = new MyAdapter(this); listView.setAdapter(mAdapter); // TODO ListView里面有ClickableSpan的时候会影响listview的点击事件 // TODO 需要在adapter的布局上添加 android:descendantFocusability="blocksDescendants" 设置属性 listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { Toast.makeText(MyActivity.this,"呵呵呵",Toast.LENGTH_SHORT).show(); } }); listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) { Toast.makeText(MyActivity.this,"呵ddd",Toast.LENGTH_SHORT).show(); return true; } }); } }
Adapter:
public class MyAdapter extends BaseAdapter { private Context mContext; private String name1 = "叶良辰"; private String name2 = "赵日天"; public MyAdapter(Context mContext) { this.mContext = mContext; } @Override public int getCount() { return 6; } @Override public Object getItem(int i) { return null; } @Override public long getItemId(int i) { return 0; } @Override public View getView(int i, View view, ViewGroup viewGroup) { ViewHolder holder; if (view == null){ holder = new ViewHolder(); view = LayoutInflater.from(mContext).inflate(R.layout.list_item_layout,null); holder.mLlPrsise = (LinearLayout) view.findViewById(R.id.ll_praise); holder.mLlReply = (LinearLayout) view.findViewById(R.id.ll_reply); view.setTag(holder); }else{ holder = (ViewHolder) view.getTag(); } // TODO 点赞得人 // TODO 这里可以用for循环动态添加,这就就添加了两个人 holder.mLlPrsise.removeAllViews(); ImageView praiseIcon = new ImageView(mContext); praiseIcon.setPadding(0, 0, 8, 0); praiseIcon.setImageDrawable(mContext.getResources().getDrawable(R.drawable.praise)); holder.mLlPrsise.addView(praiseIcon); final TextView praise = new TextView(mContext); praise.setMovementMethod(LinkMovementMethod.getInstance()); praise.setTextColor(mContext.getResources().getColor( R.color.comment_name)); praise.setTextSize(14); // 设置间距 praise.setPadding(2, 0, 2, 0); SpannableString user = new SpannableString(name1 + "、");//回复人的名字 TextViewSpan clickableSpan1 = new TextViewSpan(name1 + "、", mContext, 0, 0); clickableSpan1.setInfo(name1); user.setSpan(clickableSpan1, 0, name1.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); praise.setText(user); holder.mLlPrsise.addView(praise); final TextView praise2 = new TextView(mContext); praise2.setMovementMethod(LinkMovementMethod.getInstance()); praise2.setTextColor(mContext.getResources().getColor( R.color.comment_name)); praise2.setTextSize(14); praise2.setPadding(2, 0, 2, 0); SpannableString user2 = new SpannableString(name2);//回复人的名字 TextViewSpan clickableSpan2 = new TextViewSpan(name2, mContext, 0, 0); clickableSpan2.setInfo(name2); user2.setSpan(clickableSpan2, 0, name2.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); praise2.setText(user2); holder.mLlPrsise.addView(praise2); TextView count = new TextView(mContext); count.setTextColor(mContext.getResources().getColor( R.color.comment_bg)); count.setTextSize(14); count.setPadding(2, 0, 2, 0); count.setText("2人觉得很赞"); holder.mLlPrsise.addView(count); // TODO 评论内容 holder.mLlReply.removeAllViews(); TextView comment = new TextView(mContext); comment.setMovementMethod(LinkMovementMethod.getInstance()); comment.setTextColor(mContext.getResources().getColor( R.color.comment_bg)); comment.setTextSize(14); SpannableString commentTo = new SpannableString(name1); TextViewSpan clickName = new TextViewSpan(name1, mContext, 0,0); commentTo.setSpan(clickName,0,commentTo.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE); // TODO 处理人名的点击事件,需要什么参数传递过去一个对象(这里只传了了一个字符串) clickName.setInfo(name1); comment.setText(commentTo); comment.append(":在下叶良辰"); holder.mLlReply.addView(comment); TextView comment2 = new TextView(mContext); comment2.setMovementMethod(LinkMovementMethod.getInstance()); comment2.setTextColor(mContext.getResources().getColor( R.color.comment_bg)); comment2.setTextSize(14); SpannableString commentTo2 = new SpannableString(name2); TextViewSpan clickName2 = new TextViewSpan(name2, mContext, 0,0); commentTo2.setSpan(clickName2,0,commentTo2.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE); clickName2.setInfo(name2); comment2.setText(commentTo2); comment2.append("回复"); comment2.append(commentTo); comment2.append(":我赵日天第一个表示不服。。。"); holder.mLlReply.addView(comment2); // TODO 设置textview的点击颜色和点击事件 comment2.setBackgroundResource(R.drawable.comment_bg); comment2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Toast.makeText(mContext,"回复谁谁谁",Toast.LENGTH_SHORT).show(); } }); return view; } class ViewHolder{ LinearLayout mLlPrsise; LinearLayout mLlReply; } }
自定义控件:
public class TextViewSpan<T> extends ClickableSpan { private String clickString; private Context mContext; private int selectClick; private T votePerson; private int mColor; /** * * @param clickString 点击的内容 * @param context * @param selectClick 点击事件分类 * @param mColor 字体的颜色选择 */ public TextViewSpan(String clickString, Context context, int selectClick, int mColor) { this.clickString = clickString; this.mContext=context; this.selectClick=selectClick; this.mColor = mColor; } /** * 设置点赞人的信息 * @param t */ public void setInfo(T t){ votePerson = t; } @Override public void updateDrawState(TextPaint ds) { if (mColor == 1 ){ ds.setColor(mContext.getResources().getColor( R.color.event_reg_color_1)); } else if (mColor == 2){ ds.setColor(mContext.getResources().getColor( R.color.event_reg_color_2)); } else if (mColor == 3){ ds.setColor(mContext.getResources().getColor( R.color.event_reg_color_3)); }else{ ds.setColor(mContext.getResources().getColor( R.color.comment_name)); } ds.setUnderlineText(false); // 去掉下划线 } @Override public void onClick(View widget) { switch (selectClick) { case 0:// Toast.makeText(mContext,votePerson.toString(), Toast.LENGTH_SHORT).show(); break; case 1: /*Map<String,String> reply = (Map<String,String>)votePerson; Toast.makeText(mContext, reply.get(Constants.TALK_REPLY_NAME), Toast.LENGTH_SHORT).show();*/ break; default: break; } } }
相关文章推荐
- jQuery简单实现QQ空间点赞已经取消点赞
- android 标题动态变换(listview scrollview 滑动距离计算)
- 仿QQ空间的一款APP(二)
- 仿QQ空间的一款APP(一)
- 识别方法区
- Android方法数统计
- SSM框架整合仿QQ空间
- AutoGridView:轻松实现微信朋友圈九宫格
- qt_简易记事本_1
- Eclipse文档注释生成doc方法
- 编译GDAL支持OpenCL使用GPU加速
- Git使用教程
- k-近邻算法实现分类器
- javascript-reduce 方法 (Array)
- 加解密杂烩程序开发(二)
- WebKit之Node的继承关系图
- 信息安全系统设计基础第十二周总结
- Oracle Listener 动态注册 与 静态注册
- B. Mike and Fun---cf548B(暴力求解)
- 最新一课 老师指点用Listview适配器