A-L*_*ive 11 android textview linkify android-listview spannable
到目前为止我所做的是具有普通文本和可点击跨度的文本视图的列表视图:
单击我打开URL的跨度,单击textView周围的项目查看导致listView OnItemClickListener
导航到项目详细信息,这很好:
现在的问题是:
触摸textView使普通文本有点突出显示(完全选择项目时具有相同的颜色),textView的
OnTouchListener
触摸事件触发但不触发OnFocusChangeListener
事件,并且项目的视图不会获得选择样式.尝试FOCUS_BLOCK_DESCENDANTS
了listView的所有变体,项目视图,focusable
启用或禁用textView 具有相同的结果.
幸运的是,textView OnClickListener
事件以这种方式触发,但这太丑了:由于所选文本颜色与项目颜色相同,因此文本是不可见的,而触摸未被释放,没有其他迹象表明用户将转到其他项目详细信息比那丑陋的文字消失了.
我怀疑发生这种情况是因为Spannable
textView 的内容是,并且不是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>
Run Code Online (Sandbox Code Playgroud)
使用文本视图setClickable(false)我能够以触摸文本视图区域时没有任何反应的方式禁用这种奇怪的选择样式,不好但可能对解决方案有用.
还试图为每个项目添加不可聚焦和不可点击的按钮,当它被触摸时,选择了完整的项目,当触摸被释放时,项目的点击事件被传递,这正是我对具有Spannable内容的textView所期望的.
您是否尝试将 TextView 的背景设置为 Android 的默认背景list_selector_background
?
textView.setMovementMethod(LinkMovementMethod.getInstance());
textView.setBackgroundResource(android.R.drawable.list_selector_background);
Run Code Online (Sandbox Code Playgroud)
对我来说,它似乎给出了你想要的结果。
更新:看到该项目不仅仅是一个 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;
}
});
// ...
}
}
Run Code Online (Sandbox Code Playgroud)
该方法基本上是框架中spanClicked()
LinkMovementMethod 方法的缩写版本。onTouchEvent()
要捕获最后一个 MotionEvent 坐标(以检查单击事件),我们只需在 ListView 上添加一个 OnTouchListener 即可。