Search code examples
javaandroideclipseviewflipper

ViewFlipper order of items


activity_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >

    <ViewFlipper
        android:layout_margin="6dip"
        android:id="@+id/view_flipper"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">           

        <ImageView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/photo1"/>        

        <ImageView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/photo2"/>

        <ImageView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/photo3"/>

        <ImageView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/photo4"/>


    </ViewFlipper>

</LinearLayout>

MainActivity:

public class MainActivity extends Activity {

    private ViewFlipper vf; 
    private float lastX;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    public boolean onTouchEvent(MotionEvent touchevent) {

        switch (touchevent.getAction())
        {
            case MotionEvent.ACTION_DOWN:
            {
                lastX = touchevent.getX();
                break;
            }

            case MotionEvent.ACTION_UP:
            {
                float currentX = touchevent.getX();

                if (lastX < currentX)
                {
                    if (vf.getDisplayedChild()==0) 
                        break;

                    vf.setInAnimation(this, R.anim.in_from_left);
                    vf.setOutAnimation(this, R.anim.out_to_right);
                    vf.showNext();
                }

                if (lastX > currentX)
                {
                    if (vf.getDisplayedChild()==1) 
                        break;

                    vf.setInAnimation(this, R.anim.in_from_right);  
                    vf.setOutAnimation(this, R.anim.out_to_left);
                    vf.showPrevious();                  
                }

                break;
            }

            case MotionEvent.ACTION_MOVE:
            {
                float tempX = touchevent.getX();
                int scrollX = (int) (tempX - lastX);

                //vf.scrollBy(scrollX, 0);

                break;
            }

        }

        return false;

    }
}

Order of images goes like this: photo1, photo4, photo3, photo2. How to make it: photo1, photo2, photo3, photo4?

Thank you


Solution

  • The order of the images are alright, but there is something wrong with your code. See below how to fix the issue:

    public class MainActivity extends Activity {
    
        private ViewFlipper vf;
        private float lastX;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            vf = (ViewFlipper) findViewById(R.id.view_flipper);
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.activity_main, menu);
            return true;
        }
    
        public boolean onTouchEvent(MotionEvent touchevent) {
    
            switch (touchevent.getAction())
            {
                case MotionEvent.ACTION_DOWN:
                {
                    lastX = touchevent.getX();
                    break;
                }
    
                case MotionEvent.ACTION_UP:
                {
                    float currentX = touchevent.getX();
    
                    if (lastX < currentX)
                    {
                        if (vf.getDisplayedChild()==0)
                            break;
    
                        vf.setInAnimation(this, R.anim.in_from_left);
                        vf.setOutAnimation(this, R.anim.out_to_right);
    //                    vf.showNext();
                        vf.showPrevious();
                    }
    
                    if (lastX > currentX)
                    {
    //                    if (vf.getDisplayedChild()==1)
                        if (vf.getDisplayedChild()==vf.getChildCount()-1)
                            break;
    
                        vf.setInAnimation(this, R.anim.in_from_right);
                        vf.setOutAnimation(this, R.anim.out_to_left);
    //                    vf.showPrevious();
                        vf.showNext();
                    }
    
                    break;
                }
    
                case MotionEvent.ACTION_MOVE:
                {
                    float tempX = touchevent.getX();
                    int scrollX = (int) (tempX - lastX);
    
                    //vf.scrollBy(scrollX, 0);
    
                    break;
                }
    
            }
    
            return false;
    
        }
    }