找到你要的答案

Q:multiple text views or one text view with clickable spans?

Q:多个文本或一个观点的文本视图点击的跨越?

I have an array of models each representing a word in a text. It looks like this:

ArrayList<Word> text;

class Word {
    public String name;
    public boolean isPunctuationMark;

I need to render that text inside an activity. What I also need to do is when a word is long-clicked I need to trigger and event and show a dialog with information specific to the selected word.

There are two approaches that I can use:

  1. use one TextView widget per word
  2. use one TextView to hold entire text but make each word a ClickableSpan

I've started implementing the second approach but there is no native support for longClick event on clickable spans, so I'd have to implement it myself. Also, it seems at the first glance that highlighting text and background of selected words is easier done with TextViews rather than with ClickableSpans. So now I'm thinking about trying out the first approach. Is there something I've missed when exploring these approaches? Maybe there's a third more architecturally correct option that is easier to implement supporting long-clicks?

我有一系列的模型,每一个都代表一个字在一个文本。看起来像这样:

ArrayList<Word> text;

class Word {
    public String name;
    public boolean isPunctuationMark;

我需要在活动中呈现文本。我还需要做的是,当一个单词被长时间点击,我需要触发和事件,并显示一个对话框与特定的信息选定的字。

有两种方法我可以使用:

  1. use one TextView widget per word
  2. use one TextView to hold entire text but make each word a ClickableSpan

我开始第二方法实施但有长按事件点击跨越不支持,所以我不得不自己实现它。而且,似乎乍一看,突出文本和背景更容易做选择的单词而不是clickablespans TextViews。所以现在我想尝试第一种方法。在探索这些方法的过程中,我错过了什么吗?也许有三分之一个建筑,是正确的选项,更容易实现,支持长点击?

answer1: 回答1:

I would suggest you use one TextView as this is a much more efficient UI. When there is a request for a ViewGroup to measure all of it's children, it is much faster to measure only one than a couple of thousand.

I have found a nice library you may try to use here. It uses getOffsetForPostion method added to TextView in API level 14.

public int getOffsetForPosition (float x, float y)

Added in API level 14 Get the character offset closest to the specified absolute position. A typical use case is to pass the result of getX() and getY() to this method.

Parameters x The horizontal absolute position of a point on screen y The vertical absolute position of a point on screen Returns the character offset for the character whose position is closest to the specified position. Returns -1 if there is no layout.

我建议你使用一个TextView为这是一个更有效的用户界面。当有一个ViewGroup测量它的所有孩子的要求,它是更快的测量只有一个比一个几千。

我找到了一个好的图书馆,你可以尝试在这里使用。它采用getoffsetforpostion方法添加到TextView在API级别14。

public int(x,y getoffsetforposition浮动)

Added in API level 14 Get the character offset closest to the specified absolute position. A typical use case is to pass the result of getX() and getY() to this method.

Parameters x The horizontal absolute position of a point on screen y The vertical absolute position of a point on screen Returns the character offset for the character whose position is closest to the specified position. Returns -1 if there is no layout.

android