2013-03-22 29 views
0

開始時,應用程序看起來像'Header 1 Content 1;標題2內容2;標題3內容3'。 當你按下「轉到谷歌地圖」按鈕,這個程序打開並搜索「朝鮮」。當您返回時,應用程序變爲「標題1內容3」;標題2內容3;標題3內容3'(全部內容爲3)!爲什麼?至少在Android 4.0.4上發佈(華爲榮譽官方)。如您所見,我在其中使用帶有LinearLayout的ScrollView,並在其中添加item.xml中描述的視圖。Android:在Google地圖中搜索後發生的行爲奇怪

的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> 

回答

0

很難與你在代碼中列出說。一種需要查看「活動」回調的功能,以查看「活動」的狀態如何恢復,以及當您說要轉到Google地圖時該活動的狀態。我從經驗中瞭解到,當您在ScrollView中使用自己的ListView時,創建一個Parcelable數據對象來保存對象的狀態並且重新創建Activity或(將其帶回前面)將視圖的狀態恢復爲您想要的特定狀態。如果您使用的是Activities而不是Fragments,則可以使用onCreate()和onSavedInstanceState()回調完成此操作。如果你在那裏提供更多的信息,我可以得到更多的幫助。

+0

我沒有更多有影響力的代碼在這個項目中。是的,我使用活動。 – Vladiator 2013-03-23 22:41:53