2010-10-13 14 views
7

मुझे 4 व्यू के साथ एक एप्लिकेशन बनाने की आवश्यकता है। मुझे एक स्पर्श से बस एक स्पर्श से और बाएं या दाएं (कोई बटन) पर जाने की आवश्यकता है। जब मैं किसी पृष्ठ से दूसरे पृष्ठ पर जाता हूं तो आप जिस प्रभाव को पसंद करेंगे वह वही है जब आप एंड्रॉइड के मुख्य मेनू में नेविगेट करते हैं।मैं मुख्य एंड्रॉइड मेनू की तरह एक स्लाइडिंग लेआउट कैसे बना सकता हूं?

मैंने व्यूफ्लिपर का परीक्षण किया है, लेकिन मैं इसका उपयोग नहीं कर सकता: ऐसा लगता है कि स्पर्श ईवेंट को सही तरीके से पकड़ना नहीं है। मुझे यह भी नहीं पता कि यह सही घटक है या नहीं।

इसे संभालने का सही तरीका क्या है?

+0

अधिक जानकारी: एंड्रॉइड के मुख्य दृश्य में, आमतौर पर नीचे, कुछ गोलियां होती हैं, जिसका अर्थ है कि आप पृष्ठ 1, 2 या अन्य पर हैं। आप एक उंगली का उपयोग करके और बाएं या दाएं स्थानांतरित करके पृष्ठ बदल सकते हैं। कोई मेरी मदद कर सकता है? – Redax

उत्तर

10

अंततः मैंने इसे बनाया। यह मेरा समाधान है। सबसे पहले आपको एक मुख्य लेआउट परिभाषित करने की आवश्यकता है, जिसमें बाल लेआउट शामिल है।

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

जहां पृष्ठ_1 और पृष्ठ_2 वह लेआउट है जिसे मुझे विनिमय करने की आवश्यकता है। वे लेआउट बिल्कुल मानक लेआउट हैं, जैसा कि आप प्रीफियर करते हैं।

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; 
     }  
} 

टाडा:

तो फिर तुम एक गतिविधि की जरूरत है! किया हुआ!

2

मुझे लगता है कि आप जो खोज रहे हैं वह SlidingDrawer है। कि के साथ, आप कर सकते थे तो कुछ इस तरह:

<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

अच्छा प्रभाव, लेकिन मुझे जो चाहिए वह नहीं: मुझे पृष्ठ को बदलने की आवश्यकता है, न कि पृष्ठ के नीचे कम से कम। – Redax

0

.. मुझे लगता है कि आप इस प्रयोजन के लिए लेआउट एनीमेशन का उपयोग कर सकते

रेस/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> 

रेस/anim/popinlayout.xml:

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

स्रोत:

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

मुझे एनीमेशन की आवश्यकता नहीं है। एनीमेशन एक बोनस है: मुझे लेआउट बदलने की जरूरत है। – Redax

1

आपका मतलब होम स्क्रीन की तरह है जहां आप विचारों के बीच स्वाइप कर सकते हैं और यह प्रत्येक पर स्नैप हो सकता है?
This आपकी मदद कर सकता है।

+0

हां! लिंक ने मेरी मदद की, लेकिन यह एक अच्छा नमूना नहीं है क्योंकि इसमें त्रुटियां और कुछ अस्पष्ट सुविधा शामिल है। मैं अपना पूरा समाधान पोस्ट करूंगा। – Redax

संबंधित मुद्दे