Android - 当内部片段更改时,主活动中的滚动文本(marquee_forever)会被初始化

时间:2012-03-28 09:37:40

标签: android fragment horizontal-scrolling actionbarsherlock ticker

我正面临着一个奇怪的问题,即“自动滚动”(水平自动滚动文字)。

我的应用程序使用片段。其中很多。它基于单个活动,带有操作栏,碎片容器和底部滚动条。

Ticker正确滚动,从左到右,但每次我更改片段时,我的自动收报机再次初始化(当前水平滚动丢失,它从头开始,但没有人告诉他这样做! )。

我正在使用actionbarsherlock(就像一个魅力!谢谢杰克沃顿!)兼容模式。

以下是一些代码:

主要活动布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/mainRelative" >
<LinearLayout
    android:id="@+id/mainFragmentContainer"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:layout_alignParentTop="true"
    android:layout_above="@+id/tickerView1" >
</LinearLayout>
<my.app.views.TickerView
    android:id="@+id/tickerView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true">
     </my.app.views.TickerView>

</RelativeLayout>

TICKERVIEW CLASS

public class TickerView extends TextView {
private Context context;

public TickerView(Context context) {
    super(context);
    initialize();
    // TODO Auto-generated constructor stub
}

public TickerView(Context context, AttributeSet attrs) {
    super(context, attrs);
    initialize();
    // TODO Auto-generated constructor stub
}

public TickerView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    initialize();
    // TODO Auto-generated constructor stub
}

public void initialize() {
    context = getContext();
    String s;
    setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
    setMarqueeRepeatLimit(-1);
    setFocusable(true);
    setFocusableInTouchMode(true);
    setHorizontallyScrolling(true);
    setSingleLine();
    setEllipsize(TruncateAt.MARQUEE);
    setSelected(true);
            setText("sdghaskjghaskgjashgkasjghaksjhgaksjghakjshgkajsghaksjghaksjgh");

}
}


@Override
protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
    Log.d("DEBUG", "ON FOCUS CHANGED");
    if (focused)
        super.onFocusChanged(focused, direction, previouslyFocusedRect);
}

@Override
public void onWindowFocusChanged(boolean focused) {
    Log.d("DEBUG", "ON WINDOW FOCUS CHANGED" + (focused ? "FOCUSED" : "NOT FOCUSED"));
    if (focused)
        super.onWindowFocusChanged(focused);
}

@Override
public boolean isFocused() {
    return true;
}

我也尝试过基于xml的解决方案(设置一个布局文件,其中包含正确的属性,用于滚动文本和从LinearLayout扩展我的小部件),但我有相同的结果。 有任何想法吗?谢谢!

1 个答案:

答案 0 :(得分:0)

我知道这不是答案,但您使用的是什么版本的ABS?是3.5还是4?标签文本在3.5中对我滚动不错,但我在版本4中遇到了严重的问题。

你试过setSelected(false)吗?它可能无法解决您的问题,但您的代码看起来很好。