我有一个列表适配器,用于处理带有图像和文本的对象,我想:
这是我当前的适配器实现:
public class StatusAdapter extends ArrayAdapter<BlipStatus> {
@Override
public View getView(final int pos, final View convertView, final ViewGroup parent) {
// snip...
final Status status = statuses.get(pos);
// snip...
final View s = v.findViewById(R.id.status);
if (!(s instanceof TextView)) throw new RuntimeException("Expected textView instance");
TextView stv = (TextView) s;
stv.setText(prepareStatusContent(status.getBody()));
stv.setLinksClickable(true);
stv.setMovementMethod(LinkMovementMethod.getInstance());
// snip...
final View a = v.findViewById(R.id.avatar);
if (!(a instanceof ImageView)) throw new RuntimeException("Expected imageView instance");
String p = status.getAvatarPath();
try {
((ImageView) a).setImageBitmap(AvatarUtils.getAvatarFromPath(p));
} catch (IOException e) {
Log.w(TAG, "Failed to fetch avatar from path : " + p);
}
a.setOnClickListener(new View.OnClickListener() {
public void onClick(final View view) {
final Context c = getContext();
final Intent i = new Intent(c, UserActivity.class);
i.putExtra(UserActivity.USER_NAME, status.getUser());
c.startActivity(i);
}
});
return v;
}
private CharSequence prepareStatusContent(String s) {
SpannableString ss = new SpannableString(s);
Pattern p = Pattern.compile(URL_PATTERN);
Matcher m = p.matcher(s);
while (m.find()) {
int start = m.start();
int end = m.end();
final String url = s.substring(start, end);
ss.setSpan(new ExternalUrlSpan(new View.OnClickListener() {
public void onClick(View view) {
Toast.makeText(getContext(), url, Toast.LENGTH_LONG);
}
}), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
return ss;
}
}
和ExternalUrlSpan类:
public class ExternalUrlSpan extends ClickableSpan {
private final View.OnClickListener listener;
public ExternalUrlSpan(View.OnClickListener listener) {
super();
this.listener = listener;
}
@Override
public void onClick(View view) {
listener.onClick(view);
}
}
另外我正在打电话
registerForContextMenu()
使用我的适配器作为活动代码中的参数的ListView实例的方法。
在结果中,我有列表显示带有可点击图像的元素,按预期启动某些活动,并且在长按元素后可以使用上下文菜单。但是,当我尝试单击列表元素的文本视图中的链接时,它会被悬停,但整个点击事件由上下文菜单相关的侦听器处理。
有谁知道如何让它完全奏效?
答案 0 :(得分:0)
Toast.makeText(getContext(), url, Toast.LENGTH_LONG).show();