Advertisement
_Tobias

Fun little Android class

Mar 4th, 2014
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.92 KB | None | 0 0
  1. package appisode.gepro;
  2.  
  3. /**
  4.  * @author Tobias
  5.  */
  6.  
  7. import java.util.ArrayList;
  8. import java.util.List;
  9.  
  10. import android.app.Activity;
  11. import android.os.Bundle;
  12. import android.view.View;
  13. import android.view.View.OnClickListener;
  14. import android.view.ViewGroup;
  15. import android.widget.Button;
  16. import android.widget.ViewFlipper;
  17.  
  18. public abstract class SplashActivity extends Activity {
  19.     private ViewFlipper flipper = null;
  20.     private Button prev = null;
  21.     private Button next = null;
  22.    
  23.     private int currentPos = 0;
  24.     public void onCreate(Bundle savedInstanceState) {
  25.         super.onCreate(null);
  26.        
  27.         setContentView(R.layout.general_splash);
  28.        
  29.         viewHolder = new ArrayList<View[]>(getCount());
  30.        
  31.         flipper = (ViewFlipper) findViewById(R.id.splashFlipper);
  32.         prev = (Button) findViewById(R.id.prev);
  33.         next = (Button) findViewById(R.id.next);
  34.        
  35.         for(int i = 0; i < getCount(); i++) {
  36.             View v = getLayoutInflater().inflate(getLayout(i), null);
  37.             flipper.addView(v);
  38.             viewHolder.add(create(i, (ViewGroup) v));
  39.            
  40.             if(savedInstanceState != null) {
  41.                 Bundle b = savedInstanceState.getBundle("screen"+i);
  42.                 if(b != null) {
  43.                     restore(i, viewHolder.get(i), b);
  44.                 }
  45.             }
  46.         }
  47.        
  48.         flipper.setDisplayedChild(0);
  49.        
  50.         prev.setOnClickListener(new OnClickListener() {
  51.             public void onClick(View v) {
  52.                 if(currentPos > 0) {
  53.                     if(move(currentPos, currentPos-1)) {
  54.                         return;
  55.                     }
  56.                     prev.setText(R.string.back);
  57.                     next.setText(R.string.fwd);
  58.                     next.setVisibility(View.VISIBLE);
  59.                     currentPos--;
  60.                     if(currentPos == 0) {
  61.                         prev.setVisibility(View.INVISIBLE);
  62.                     }
  63.                     flipper.setDisplayedChild(currentPos);
  64.                     moved(currentPos);
  65.                 }
  66.             }
  67.         });
  68.        
  69.         next.setOnClickListener(new OnClickListener() {
  70.             public void onClick(View v) {
  71.                 if(currentPos < getCount()-1) {
  72.                     if(move(currentPos, currentPos+1)) {
  73.                         return;
  74.                     }
  75.                     prev.setVisibility(View.VISIBLE);
  76.                     currentPos++;
  77.                     if(currentPos == getCount()-1) {
  78.                         next.setText(R.string.finish);
  79.                     }
  80.                     flipper.setDisplayedChild(currentPos);
  81.                     moved(currentPos);
  82.                 }
  83.                 else if(currentPos == getCount()-1) {
  84.                     onFinish();
  85.                 }
  86.             }
  87.         });
  88.     }
  89.    
  90.     public void onSaveInstanceState(Bundle out) {
  91.         for(int i = 0; i < getCount(); i++) {
  92.             out.putBundle("screen"+i, save(i, viewHolder.get(i)));
  93.         }
  94.     }
  95.    
  96.     public ViewGroup getItem(int pos) {
  97.         return (ViewGroup)  flipper.getChildAt(pos);   
  98.     }
  99.    
  100.     private List<View[]> viewHolder = null;
  101.    
  102.     public abstract int getCount();
  103.     public abstract int getLayout(int pos);
  104.     public abstract View[] create(int pos, ViewGroup v);
  105.     public abstract Bundle save(int pos, View[] holder);
  106.     public abstract void restore(int pos, View[] holder, Bundle restore);
  107.     public abstract void reset(int pos, ViewGroup v, View[] holder);
  108.     public abstract boolean move(int current, int to);
  109.     public abstract void moved(int pos);
  110.     public abstract void onFinish();
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement