程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> ontouchevent-ListView的item中實現@功能,點擊時跳轉執行兩次的問題,

ontouchevent-ListView的item中實現@功能,點擊時跳轉執行兩次的問題,

編輯:編程綜合問答
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;
}

改一下這個方法就行!

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved