1

I am designing an application that requires a tablet to be split into three separate screens as shown below. Each screen will have a picture on it and when you swipe this picture vertically it will bring you to the next picture.I have used Fragments to do this and so far I have managed to create 3 Fragments that are displayed on one screen as shown below.Each fragment responds to an up or down swipe.I have used viewflipper and animation to swipe between images. However when I add this code to my application it causes it to crash.I get an inflate exception in my logcat as show below:

01-08 15:52:05.870: E/AndroidRuntime(4061): FATAL EXCEPTION: main
01-08 15:52:05.870: E/AndroidRuntime(4061): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.david.fragmenttest/com.david.fragmenttest.MainFragment}: android.view.InflateException: Binary XML file line #13: Error inflating class fragment

The code works fine on its own but when I add it to my fragment class I get the above error.Does anyone know if its possible to use viewflipper and animation within a fragment class?Am i maybe approaching this the wrong way?Thanks

enter image description here

Main Class:

public class Main extends Activity {

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

    }

}

Main.xml

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

    <fragment
        android:id="@+id/imOne"
        android:name="com.david.ImageOne"
        android:layout_width="400dp"
        android:layout_height="fill_parent" />

    <fragment
        android:id="@+id/imTwo"
        android:name="com.david.ImageTwo"
        android:layout_width="400dp"
        android:layout_height="fill_parent" />

    <fragment
        android:id="@+id/imThree"
        android:name="com.david.ImageThree"
        android:layout_width="400dp"
        android:layout_height="fill_parent" />

</LinearLayout>

Class for Fragment A

public class ImageTwo extends Fragment {

private ViewFlipper mViewFlipper;

private int mSpeed;
private int mCount;
private int mFactor;
private boolean mAnimating;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.pic_two, container, false);
    mViewFlipper = (ViewFlipper) getView().findViewById(R.id.view_flipper);

    mAnimating = false;
    mCount = 0;
    mSpeed = 0;

    final GestureDetector gesture = new GestureDetector(getActivity(),
            new GestureDetector.SimpleOnGestureListener() {

                @Override
                public boolean onDown(MotionEvent e) {
                    // TODO Auto-generated method stub
                    return true;
                }

                private Runnable r1 = new Runnable() {

                    @Override
                    public void run() {
                        up();
                        if (mCount < 1) {
                            mAnimating = false;
                        } else {
                            Handler h = new Handler();
                            h.postDelayed(r1, mSpeed);
                        }
                    }

                };

                private Runnable r2 = new Runnable() {

                    @Override
                    public void run() {
                        down();
                        if (mCount < 1) {
                            mAnimating = false;
                        } else {
                            Handler h = new Handler();
                            h.postDelayed(r2, mSpeed);
                        }
                    }

                };

                @Override
                public boolean onFling(MotionEvent start,
                        MotionEvent finish, float xVelocity, float yVelocity) {
                    try {
                        if (mAnimating)
                            return true;
                        mAnimating = true;
                        mCount = (int) Math.abs(yVelocity) / 900;
                        mFactor = (int) 300 / mCount;
                        mSpeed = mFactor;
                        if (yVelocity > 0) {
                            // down
                            Handler h = new Handler();
                            h.postDelayed(r2, mSpeed);
                        } else {
                            // up
                            Handler h = new Handler();
                            h.postDelayed(r1, mSpeed);
                        }
                        // ((TextView)findViewById(R.id.velocity)).setText("VELOCITY => "+Float.toString(yVelocity));
                    } catch (ArithmeticException e) {
                        // swiped too slow doesn't register
                        mAnimating = false;
                    }
                    return true;
                }

                private void up() {
                    mCount--;
                    mSpeed += mFactor;
                    Animation inFromBottom = new TranslateAnimation(
                            Animation.RELATIVE_TO_PARENT, 0.0f,
                            Animation.RELATIVE_TO_PARENT, 0.0f,
                            Animation.RELATIVE_TO_PARENT, 1.0f,
                            Animation.RELATIVE_TO_PARENT, 0.0f);
                    inFromBottom
                            .setInterpolator(new AccelerateInterpolator());
                    inFromBottom.setDuration(mSpeed);
                    Animation outToTop = new TranslateAnimation(
                            Animation.RELATIVE_TO_PARENT, 0.0f,
                            Animation.RELATIVE_TO_PARENT, 0.0f,
                            Animation.RELATIVE_TO_PARENT, 0.0f,
                            Animation.RELATIVE_TO_PARENT, -1.0f);
                    outToTop.setInterpolator(new AccelerateInterpolator());
                    outToTop.setDuration(mSpeed);
                    mViewFlipper.clearAnimation();
                    mViewFlipper.setInAnimation(inFromBottom);
                    mViewFlipper.setOutAnimation(outToTop);
                    if (mViewFlipper.getDisplayedChild() == 0) {
                        mViewFlipper.setDisplayedChild(2);
                    } else {
                        mViewFlipper.showPrevious();
                    }

                }

                private void down() {
                    mCount--;
                    mSpeed += mFactor;
                    Animation outToBottom = new TranslateAnimation(
                            Animation.RELATIVE_TO_PARENT, 0.0f,
                            Animation.RELATIVE_TO_PARENT, 0.0f,
                            Animation.RELATIVE_TO_PARENT, 0.0f,
                            Animation.RELATIVE_TO_PARENT, 1.0f);
                    outToBottom
                            .setInterpolator(new AccelerateInterpolator());
                    outToBottom.setDuration(mSpeed);
                    Animation inFromTop = new TranslateAnimation(
                            Animation.RELATIVE_TO_PARENT, 0.0f,
                            Animation.RELATIVE_TO_PARENT, 0.0f,
                            Animation.RELATIVE_TO_PARENT, -1.0f,
                            Animation.RELATIVE_TO_PARENT, 0.0f);
                    inFromTop.setInterpolator(new AccelerateInterpolator());
                    inFromTop.setDuration(mSpeed);
                    mViewFlipper.clearAnimation();
                    mViewFlipper.setInAnimation(inFromTop);
                    mViewFlipper.setOutAnimation(outToBottom);
                    if (mViewFlipper.getDisplayedChild() == 0) {
                        mViewFlipper.setDisplayedChild(2);
                    } else {
                        mViewFlipper.showPrevious();
                    }

                }

                @Override
                public void onLongPress(MotionEvent e) {
                    // TODO Auto-generated method stub

                }

                @Override
                public boolean onScroll(MotionEvent e1, MotionEvent e2,
                        float distanceX, float distanceY) {
                    // TODO Auto-generated method stub
                    return false;
                }

                @Override
                public void onShowPress(MotionEvent e) {
                    // TODO Auto-generated method stub

                }

                @Override
                public boolean onSingleTapUp(MotionEvent e) {
                    // TODO Auto-generated method stub
                    return false;
                }

            });

    view.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            return gesture.onTouchEvent(event);
        }
    });

    return view;

}

}

