为了解决我的问题,想想Twitter:有一个推文列表。您可以单击推文的任何部分以单独查看推文。如果在Tweet中使用@标记了某个帐户,那么如果您点击该帐户名称,则会转到该帐户的个人资料。
这几乎就是我在我的应用中尝试做的事情。我有自定义布局的ListView我会称之为墙贴。我的墙贴包含TextView,如果用户在此文本视图中使用@标记,例如@username,然后我创建@username spannable并为该文本设置onClickListener。在我的实现中,我知道spannable字符串已成功设置,因为默认情况下文本正确加下划线并为蓝色。问题是,当我点击文字时,我的点击被视为我点击了墙贴,并且发布了贴墙帖帐户的用户被打开以供查看,而不是被标记的用户。
总而言之,在给出上下文的情况下,我的问题是:如何使spannable字符串单击覆盖包含spannable字符串的ListView项的单击?当不单击spannable字符串时,我仍然需要ListView OnItemClickListener来处理ListView项目的点击。感谢您的帮助,我真的很感激!
P.S。 我查看了类似的问题,例如this one,其中没有答案,this one,其建议在此处添加此行:
txtViewTwo.setMovementMethod(LinkMovementMethod.getInstance());
使我的spannable字符串可以点击,但无效点击ListView项目。
答案 0 :(得分:1)
以下为我工作
int ID = txtID.text.Replace(",","").Replace("$", "");
修改强>
还应该阅读ListView: TextView with LinkMovementMethod makes list item unclickable?以获得一种解决方法,使非Text的部分TextView上的点击进入父视图
答案 1 :(得分:0)
尝试一下,它对我有用
class MyClickableSpan extends ClickableSpan {// extend ClickableSpan
Object object;
String title;
private boolean mIsPressed;
private int mPressedBackgroundColor = 0xffeeeeee;
private int mNormalTextColor;
private int mPressedTextColor;
public MyClickableSpan(Object o) {
super();
object = o;
}
public MyClickableSpan(Object o, String title) {
super();
object = o;
this.title = title;
}
@Override
public void onClick(View widget) {
}
@Override
public void updateDrawState(TextPaint ds) { // In here Customise your state colors
super.updateDrawState(ds);
ds.setUnderlineText(false); // set to false to remove underline
ds.setColor(Color.parseColor("#363636")); // ds.setColor(mIsPressed ? mPressedTextColor : mNormalTextColor);
ds.bgColor = mIsPressed ? mPressedBackgroundColor : 0xffffffff;
}
public void setPressed(boolean isSelected) {
mIsPressed = isSelected;
}
}
public class MovementMethod extends LinkMovementMethod {
private MyClickableSpan mPressedSpan;
@Override
public boolean onTouchEvent(TextView textView, Spannable spannable, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
mPressedSpan = getPressedSpan(textView, spannable, event);
if (mPressedSpan != null) {
mPressedSpan.setPressed(true);
Selection.setSelection(spannable, spannable.getSpanStart(mPressedSpan),
spannable.getSpanEnd(mPressedSpan));
}
} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
MyClickableSpan touchedSpan = getPressedSpan(textView, spannable, event);
if (mPressedSpan != null && touchedSpan != mPressedSpan) {
mPressedSpan.setPressed(false);
mPressedSpan = null;
Selection.removeSelection(spannable);
}
} else {
if (mPressedSpan != null) {
mPressedSpan.setPressed(false);
super.onTouchEvent(textView, spannable, event);
}
mPressedSpan = null;
Selection.removeSelection(spannable);
}
return true;
}
public SpannableStringBuilder appendClickableText(Object o, SpannableStringBuilder sb, String text) {
// SpannableStringBuilder sb = new SpannableStringBuilder();
StyleSpan b = new StyleSpan(android.graphics.Typeface.BOLD);
sb.append(text);
MyClickableSpan clickableSpan = new MyClickableSpan(o);
sb.setSpan(clickableSpan, sb.length() - text.length(), sb.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
sb.setSpan(b, sb.length() - text.length(), sb.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
try {
if (o instanceof User) {
if (((User) o).isPure()) {
final ForegroundColorSpan fcs = new ForegroundColorSpan(Color.parseColor(context.getString(R.color.pure_gold)));
sb.setSpan(fcs, 0, user.getName().length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
} catch (Exception ignored) {
}
}