我正在設計一個應用程序,需要將平板電腦分成三個獨立的屏幕,如下所示。每個屏幕上都會有一個圖片,當你垂直滑動這張圖片時,它會帶你到下一張圖片。我已經使用Fragments來做到這一點,到目前爲止,我已經設法創建3個片段,顯示在一個屏幕上,如下所示。每個片段都會響應向上或向下滑動。我已經使用了viewflipper和動畫來在圖像之間滑動。然而,當我將此代碼添加到我的應用它,它會導致crash.I我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
代碼工作對自己很好,但是當我把它添加到我的片段類?我得到的上述error.Does任何人都知道,如果可能的片段類中使用viewflipper和動畫難道我也許這接近了錯誤的方式感謝
主類:
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>
爲什麼不使用'ViewPager'? – CommonsWare
因爲我是垂直滑動圖片而不是水平滑動 – DMC