问题描述
- ListView的item中实现@功能,点击时跳转执行两次的问题,
-
下面是一个自定义的TextView:
`public class TextViewFixTouchConsume extends TextView {public TextViewFixTouchConsume(Context context) { super(context); } public TextViewFixTouchConsume(Context context, AttributeSet attrs) { super(context, attrs); } public TextViewFixTouchConsume(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override public boolean onTouchEvent(MotionEvent event) { if (getMovementMethod() == null ) { boolean result = super.onTouchEvent(event); return result; } MovementMethod m = getMovementMethod(); setMovementMethod(null); boolean mt = m.onTouchEvent(this, (Spannable) getText(), event); if (mt && event.getAction() == MotionEvent.ACTION_DOWN) { event.setAction(MotionEvent.ACTION_UP); mt = m.onTouchEvent(this, (Spannable) getText(), event); event.setAction(MotionEvent.ACTION_DOWN); } boolean st = super.onTouchEvent(event); setMovementMethod(m); setFocusable(false); return mt || st; }
}`
下面是在adapter中写的代码:mHolder.content.setText(parser.replace(string, true)); mHolder.content.setMovementMethod(LinkMovementMethod.getInstance());
下面是replace方法以及自定义的ClickableSpan:
public CharSequence replace(CharSequence text, boolean flag) { text = Html.fromHtml(MicroblogUtil.getLinkString(text.toString())); SpannableStringBuilder builder = new SpannableStringBuilder(text); if (flag) { if (text instanceof Spannable) { int end = text.length(); // 其实就是得到sp1 Spannable sp = (Spannable) text; // 得到其中所有的关键字的数组 URLSpan[] urls = sp.getSpans(0, end, URLSpan.class); // 清楚掉所有的关键字标志 builder.clearSpans(); for (URLSpan url : urls) { // 将关键字数组中的文字添加到新生成的style中去 MyURLSpan myURLSpan = new MyURLSpan(url.getURL()); builder.setSpan(myURLSpan, sp.getSpanStart(url), sp.getSpanEnd(url), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } } } else { Matcher matcher = mPattern.matcher(text); while (matcher.find()) { int resId = mSmileyToRes.get(matcher.group()); builder.setSpan(new ImageSpan(mContext, resId), matcher.start(), matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } } return builder; } /** * 继承于可点击的标签 * * @author excuce * */ private class MyURLSpan extends ClickableSpan { private String mUrl; /** * 构造器 * * @param url * 可以点击的关键字,构造时传入的 */ public MyURLSpan(String url) { mUrl = url; } @Override public void updateDrawState(TextPaint ds) { ds.setColor(ds.linkColor); ds.setUnderlineText(false);// <span // style="color: red;">//去掉下划线</span> } @Override public void onClick(View widget) { // System.out.println(mUrl.trim().substring(ONE)); Intent intent = new Intent(mContext, WeiBoPersonalActivity.class); intent.putExtra(KEY_NAME, mUrl.trim().substring(ONE)); mContext.startActivity(intent); // Log.d("MyURLSpan", "MyURLSpan"); } }
帮忙看看是怎么回事?这个方法我在模拟器上面试过没有出现跳转两次的问题,但在真机上出现了。
解决方案
@Override
public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction();
boolean flag = false;
switch (action) {
case MotionEvent.ACTION_DOWN:
MovementMethod m = getMovementMethod();
setMovementMethod(null);
boolean mt = m.onTouchEvent(this, (Spannable) getText(), event);
if (mt) {
event.setAction(MotionEvent.ACTION_UP);
mt = m.onTouchEvent(this, (Spannable) getText(), event);
event.setAction(MotionEvent.ACTION_DOWN);
}
boolean st = super.onTouchEvent(event);
setMovementMethod(m);
setFocusable(false);
flag = mt || st;
break;
default:
break;
}
return flag;
}
改一下这个方法就行!
时间: 2024-10-30 12:19:14