11

到目前为止,我所做的是具有普通文本和可点击跨度的文本视图的列表视图: 物品原封不动

单击我打开 URL 的跨度,单击 textView 周围的项目 View 会导致 listViewOnItemClickListener导航到项目详细信息,这很好: 在 textView 之外触摸的项目

现在的问题是:

项目 textView 正常触摸的文本 项目 textView 跨度已触及 触摸 textView 会使普通文本有点突出显示(与完全选择项目时的颜色相同), textView 的OnTouchListener触摸事件会触发但不会触发OnFocusChangeListener事件,并且项目的 View 不会获得选择样式。尝试了FOCUS_BLOCK_DESCENDANTSlistView、item View 的所有变体,focusable启用或禁用了 textView,结果相同。

幸运的是,textViewOnClickListener事件以这种方式触发,但这太难看了:文本是不可见的,而触摸没有释放,因为选定的文本颜色与项目颜色相同,没有其他迹象表明用户将转到项目详细信息其他比那个丑陋的文字消失。

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

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


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>

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

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

4

4 回答 4

4

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

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

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

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

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

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

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

于 2012-07-21T20:19:22.937 回答
1

使用TextViewsetHighlightColor(Color.TRANSPARENT);

于 2012-09-18T05:42:08.497 回答
0

我认为解决这个问题的最好方法是使用选择器。您可以更改选择背景颜色,以便他们看到相同的颜色

示例选择器代码:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
       android:color="#000000" /> <!-- pressed -->
    <item android:state_focused="true"
       android:color="#000000" /> <!-- focused -->
    <item android:color="#FFFFFF" /> <!-- default -->
</selector>

对你的链接实施这样的事情。

让我知道结果

于 2012-07-19T12:21:27.733 回答
0

引用文档setMovementMethod()TextView

设置用于此 TextView 的移动方法(箭头键处理程序)。这可以为 null 以禁止使用箭头键移动光标或滚动视图。

请注意,如果您希望具有关键侦听器或移动方法的 TextView 不可聚焦,或者如果您希望没有关键侦听器或移动方法的 TextView 可聚焦,则必须调用 android.view.View.setFocusable(boolean)在调用它以使焦点恢复到你想要的方式之后再次调用它。

第二段解释说,我们应该setFocusable在设置 LinkMovementMethod 后显式调用该方法,以使可聚焦性按照我们想要的方式工作。

膨胀列表项后,您需要在代码中执行此操作。获得焦点后,您可以将选择器设置为具有不同颜色的文本。

这是setMovementMethod的源代码。

于 2012-07-20T11:50:52.693 回答