Fragment Layout - pic_one.xml

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

    <ImageView
        android:id="@+id/imOne"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/bus_1" />

</LinearLayout>
DMC
  • 1,184
  • 5
  • 21
  • 50

2 Answers2

2

I worked out how to get this working. I had not set up my gestureDetector properly which is why nothing was happening when I was swiping the screen. Below is the code I used to get it working.

public class ImageOne extends Fragment  {


    private static final int SWIPE_THRESHOLD = 100;
    private static final int SWIPE_VELOCITY_THRESHOLD = 100;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.pic_one, container, false);
        final GestureDetector gesture = new GestureDetector(getActivity(),
                new GestureDetector.SimpleOnGestureListener() {


            @Override
            public boolean onDown(MotionEvent e) {
                // TODO Auto-generated method stub
                return true;
            }

            @Override
            public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                    float velocityY) {
                boolean result = false;
                try {
                    float diffY = e2.getY() - e1.getY();
                    float diffX = e2.getX() - e1.getX();
                    if (Math.abs(diffX) > Math.abs(diffY)) {
                        if (Math.abs(diffX) > SWIPE_THRESHOLD
                                && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
                            if (diffX > 0) {
                                onSwipeRight();
                            } else {
                                onSwipeLeft();
                            }
                        }
                    } else {
                        if (Math.abs(diffY) > SWIPE_THRESHOLD
                                && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
                            if (diffY > 0) {
                                onSwipeBottom();
                            } else {
                                onSwipeTop();
                            }
                        }
                    }
                } catch (Exception exception) {
                    exception.printStackTrace();
                }
                return result;
            }

            @Override
            public void onLongPress(MotionEvent e) {
                // TODO Auto-generated method stub

            }

            @Override
            public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
                    float distanceY) {
                // TODO Auto-generated method stub
                return false;
            }

            @Override
            public void onShowPress(MotionEvent e) {
                // TODO Auto-generated method stub

            }

            @Override
            public boolean onSingleTapUp(MotionEvent e) {
                // TODO Auto-generated method stub
                return false;
            }

            public void onSwipeRight() {
                Toast.makeText(getActivity(), " RIGHT ", Toast.LENGTH_LONG).show();
            }

            public void onSwipeLeft() {
                Toast.makeText(getActivity(), " LEFT ", Toast.LENGTH_LONG).show();


            }

            public void onSwipeTop() {
                Toast.makeText(getActivity(), " TOP ", Toast.LENGTH_LONG).show();

            }

            public void onSwipeBottom() {
                Toast.makeText(getActivity(), " BOTTOM ", Toast.LENGTH_LONG).show();

            }


        });


        view.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return gesture.onTouchEvent(event);
            }
        });

        return view;

    }


}
DMC
  • 1,184
  • 5
  • 21
  • 50
0

Try returning true from onDown(MotionEvent e) method

Srokowski Maciej
  • 431
  • 5
  • 15
  • I have edited my code to include my fragment layout which consists of an ImageView. The plan is to have three ImageViews and a ViewFlipper which the user will be able to scroll through. – DMC Jan 03 '13 at 13:15
  • No because I have my code working as a separate application but when I try and use it with fragments then it stops working. – DMC Jan 03 '13 at 13:43