RabbitMQ サーバーでトピックをリッスンし、テキストビューにメッセージを表示するテスト Android アプリを作成しました。
「出力」テキストビューに追加されるすべての新しいメッセージで、新しいテキストが古いテキストの上に描画されることを除いて、すべて正常に機能します。2 番目の画像に見られるように、スクロールすると同じことが起こります。アプリがバックグラウンドに移行して再びアクティブになると、テキストは再び通常どおり表示されます (図 1 を参照)。
私のレイアウトはとてもシンプルです。何が間違っているのでしょうか?
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="60dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Connection status" />
<TextView
android:id="@+id/status"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:saveEnabled="false"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Received messages:" />
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/output"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:saveEnabled="false"
android:textAppearance="?android:attr/textAppearanceSmall" />
</ScrollView>
入ってくる新しいメッセージを処理する場所は次のとおりです。
final Handler myViewUpdateHandler = new Handler() {
// @Override
public void handleMessage(Message msg) {
switch (msg.what) {
case NEWSTATUS:
status.setText((String) msg.obj);
status.invalidate();
//also add to output
case NEWMESSAGE:
String oldText= (String) output.getText();
output.setText("");
String newText = DateUtil.getClockTimeMillis()+" "+(String) msg.obj+"\n"+oldText;
output.setText(newText);
output.invalidate();
break;
default:
throw new IllegalArgumentException("no case for:" + msg.arg1);
}
super.handleMessage(msg);
}
};