花了一天时间,终于解决了个大毛病
文字始终显示一半,纠结了半天 终于找到原因 是 超链接以外部分未响应listview 点击事件 看了一个大神的博客终于解决了 在此 贴上我的小代码,记录一下问题,以后长点儿心啊!! 知己诶进入主题
先说 Textview 部分文字加超链接 响应点击事件
public class MySpan { String strcontengt; TextView textView; int i; MessageInfo info; public MySpan(String strcontengt, TextView textView, int i, MessageInfo info) { this.strcontengt = strcontengt; this.textView = textView; this.i = i; this.info = info; } private SpannableString getClickableSpan() { //监听器 View.OnClickListener listener = new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MyMessageActivity.this, "Click Success", Toast.LENGTH_SHORT).show(); } }; SpannableString spanableInfo = new SpannableString(strcontengt); int firsStar = spanableInfo.toString().indexOf("《"); int firstEnd = spanableInfo.toString().indexOf("》") + 1; //可以为多部分设置超链接 spanableInfo.setSpan(new Clickable(listener), firsStar, firstEnd, Spanned.SPAN_MARK_MARK); return spanableInfo; } class Clickable extends ClickableSpan implements View.OnClickListener { private final View.OnClickListener mListener; public Clickable(View.OnClickListener listener) { mListener = listener; } @Override public void onClick(View view) { mListener.onClick(view); } } } //绑定Textview直接写解决方法了:MySpan mySpan = new MySpan(info.getContent(), vh.content, i, info); vh.content.setText(mySpan.getClickableSpan());
/**解决超链接外不响应listview item点击事件问题*/ vh.content.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { // TODO Auto-generated method stub boolean ret = false; CharSequence text = ((TextView) v).getText(); Spannable stext = Spannable.Factory.getInstance().newSpannable(text); TextView widget = (TextView) v; int action = event.getAction(); if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_DOWN) { int x = (int) event.getX(); int y = (int) event.getY(); x -= widget.getTotalPaddingLeft(); y -= widget.getTotalPaddingTop(); x += widget.getScrollX(); y += widget.getScrollY(); Layout layout = widget.getLayout(); int line = layout.getLineForVertical(y); int off = layout.getOffsetForHorizontal(line, x); ClickableSpan[] link = stext.getSpans(off, off, ClickableSpan.class); if (link.length != 0) { if (action == MotionEvent.ACTION_UP) { link[0].onClick(widget); } ret = true; } } return ret; } });