Java runOnUiThread防止';从marquee的文本视图?
假设我有一个Java runOnUiThread防止';从marquee的文本视图?,java,android,android-fragments,Java,Android,Android Fragments,假设我有一个片段,带有文本视图(我们称之为TextView a),其中包含一个字符串,还有另一个文本视图(TextView B),它是一个计时器,由这样的处理程序更新…mHandler.postDelayed(这个,100) 问题是,每当TextViewB的处理程序每秒更新一次时,就会阻止TextViewA运行字幕动画 下面是我用来更新TextViewB的代码 public void updateTextViewB() { Thread thread = new Thread() {
片段
,带有文本视图
(我们称之为TextView a),其中包含一个字符串
,还有另一个文本视图
(TextView B),它是一个计时器,由这样的处理程序更新…mHandler.postDelayed(这个,100)代码>
问题是,每当TextView
B的处理程序每秒更新一次时,就会阻止TextView
A运行字幕动画
下面是我用来更新TextView
B的代码
public void updateTextViewB()
{
Thread thread = new Thread() {
@Override
public void run() {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
// Code that set's the constantly updating time
mHandler.postDelayed(this, 100);
}
});
}
};
thread.start();
}
有什么办法可以解决这个问题?我是否应该将焦点设置为TextView
A?我遇到了同样的问题,刚刚解决了它。如果TextView A和B位于同一布局中,则它们是同级,每次更新任何同级(包括A本身)的焦点时,字幕动画将重新启动。每次更新B时,它都会请求焦点,以便字幕动画重新启动。解决方案你所需要做的就是把一个放在它自己的布局中,这样它就没有同级,只有一个父级。布局如下:
<LinearLayout
android:id="@+id/mainLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/text_b"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="text being updated each second"
/>
<LinearLayout
android:id="@+id/marquee_text_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/text_b"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="true"
android:focusableInTouchMode="true"
android:marqueeRepeatLimit="marquee_forever"
android:singleLine="true"
android:text="Long text with marquee animation that keeps running on the screen :P :)"
/>
</LinearLayout>
</LinearLayout>
处理程序是在后台线程上运行还是在ui线程上运行?我假设是ui线程。最好创建一个自定义的textview
而不是嵌套的run()
如何执行?它会在ui上更新而不创建新线程吗?这段代码非常可疑。为什么要创建线程
,然后将线程
发布到处理程序
?请包括你遗漏的代码。这可能会变得更糟。我知道这篇文章很旧,但是,如果没有两个TextView的兄弟姐妹,我实际上遇到了问题。。。A的外祖父母和B的外祖父母的布局其实是一样的。。。有什么想法吗?