Android:水平滚动,可跨越的文本不能一起使用

时间:2013-12-20 20:52:20

标签: android

Hello stackoverflow社区!

我有一点问题,在Google搜索了好几天后,我想寻求你的帮助。

我想要达到的目标是在Android中使用滚动文本,其中包含可点击的部分。到目前为止一直很好,滚动部分正在工作并且点击,但遗憾的是当我单击整个文本的跨度时,只调用链接到第一个跨度的onClick侦听器,而不是链接到跨度的那个点击了。

这是我到目前为止的代码(因为Xamarin的C#代码):

SpannableStringBuilder scrollingTextBuilder = new SpannableStringBuilder();
foreach(DataStruct detail in dbExchange.DataList)
{
    SpannableStringBuilder singleTextBuilder = new SpannableStringBuilder(detail.timeSlot + ": " + detail.videoName);
    singleTextBuilder.SetSpan(new SpanClickHelper(detail.videoID), 0, singleTextBuilder.Length(), SpanTypes.ExclusiveExclusive);
    scrollingTextBuilder.Append(singleTextBuilder);
}

TextView scrollingTextView = (TextView) FindViewById(Resource.Id.scrollingTextView);

scrollingTextView.MovementMethod = LinkMovementMethod.Instance;
scrollingTextView.SetText(scrollingTextBuilder, TextView.BufferType.Spannable);
scrollingTextView.StartAnimation((Animation)AnimationUtils.LoadAnimation(this, Resource.Animation.scrollAnim));

这里是SpanClickHelper类:

public class SpanClickHelper : ClickableSpan
{
    private string videoID;

    public SpanClickHelper(string videoID)
    {
        this.videoID = videoID;
    }

    #region implemented abstract members of ClickableSpan

    public override void OnClick(global::Android.Views.View widget)
    {
        Console.WriteLine("clicked: " + videoID);
    }

    #endregion
}

我希望你知道一两个提示让我走上正轨。希望我不会错误地旅行。

1 个答案:

答案 0 :(得分:0)

试试这个(把它放在onCreate()方法中)

ScrollView sv = new ScrollView(this);
TextView tv = new TextView(this);
tv.setTextSize(32);
tv.setMovementMethod(LinkMovementMethod.getInstance());
SpannableStringBuilder b = new SpannableStringBuilder();
for (int i = 0; i < 5; i++) {
    String link = "#" + i;
    SpannableStringBuilder tmp = new SpannableStringBuilder();
    tmp.append("link " + link);
    ClickableSpan span = new CS(link);
    tmp.setSpan(span, 0, tmp.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    tmp.append("\n\n\n");
    b.append(tmp);
}
tv.setText(b);
sv.addView(tv);

setContentView(sv);

Printer printer = new LogPrinter(Log.DEBUG, TAG);
TextUtils.dumpSpans(b, printer, "spans: ");

类CS看起来像:

class CS extends ClickableSpan {
    private String s;

    public CS(String s) {
        this.s = s;
    }

    @Override
    public void onClick(View widget) {
        Log.d(TAG, "onClick " + s);
    }
}

for flags ==我得到的setSpan()中的Spanned.SPAN_EXCLUSIVE_EXCLUSIVE(注意fl = ...之前的范围):

D/Main    (  386): spans: link #0: 43dccd60 org.pskink.pathdrawable.Main.CS (0-7) fl=#33
D/Main    (  386): spans: link #1: 43dcd138 org.pskink.pathdrawable.Main.CS (10-17) fl=#33
D/Main    (  386): spans: link #2: 43dcd468 org.pskink.pathdrawable.Main.CS (20-27) fl=#33
D/Main    (  386): spans: link #3: 43dcd780 org.pskink.pathdrawable.Main.CS (30-37) fl=#33
D/Main    (  386): spans: link #4: 43dcda10 org.pskink.pathdrawable.Main.CS (40-47) fl=#33

for flags == Spanned.SPAN_INCLUSIVE_INCLUSIVE(由于所有跨度,我删除了大量输出 延伸到缓冲区的末尾,见下面的* -50)你可以看到链接#0的范围是0-50和 此跨度拦截所有点击:

D/Main    (  470): spans: link #0 [cut] : 43dccd60 org.pskink.pathdrawable.Main.CS (0-50) fl=#18
D/Main    (  470): spans: link #1 [cut] : 43dcd138 org.pskink.pathdrawable.Main.CS (10-50) fl=#18
D/Main    (  470): spans: link #2 [cut] : 43dcd468 org.pskink.pathdrawable.Main.CS (20-50) fl=#18
D/Main    (  470): spans: link #3 [cut] : 43dcd780 org.pskink.pathdrawable.Main.CS (30-50) fl=#18
D/Main    (  470): spans: link #4 [cut] : 43dcda10 org.pskink.pathdrawable.Main.CS (40-50) fl=#18