解决listView中textview超链接导致listitem点击事件失效问题
ListView的Item里的TextView设置ClickableSpan和LinkMovementMethod导致ListView无法响应点击事件
- CharSequence charSequence;
- if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
- charSequence = Html.fromHtml(data.getContent(), Html.FROM_HTML_MODE_LEGACY);
- } else {
- charSequence = Html.fromHtml(data.getContent());
- }
- this.content.setText(charSequence);
- //this.content.setMovementMethod(LinkMovementMethod.getInstance());
- this.content.setOnTouchListener(onTextTouch);
- //this.content.setFocusable(false);
注意:要注释 setMovementMethod 才行
- 主要代码:
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- // TODO Auto-generated method stub
- ViewHolder holder;
- if (convertView==null) {
- holder = new ViewHolder();
- convertView = View.inflate(context, R.layout.list_item, null);
- holder.mtext = (TextView) convertView.findViewById(R.id.linktest);
- convertView.setTag(holder);
- }else{
- holder = (ViewHolder) convertView.getTag();
- }
- ((ViewGroup)convertView).setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);//加上次属性使焦点能够传递
- Spanned html = Html.fromHtml(list.get(position));
- Object[] spans = html.getSpans(0, html.length(), URLSpan.class);
- holder.mtext.setAutoLinkMask(Linkify.WEB_URLS);
- holder.mtext.setMovementMethod(LinkMovementMethod.getInstance());
- holder.mtext.setText(html);
- SpannableString ss = (SpannableString) holder.mtext.getText();
- for (int i = 0; i < spans.length; i++) {
- URLSpan span = (URLSpan) spans[i];
- int end = html.getSpanEnd(span);
- int start = html.getSpanStart(span);
- ss.setSpan(span, start, end, 0);
- }
- holder.mtext.setOnTouchListener(new OnTouchListener() {
- @Override
- public boolean onTouch(View v, MotionEvent event) {
- // TODO Auto-generated method stub
- boolean ret = false;
- CharSequence text = ((TextView) v).getText();
- Spannable stext = Spannable.Factory.getInstance().newSpannable(text);
- TextView widget = (TextView) v;
- int action = event.getAction();
- if (action == MotionEvent.ACTION_UP ||
- action == MotionEvent.ACTION_DOWN) {
- int x = (int) event.getX();
- int y = (int) event.getY();
- x -= widget.getTotalPaddingLeft();
- y -= widget.getTotalPaddingTop();
- x += widget.getScrollX();
- y += widget.getScrollY();
- Layout layout = widget.getLayout();
- int line = layout.getLineForVertical(y);
- int off = layout.getOffsetForHorizontal(line, x);
- ClickableSpan[] link = stext.getSpans(off, off, ClickableSpan.class);
- if (link.length != 0) {
- if (action == MotionEvent.ACTION_UP) {
- link[0].onClick(widget);
- }
- ret = true;
- }
- }
- return ret;
- }
- });
- return convertView;
- }
参考:ListView: TextView with LinkMovementMethod makes list item unclickable?
============ 欢迎各位老板打赏~ ===========
与本文相关的文章
- · 【Unity】SQLite发布到Android
- · unity3D导出模块给android使用
- · XPosed模块开发:XPosed入门与登陆劫持演示
- · 解决android 安卓中横屏、竖屏切换 应用崩溃问题
- · Android中LinearLayout的点击事件如何包含内部的子控件?
- · 关于Fragment 不响应onActivityResult的情况分析
- · Picasso-Android开源图片异步加载库
- · 如何用Fiddler对手机(安卓/iOS)数据进行抓包
- · 解决android webview无法上传的解决办法
- · PANIC: Could not open:my_avd.ini完美解决
- · 在 Android 模拟器中启用摄像头支持
- · Please use Android Tools > Fix Project Properties