0

最初のアプリは「Header1Content1」のようになります。ヘッダー2コンテンツ2; ヘッダー3コンテンツ3'。「グーグルマップに行く」ボタンを押すと、このアプリが開いて「北朝鮮」を検索しました。戻ってきたとき、アプリは「ヘッダー1コンテンツ3」になりました。ヘッダー2コンテンツ3; ヘッダー3コンテンツ3'(すべてのコンテンツは3です)!なんで?少なくともAndroid4.0.4(Huawei Honorの公式)で発生します。

ご覧のとおり、LinearLayoutを含むScrollViewを使用して、item.xmlで説明されているビューを追加します

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.example.HelloWorld"
          android:versionCode="1"
          android:versionName="1.0">
    <uses-sdk android:minSdkVersion="10"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <application android:label="@string/app_name" android:icon="@drawable/ic_launcher">
        <activity android:name="MyActivity" android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>
</manifest>

MyActivity.java

package com.example.HelloWorld;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MyActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        addBlock("Header1", "Content1");
        addBlock("Header2", "Content2");
        addBlock("Header3", "Content3");
    }

    public void openMaps(View view) {
        String uri = "geo:0,0?q=North+Korea";
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
        startActivity(intent);
    }

    private void addBlock(String header, String content) {
        final LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linearLayout);
        final LayoutInflater factory = LayoutInflater.from(this);
        final View view = factory.inflate(R.layout.item, null);
        final TextView headerView = (TextView) view.findViewById(R.id.header);
        final TextView contentView = (TextView) view.findViewById(R.id.content);
        if (header.isEmpty()) {
            headerView.setHeight(0);
        }
        headerView.setText(header);
        contentView.setText(content);
        linearLayout.addView(view);
    }
}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
        >
    <Button
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Go to Google Maps"
            android:id="@+id/button" android:onClick="openMaps"/>
    <ScrollView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/scrollView">
        <LinearLayout
                android:orientation="vertical"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" android:longClickable="false" android:id="@+id/linearLayout">
        </LinearLayout>
    </ScrollView>
</LinearLayout>

item.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Header"
            android:id="@+id/header" android:layout_gravity="left|center_vertical" android:textStyle="bold"
            android:layout_marginLeft="5dp"/>
    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Content"
            android:id="@+id/content" android:layout_gravity="left|center_vertical" android:layout_marginLeft="10dp"
            android:textIsSelectable="true"/>
</LinearLayout>
4

1 に答える 1

0

あなたがコードにリストしたもので言うのは難しいです。ある種のアクティビティは、アクティビティのコールバックを確認して、アクティビティの状態がどのように復元されているか、およびGoogleマップに移動すると言ったときにどこに移動するかを確認する必要があります。ScrollView内で独自のListViewを操作しているときの経験から、オブジェクトの状態を保持するためにParcelableデータオブジェクトを作成すること、およびアクティビティが再作成されるとき、または(前面に戻される)ときに、多くの場合、良い習慣があります。ビューの状態を、具体的に希望する状態に戻します。これは、フラグメントではなくアクティビティを使用している場合は、onCreate()およびonSavedInstanceState()コールバックを使用して実行できます。あなたがそこにもっと多くの情報を載せれば、私はもっと助けになることができます。

于 2013-03-23T14:44:48.580 に答える