2011年2月5日土曜日

Android 複数画面 1 Activity で画面遷移

1画面 1Activity は設計も実装も楽なんですけど、なんせ Activity 間の遷移は時間がかかります。一瞬待たされるよね? setContentView() を使って画面遷移を実現すると、待たされることはなくなります。

では、コード。

R.layout.main -> R.layout.first -> R.layout.second -> finish




という画面遷移になっています。

  1. package yanzm.products.screentransition.lib;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Context;  
  5. import android.content.SharedPreferences;  
  6. import android.os.Bundle;  
  7. import android.view.View;  
  8. import android.view.View.OnClickListener;  
  9. import android.widget.Button;  
  10.   
  11. public class MainActivity extends Activity {  
  12.   
  13.   static final private String prefName = "MY_PREF";  
  14.   private int mScreenId = R.layout.main;  
  15.   
  16.   @Override  
  17.   public void onCreate(Bundle savedInstanceState) {  
  18.     super.onCreate(savedInstanceState);  
  19.   
  20.     SharedPreferences prefs = getSharedPreferences(prefName, Context.MODE_PRIVATE);  
  21.     int screenId = prefs.getInt("screenId", R.layout.main);  
  22.   
  23.     setScreenContent(screenId);  
  24.   }  
  25.   
  26.   @Override  
  27.   public void onDestroy() {  
  28.     super.onDestroy();  
  29.     SharedPreferences prefs = getSharedPreferences(prefName, Context.MODE_PRIVATE);  
  30.     SharedPreferences.Editor editor = prefs.edit();  
  31.     editor.putInt("screenId", mScreenId);  
  32.     editor.commit();  
  33.   }  
  34.   
  35.   private void setScreenContent(int screenId) {  
  36.     mScreenId = screenId;  
  37.     setContentView(screenId);  
  38.     
  39.     switch (screenId) {  
  40.     case R.layout.first: {  
  41.       setFirstScreenContent();  
  42.       break;  
  43.     }  
  44.     case R.layout.second: {  
  45.       setSecondScreenContent();  
  46.       break;  
  47.     }  
  48.     case R.layout.main: {  
  49.       setMainScreenContent();  
  50.       break;  
  51.     }  
  52.     }  
  53.   }  
  54.   
  55.   private void setMainScreenContent() {  
  56.     Button backButton = (Button) findViewById(R.id.back);  
  57.     backButton.setVisibility(View.INVISIBLE);  
  58.   
  59.     Button nextButton = (Button) findViewById(R.id.next);  
  60.     nextButton.setOnClickListener(new OnClickListener() {  
  61.       public void onClick(View v) {  
  62.         setScreenContent(R.layout.first);  
  63.       }  
  64.     });  
  65.   }  
  66.   
  67.   private void setFirstScreenContent() {  
  68.     Button backButton = (Button) findViewById(R.id.back);  
  69.     backButton.setOnClickListener(new OnClickListener() {  
  70.       public void onClick(View v) {  
  71.         setScreenContent(R.layout.main);  
  72.       }  
  73.     });  
  74.   
  75.     Button nextButton = (Button) findViewById(R.id.next);  
  76.     nextButton.setOnClickListener(new OnClickListener() {  
  77.       public void onClick(View v) {  
  78.         setScreenContent(R.layout.second);  
  79.       }  
  80.     });  
  81.   }  
  82.   
  83.   private void setSecondScreenContent() {  
  84.     Button backButton = (Button) findViewById(R.id.back);  
  85.     backButton.setOnClickListener(new OnClickListener() {  
  86.       public void onClick(View v) {  
  87.         setScreenContent(R.layout.first);  
  88.       }  
  89.     });  
  90.   
  91.     Button nextButton = (Button) findViewById(R.id.next);  
  92.     nextButton.setOnClickListener(new OnClickListener() {  
  93.       public void onClick(View v) {  
  94.         finish();  
  95.       }  
  96.     });  
  97.   }  
  98. }  


R.layout.main
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7.     <TextView    
  8.         android:id="@+id/content"  
  9.         android:layout_width="fill_parent"   
  10.         android:layout_height="wrap_content"   
  11.         android:layout_weight="1"  
  12.         android:text="This is main contents"  
  13.         />  
  14.     <include layout="@layout/back_next" />  
  15. </LinearLayout>  


R.layout.first
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7.     <LinearLayout  
  8.         android:orientation="vertical"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_weight="1">  
  12.         <TextView    
  13.            android:id="@+id/content"  
  14.            android:layout_width="wrap_content"   
  15.            android:layout_height="wrap_content"   
  16.            android:text="this is first contents"  
  17.            />  
  18.        <ImageView  
  19.            android:layout_width="wrap_content"   
  20.            android:layout_height="wrap_content"   
  21.            android:src="@drawable/icon"  
  22.            />  
  23.     </LinearLayout>  
  24.     <include layout="@layout/back_next" />  
  25. </LinearLayout>  


R.layout.second
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7.     <LinearLayout  
  8.         android:orientation="vertical"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_weight="1">  
  12.         <TextView    
  13.            android:id="@+id/content"  
  14.            android:layout_width="wrap_content"   
  15.            android:layout_height="wrap_content"   
  16.            android:text="this is second contents"  
  17.            />  
  18.        <ImageView  
  19.            android:layout_width="wrap_content"   
  20.            android:layout_height="wrap_content"   
  21.            android:src="@drawable/icon"  
  22.            />  
  23.        <ImageView  
  24.            android:layout_width="wrap_content"   
  25.            android:layout_height="wrap_content"   
  26.            android:src="@drawable/icon"  
  27.            />  
  28.     </LinearLayout>  
  29.     <include layout="@layout/back_next" />  
  30. </LinearLayout>  


R.layout.back_next
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="horizontal"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="wrap_content"  
  6.     >  
  7.     <Button  
  8.         android:id="@+id/back"  
  9.         android:layout_width="0dip"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_weight="1"  
  12.         android:text="Back"  
  13.         />  
  14.     <Button  
  15.         android:id="@+id/next"  
  16.         android:layout_width="0dip"  
  17.         android:layout_height="wrap_content"  
  18.         android:layout_weight="1"  
  19.         android:text="Next"  
  20.         />  
  21. </LinearLayout>  




 

1 件のコメント:

  1. 複数画面 1 Activity で画面遷移する方法は、簡単でいい方法ですね。初心者なので感心しました。
    andoroidoの、どのドキュメントのどのあたりに、この方法が載っているのでしょうか。
    教えていただけませんか。よろしくお願いします。

    返信削除