android - 使用 Spannable 和 LinkMovementMethod 选择 TextView

标签 android android-listview textview linkify spannable

到目前为止,我所做的是具有普通文本和可点击跨度的 TextView 的 ListView : Item untouched

单击我正在打开 URL 的跨度,单击 textView 周围的项目 View 会导致 listView OnItemClickListener 导航到项目详细信息,这很好: Item touched outside the textView

现在的问题是:

Item textView normal text touched Item textView span touched 触摸 textView 会使普通文本有点突出显示(当项目被完全选中时具有相同的颜色),textView 的 OnTouchListener 触摸事件触发但不是 OnFocusChangeListener 事件和项目的 View 没有获得选择样式。为 listView、item View、textView focusable 尝试了 FOCUS_BLOCK_DESCENDANTS 的所有变体,启用或禁用的结果相同。

幸运的是,textView OnClickListener 事件以这种方式触发,但这太丑陋了:文本不可见,而触摸未释放,因为所选文本颜色与项目颜色相同,没有其他指示用户将转到项目详细信息而不是消失的丑陋文本。

我怀疑发生这种情况是因为 textView 的内容是 Spannable,而不是 CliclableSpan 的部分以这种奇怪的方式表现。

一旦触摸普通文本,我是否有机会选择该项目?


listView 项目布局:

<?xml version="1.0" encoding="utf-8"?>
<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="horizontal" >

    <ImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:layout_marginBottom="5dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="5dp"
        android:focusable="false" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/title"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:layout_marginTop="5dp"
            android:focusable="false"
            android:text=""
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/info"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="4dp"
            android:layout_marginLeft="4dp"
            android:layout_marginRight="4dp"
            android:focusable="false"
            android:text=""
            android:textAppearance="?android:attr/textAppearanceSmall" />

        <TextView
            android:id="@+id/details"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="4dp"
            android:layout_marginLeft="4dp"
            android:layout_marginRight="4dp"
            android:focusable="false"
            android:gravity="fill_horizontal|right"
            android:text=""
            android:textAppearance="?android:attr/textAppearanceMedium"/>

    </LinearLayout>

</LinearLayout>

使用 TextView setClickable(false) 我可以禁用这种奇怪的选择样式,在触摸 TextView 区域时不会发生任何事情,虽然不好但可能对解决方案有用。

还尝试向每个项目添加不可聚焦和不可点击的按钮,当它被触摸时,完整的项目被选中,当触摸被释放时,项目的点击事件被传递,这正是我对带有 Spannable 内容的 textView 的期望。

最佳答案

您是否尝试将 TextView 的背景设置为 Android 的默认 list_selector_background

    textView.setMovementMethod(LinkMovementMethod.getInstance());
    textView.setBackgroundResource(android.R.drawable.list_selector_background);

对我来说,它似乎给出了你想要的结果。

更新:在看到该项目不仅仅是一个 TextView 之后

嗯,这不是一个完美的解决方案(因为以某种方式修复 TextView - ListView 突出显示交互可能更好),但它工作得很好。

我想通了,而不是在 TextView 上设置移动方法(触发问题),检查 ListView 的 onItemClick() 更简单(在确定点击后)看看我们是否应该在我们的 ClickableSpans 上启动 onClick():

public class MyActivity extends Activity {

    private final Rect mLastTouch = new Rect();

    private boolean spanClicked(ListView list, View view, int textViewId) {
        final TextView widget = (TextView) view.findViewById(textViewId);
        list.offsetRectIntoDescendantCoords(widget, mLastTouch);
        int x = mLastTouch.right;
        int y = mLastTouch.bottom;

        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 Editable buffer = widget.getEditableText();
        final ClickableSpan[] link = buffer.getSpans(off, off, ClickableSpan.class);

        if (link.length == 0) return false;

        link[0].onClick(widget);
        return true;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // ...

        listView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (spanClicked(listView, view, R.id.details)) return;

                // no span is clicked, normal onItemClick handling code here ..                    
            }
        });
        listView.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    mLastTouch.right = (int) event.getX();
                    mLastTouch.bottom = (int) event.getY();
                }
                return false;
            }
        });

        // ...
    }

}

spanClicked() 方法基本上是框架中 LinkMovementMethod 的 onTouchEvent() 方法的缩写版本。要捕获最后一个 MotionEvent 坐标(以检查点击事件),我们只需在我们的 ListView 上添加一个 OnTouchListener。

关于android - 使用 Spannable 和 LinkMovementMethod 选择 TextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11514554/

相关文章:

android - 获取表格行内动态 TextView 的宽度

android - 如何从源代码中隐藏 AdMob 应用程序 ID

java - actionbarshearlock Activity 中的透明操作栏

Android htmlText 上的换行符

android - 仅显示来自 json 调用的某些数据

android - textview的动态布局 - android

java - 如何将事件从 MainActivity.java 发送到 React Native 中的 Javascript?

java - 如何将 Assets 文件夹中的本地 JSON 文件解析为 ListView?

android - 将自定义行动态添加到 ListView

android - 如何同时使抽屉导航选框 ListView 中的所有 TextView ?