Android - ListView中的可点击链接文本视图无法接收OnItemClick事件

20
我有一个 ListView,每行都包含一个 TextView 和一些其他视图。TextView 中渲染了可能含有链接的HTML内容。
以下代码出现在列表适配器中。 m_textview.setMovementMethod(LinkMovementMethod.getInstance()); m_textview.setText(Html.fromHtml(myhtmlcontent));
这会导致ListView不再接收点击事件。我决定将列表点击代码放在适配器返回的视图上。但这并不完全符合要求。现在我可以在行的任何位置点击以启动另一个活动,除了TextView之外。我希望用户能够点击TextView的非链接部分并启动另一个活动。
如果我将onclick移动到TextView而不是其父视图,则可以工作,但现在点击链接会触发两个事件-一个是链接的单击事件,另一个是TextView的(这是不希望的)。
我注意到Android上的Google+和Peep的工作方式符合我的要求。我不确定如何实现这一点。
5个回答

23

实际上这是一个 BUG。为了解决这个问题,您可以在ListView的行布局xml中添加android:descendantFocusability="blocksDescendants"。例如:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:descendantFocusability="blocksDescendants"
android:orientation="vertical" >

 <TextView
    android:id="@+id/lblStatusMessage"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:autoLink="web"
    android:focusable="false"
    android:textSize="15sp" />
</LinearLayout>

来源: 这里这里

祝好运 :)


可能已经太晚了...但我遇到了相同的问题,发现这篇未回答的帖子。 :) - Vishal Vyas
我无法在我的项目父项上使用长按监听器和TextView自动链接来完成这个操作。 - Cyph3rCod3r

1

在 ListView 项中的可聚焦视图将禁用选择 ListView 项的能力。将 android:focusable="false" 应用于 TextView 将允许 OnItemClick 再次工作。您还可能需要应用 android:focusableInTouchMode="false",以使轨迹球忽略链接,因为在 ListView 中单击可聚焦元素上的轨迹球既可以单击链接也可以单击 ListView 项。


0

只响应链接触摸事件的TextView。基于https://dev59.com/PFrUa4cB1Zd3GeqPm8Jr#7327332

        public class AutoLinkTextView extends TextView {

            public AutoLinkTextView(Context context, AttributeSet attrs, int defStyle) {
                super(context, attrs, defStyle);
                init();
            }

            public AutoLinkTextView(Context context, AttributeSet attrs) {
                super(context, attrs);
                init();
            }

            public AutoLinkTextView(Context context) {
                super(context);
                init();
            }

            private void init() {
                this.setAutoLinkMask(Linkify.ALL);
            }

            /**
             * @Linkify applies to a movementMethod to the textView @LinkMovementMethod.
             *          That movement method thought it implements a scrolling
             *          vertically method it overrides any other scrolling method the
             *          parent has.
             * 
             *          Although touchEvent can be dispached to the parent, the specific
             *          parent ScrollView needed the whole sequence ACTION_DOWN ,
             *          ACTION_MOVE, ACTION_UP to perform (sweep detection). So the
             *          solution to this problem is after applying @Linkify we need to
             *          remove the textView's scrolling method and handle the @LinkMovementMethod
             *          link detection action in onTouchEvent of the textView.
             */
            @Override
            public boolean onTouchEvent(MotionEvent event) {
                final TextView widget = (TextView) this;
                final Object text = widget.getText();
                if (text instanceof Spannable) {
                    final Spannable buffer = (Spannable) text;
                    final 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();

                        final Layout layout = widget.getLayout();
                        final int line = layout.getLineForVertical(y);
                        final int off = layout.getOffsetForHorizontal(line, x);

                        final ClickableSpan[] link = buffer.getSpans(off, off,
                                ClickableSpan.class);

                        if (link.length != 0) {
                            if (action == MotionEvent.ACTION_UP) {
                                link[0].onClick(widget);
                            } else if (action == MotionEvent.ACTION_DOWN) {
                                Selection.setSelection(buffer,
                                        buffer.getSpanStart(link[0]),
                                        buffer.getSpanEnd(link[0]));
                            }
                            return true;
                        }
                    }

                }
                return false;
            }

            @Override
            public void setText(CharSequence text, BufferType type) {
                super.setText(text, type);
                this.setMovementMethod(null);
            }
        }

0
你可以在列表视图上附加setOnItemClickListener。

0

我曾经遇到同样的问题,但是这些答案都没能解决我的问题。最终,我通过删除属性android:inputType="textMultiLine"来解决了这个问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接