Search code examples
javaandroidviewflipper

does not work ViewFlipper


sorry for my english. When the picture to fill the screen, I want to do that could be used to scroll. To do this, use ViewFlipper. but it does not work. is the class that displays images in full screen. What am I doing wrong?

    public class FullScreenImage extends Activity implements OnTouchListener{
    private ViewFlipper flipper = null;
    private float fromPosition;

    protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.full_screen);
         Intent intent = getIntent();
         long imageId = intent.getExtras().getLong(FullScreenImage.class.getName());

         LinearLayout mainLayout = (LinearLayout) findViewById(R.id.main_layout);
         flipper = (ViewFlipper) findViewById(R.id.fullImage);
flipper.setOnTouchListener(this);
         ImageView imageView = (ImageView) findViewById(R.id.imageView1);

         imageView.setLayoutParams( new ViewFlipper.LayoutParams(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT));

         imageView.setImageResource((int) imageId);


  }

    public boolean onTouch(View view, MotionEvent event)
    {
        switch (event.getAction())
        {
        case MotionEvent.ACTION_DOWN: 
            fromPosition = event.getX();
            break;
        case MotionEvent.ACTION_UP: 
            float toPosition = event.getX();
            if (fromPosition > toPosition)
                flipper.showNext();
            else if (fromPosition < toPosition)
                flipper.showPrevious();
        default:
            break;
        }
        return true;
    }
}

xml file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ViewFlipper
            android:id="@+id/fullImage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    </ViewFlipper>

</LinearLayout>

Solution

  • Your XML has but a single member within the flipper. The idea is to have two or items and the flipper will change between them. Add another image like this to your flipper

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
    <ViewFlipper
            android:id="@+id/fullImage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >
    
    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    
    
    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    
    </ViewFlipper>