2010-10-13 5 views
7

Ich muss eine Anwendung mit 4 Ansicht erstellen. Ich muss von einer Ansicht zu einer anderen einfach durch eine Berührung und eine Bewegung nach links oder nach rechts (keine Taste) gehen. Der Effekt, den ich möchte, ist der gleiche, den Sie sehen, wenn Sie im Hauptmenü von Android navigieren, wenn Sie von einer Seite zu einer anderen wechseln.Wie kann ich ein Schiebe-Layout erstellen, wie das Haupt-Android-Menü?

Ich habe den ViewFlipper getestet, aber ich kann es nicht verwenden: es scheint das Touch-Ereignis nicht korrekt zu erfassen. Ich weiß nicht einmal, ob es die richtige Komponente ist.

Was ist der richtige Weg, damit umzugehen?

+0

Weitere Details: in der Hauptansicht von Android, in der Regel auf der Unterseite, gibt es einige Kugeln, was bedeutet, dass Sie auf Seite 1, 2 oder andere sind. Sie können die Seite wechseln, indem Sie einen Finger verwenden und nach links oder rechts bewegen. Jemand kann mir helfen? – Redax

Antwort

10

Endlich habe ich es geschafft. Das ist meine Lösung. Zunächst müssen Sie ein Hauptlayout definieren, das das untergeordnete Layout enthält.

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 


<ViewFlipper android:id="@+id/ViewFlipper01" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
    <include android:id="@+id/libraryView1" layout="@layout/page_1" /> 
    <include android:id="@+id/libraryView2" layout="@layout/page_2" /> 


</ViewFlipper> 

</RelativeLayout> 

wo page_1 und page_2 sind das Layout, das ich austauschen muss. Diese Layouts sind absolut Standard-Layout, so wie Sie es vorgeben.

Dann benötigen Sie eine Aktivität:

public class Main extends Activity { 

    private ViewFlipper vf; 

    private float oldTouchValue; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     vf=(ViewFlipper)findViewById(R.id.ViewFlipper01); 
    } 

    @Override 
    public boolean onTouchEvent(MotionEvent touchevent) { 
     switch (touchevent.getAction()) 
     { 
      case MotionEvent.ACTION_DOWN: 
      { 
       oldTouchValue = touchevent.getX(); 
       break; 
      } 
      case MotionEvent.ACTION_UP: 
      { 
       //if(this.searchOk==false) return false; 
       float currentX = touchevent.getX(); 
       if (oldTouchValue < currentX) 
       { 
        vf.setInAnimation(inFromLeftAnimation()); 
        vf.setOutAnimation(outToRightAnimation()); 
        vf.showNext(); 
       } 
       if (oldTouchValue > currentX) 
       { 
        vf.setInAnimation(inFromRightAnimation()); 
        vf.setOutAnimation(outToLeftAnimation()); 
        vf.showPrevious(); 
       } 
      break; 
      } 
     } 
     return false; 
    } 

    //for the previous movement 
    public static Animation inFromRightAnimation() { 

     Animation inFromRight = new TranslateAnimation(
     Animation.RELATIVE_TO_PARENT, +1.0f, Animation.RELATIVE_TO_PARENT, 0.0f, 
     Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f 
     ); 
     inFromRight.setDuration(350); 
     inFromRight.setInterpolator(new AccelerateInterpolator()); 
     return inFromRight; 
     } 
    public static Animation outToLeftAnimation() { 
     Animation outtoLeft = new TranslateAnimation(
     Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, -1.0f, 
     Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f 
     ); 
     outtoLeft.setDuration(350); 
     outtoLeft.setInterpolator(new AccelerateInterpolator()); 
     return outtoLeft; 
     }  
    // for the next movement 
    public static Animation inFromLeftAnimation() { 
     Animation inFromLeft = new TranslateAnimation(
     Animation.RELATIVE_TO_PARENT, -1.0f, Animation.RELATIVE_TO_PARENT, 0.0f, 
     Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f 
     ); 
     inFromLeft.setDuration(350); 
     inFromLeft.setInterpolator(new AccelerateInterpolator()); 
     return inFromLeft; 
     } 
    public static Animation outToRightAnimation() { 
     Animation outtoRight = new TranslateAnimation(
     Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, +1.0f, 
     Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f 
     ); 
     outtoRight.setDuration(350); 
     outtoRight.setInterpolator(new AccelerateInterpolator()); 
     return outtoRight; 
     }  
} 

Tada! Erledigt!

2

Ich denke, was Sie suchen, ist ein . Damit konnte man so etwas wie folgt aus:

<SlidingDrawer 
    android:id="@+id/drawer" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 

    android:handle="@+id/handle" 
    android:content="@+id/content"> 

    <ImageView 
     android:id="@id/handle" 
     android:layout_width="88dip" 
     android:layout_height="44dip" /> 

    <GridView 
     android:id="@id/content" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

</SlidingDrawer> 
+0

Guter Effekt, aber nicht was ich brauche: Ich muss die Seite wechseln, um nicht auf der Unterseite eine Seite zu minimieren. – Redax

0

Ich glaube, Sie Layout-Animation für den Zweck verwenden können ..

res/Anim/popin.xml:

<set xmlns:android="http://schemas.android.com/apk/res/android" 
    android:interpolator="@android:anim/accelerate_interpolator"> 
    <scale 
    android:fromXScale="0.0" android:toXScale="1.0" 
    android:fromYScale="0.0" android:toYScale="1.0" 
    android:pivotX="50%" 
    android:pivotY="50%" 
    android:duration="400" 
    /> 
</set> 

res/anim/popinlayout.xml:

<layoutAnimation 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:delay="0.5" 
    android:animationOrder="random" 
    android:animation="@anim/popin" 
/> 

Quelle:

// Applying a Layout Animation and Animation Listener 
aViewGroup.setLayoutAnimationListener(new AnimationListener() { 
    public void onAnimationEnd(Animation _animation) { 
    // TODO: Actions on animation complete. 
    } 
    public void onAnimationRepeat(Animation _animation) {} 
    public void onAnimationStart(Animation _animation) {} 
}); 

aViewGroup.scheduleLayoutAnimation(); 
+0

Ich brauche keine Animation. Animation ist ein Bonus: Ich muss das Layout ändern. – Redax

1

Sie meinen, wie der Startbildschirm, wo Sie zwischen Ansichten wischen können und es an jedem schnappt?
This könnte Ihnen helfen.

+0

Ja! Der Link hat mir geholfen, ist aber kein gutes Beispiel, weil er Fehler und einige unerklärliche Funktionen enthält. Ich werde die komplette Lösung veröffentlichen, die ich gemacht habe. – Redax

Verwandte Themen