简体   繁体   English

Android TextView 水平滚动延迟

[英]Android TextView horizontal scroll delay

I am trying to implement a ListView which contains a TextView that scrolls automatically when the text gets too long.我正在尝试实现一个 ListView,它包含一个 TextView,当文本太长时会自动滚动。 So far so good but my problem is that the scrolling starts immediatelly after the item is shown and thus the user has no chance to read the first few letters.到目前为止一切顺利,但我的问题是在显示项目后立即开始滚动,因此用户没有机会阅读前几个字母。 I was wondering if there is any kind of a delay for that scrolling to start?我想知道滚动开始是否有任何延迟?

Here's my code:这是我的代码:

<TextView
        android:id="@+id/item_track"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:marqueeRepeatLimit ="marquee_forever"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:scrollHorizontally="true"
        android:text="Username"
        android:textColor="#fff"
        android:textSize="17sp"
        app:fontFamily="sans-serif"
        />

I had this exact same question.我有这个完全相同的问题。

I solved this by using a handler that runs on the UI thread:我通过使用在 UI 线程上运行的处理程序解决了这个问题:

Handler handler = new Handler(Looper.getMainLooper());
handler.postDelayed(() -> textView.setSelected(true), 1000);    

Handler's postDelayed() lets you fire a runnable after a certain amount of time elapses.处理程序的 postDelayed() 允许您在经过一定时间后触发可运行。 In my example the text view will sit for 1 second (1000 milliseconds) and then the scrolling animation starts when the handler executes.在我的示例中,文本视图将停留 1 秒(1000 毫秒),然后在处理程序执行时开始滚动动画。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM