【发布时间】:2018-05-29 16:52:19
【问题描述】:
我有一个带有 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 的 span 时,ScrollView 都会停止滚动,并且更改 span 后,ScrollView 可以正常工作。
我想要实现的是更改从 HTML 文件加载的长文本的 UnderlineSpan 位置。文字大小没有变化,但足够长,可以放入ScrollView。
有谁知道如何让它平滑滚动?
谢谢!
【问题讨论】:
-
你能分享错误详情吗...
-
@NagarjunaReddy 没有错误。 ScrollView 仅在 span 发生变化时才停止滚动,因此滚动不流畅。它每秒都会滞后,给用户带来不愉快的体验。
-
@arek 使用 NestedScrollView
-
@ShivamKumar 我已将支持库中的 ScrollView 更改为 NestedScrollView(我的 minSdk 为 15),但不幸的是我的滚动仍然不流畅。
-
@arek 你能添加你的xml代码吗
标签: android android-scrollview user-experience spannable