0

我正在面對一個「ticker」(水平自動滾動文本)的奇怪問題。Android - 在內部片段發生變化時,主要活動(marquee_forever)中的滾動文本被初始化

我的應用程序使用片段。其中很多。它基於單個活動,包含一個操作欄,一個片段容器和一個底部滾動條。

股票滾動從左到右正確,但每次我改變片段,我的股票代碼被重新初始化(當前水平滾動丟失,並從頭再次開始,但沒有人告訴他這樣做!)。

我正在使用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),但我有同樣的結果。 任何想法?謝謝!

回答

0

我知道這不是一個答案,但您使用的是什麼版本的ABS?是3.5還是4?標籤文本在3.5中適合我,但我在版本4中遇到了嚴重的問題。

您試過setSelected(false)?它可能不會解決你的問題,但你的代碼看起來很好。

+0

謝謝你的回答(我正在使用ABS4),我已經解決了我的問題,而不使用xml marquee屬性,而是使用具有動畫代碼的自定義類代替。 – 2012-05-16 08:35:51