如何在一个屏幕上使用三个片段?

时间:2012-12-19 10:35:23

标签: android android-fragments android-animation android-gesture

我正在设计一个应用程序,需要将平板电脑拆分为三个单独的屏幕,如下所示。每个屏幕上都会有一张图片,当你垂直滑动这张图片时,它会带你到下一张图片。我使用Fragments来做到这一点,到目前为止我已经设法创建了3个片段,显示在一个屏幕上,如下图所示每个片段响应向上或向下滑动。我使用了viewflipper和动画来在图像之间滑动。但是,当我将此代码添加到我的应用程序时,它会导致崩溃。我在logcat中获得了一个膨胀异常,如下所示:

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

代码本身工作正常但是当我将它添加到我的片段类时,我得到了上面的错误。有人知道是否可以在片段类中使用viewflipper和动画?我可能接近这个错误的方法吗?感谢

enter image description here

主类:

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>

片段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;

}

}

片段布局 - 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>

2 个答案:

答案 0 :(得分:2)

我找到了如何使这个工作。我没有正确设置我的gestureDetector,这就是我刷屏幕时没有发生任何事情的原因。下面是我用来使它工作的代码。

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;

    }


}

答案 1 :(得分:0)

尝试从onDown(MotionEvent e)方法返回true