当子TextView改变它的跨度时,ScrollView停止滚动

时间:2018-05-28 08:15:11

标签: android android-scrollview user-experience spannable

我有一个带有TextView的ScrollView作为它的子视图,它在MainActivity调用的Runnable中每秒更改它的范围:

MainActivity:

// MainActivity's private members
private ConstraintLayout m_mainLayout;
private UnderlineSpan m_underlineSpan; 
private SpannableStringBuilder m_spannableStringBuilder;
private Spannable m_spannableText;
private TextView m_textView;
private ScrollView m_scrollView;
private Runnable onUpdateTime = new Runnable()
{
    @Override
    public void run()
    {
        changeTextViewSpan();

        m_mainLayout.postDelayed(onUpdateTime, 1000);
    }
};

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    m_textView = new TextView(this);
    m_textView.setLayoutParams(
            new ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT));
    m_scrollView.addView(m_textView );
    m_spannableStringBuilder = new SpannableStringBuilder(Html.fromHtml(html)); // load long text from HTML file
    m_textView.setText(m_spannableStringBuilder, TextView.BufferType.SPANNABLE);
    m_spannableText = (Spannable) m_textView.getText();
}

private void changeTextViewSpan()
{
    m_spannableText.setSpan(
            m_underlineSpan,
            startIndex,
            endIndex,
            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}

MainActivity布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/mainLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <ScrollView
        android:id="@+id/largeTextScrollView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="8dp">
    </ScrollView>
<SeekBar
    android:id="@+id/playbackSeekBar"
    android:layout_width="251dp"
    android:layout_height="30dp"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginStart="8dp"
    app:layout_constraintBottom_toTopOf="@+id/playbackControlButton"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toEndOf="@+id/durationTextView" />

<ImageButton
    android:id="@+id/playbackControlButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="16dp"
    android:layout_marginLeft="8dp"
    android:layout_marginStart="8dp"
    android:onClick="onClick"
    android:src="@drawable/ic_play_arrow_black_42dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="parent" />

<ImageButton
    android:id="@+id/volumeImageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginTop="8dp"
    android:src="@drawable/ic_volume_up_black_24dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toStartOf="@+id/volumeBar"
    app:layout_constraintTop_toBottomOf="@+id/playbackSeekBar" />

<SeekBar
    android:id="@+id/volumeBar"
    android:layout_width="94dp"
    android:layout_height="0dp"
    android:layout_marginBottom="16dp"
    android:layout_marginEnd="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginTop="16dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/playbackSeekBar" />

<TextView
    android:id="@+id/currentPositionTextView"
    android:layout_width="40dp"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:text="0:00"
    app:layout_constraintBottom_toTopOf="@+id/playbackControlButton"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/transcriptScrollView" />

<TextView
    android:id="@+id/slashTextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginTop="8dp"
    android:text="/"
    app:layout_constraintBottom_toTopOf="@+id/playbackControlButton"
    app:layout_constraintStart_toEndOf="@+id/currentPositionTextView"
    app:layout_constraintTop_toBottomOf="@+id/transcriptScrollView" />

<TextView
    android:id="@+id/durationTextView"
    android:layout_width="40dp"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginTop="8dp"
    android:text="0:00"
    app:layout_constraintBottom_toTopOf="@+id/playbackControlButton"
    app:layout_constraintStart_toEndOf="@+id/slashTextView"
    app:layout_constraintTop_toBottomOf="@+id/transcriptScrollView" />

<ImageButton
    android:id="@+id/trackImageButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginTop="8dp"
    android:onClick="onClick"
    android:src="@drawable/ic_visibility_off_black_24dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toStartOf="@+id/volumeImageView"
    app:layout_constraintTop_toBottomOf="@+id/playbackSeekBar" />
</android.support.constraint.ConstraintLayout>

现在,每当我更改TextView的跨度时,ScrollView都会停止滚动,并且在更改span后,ScrollView会正常工作。

我想要实现的是更改从HTML文件加载的长文本的UnderlineSpan位置。文本的大小不会改变,但它足够长,可以将其放入ScrollView。

有谁知道如何让它顺利滚动?

谢谢!

1 个答案:

答案 0 :(得分:0)

我不确定你想要达到什么目标 但是如果您想要平滑滚动,则必须将textview的高度设置为固定大小 然后,您可以更改textview内容,而不会每秒更改其大小 这样滚动必须平滑。