I have a list adapter that handles objects with some image and text and I want to:
- display context menu displayed when long click happens on whole list item
- start some activity when image is clicked
- handle clicks on links inside TextView (for now just displaying Toast message)
This is my current adapter implementation:
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, "Fail开发者_如何学Pythoned 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;
}
}
and ExternalUrlSpan class:
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);
}
}
Additionaly I'm calling
registerForContextMenu()
method with a ListView instance using my adapter as an parameter in an activity code.
In a result I'm having list displaying elements with clickable image that starts some activity as expected and context menu available after long click on an element. But when I'm trying to click a link inside text view of list element it gets hovered but whole click event is handled by context menu related listener and .
Does anyone know how to make it work altogether?
That was stupid. Forgotten to call show() method on Toast message
Toast.makeText(getContext(), url, Toast.LENGTH_LONG).show();
精彩评论