仿QQ控件,朋友圈

仿QQ空间列表显示,只要原理是自定义一个控件继承ClickableSpan,在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;
			}
			
		}

}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值