gpt4 book ai didi

android - 如何在一个屏幕上使用三个 fragment ?

转载 作者:行者123 更新时间:2023-11-29 21:53:02 26 4
gpt4 key购买 nike

我正在设计一个需要将平板电脑分成三个独立屏幕的应用程序,如下所示。每个屏幕上都会有一张图片,当你垂直滑动这张图片时,它会带你到下一张图片。我已经使用 fragment 来做到这一点,到目前为止,我已经成功地创建了 3 个 fragment ,它们显示在一个屏幕上,如下所示.Each fragment responses to an up or down swipe.I have used viewflipper and animation to swipe between images. 每个 fragment 响应向上或向下滑动。我使用 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

代码本身运行良好,但是当我将它添加到我的 fragment 类时,我得到了上述错误。有谁知道是否可以在 fragment 类中使用 viewflipper 和动画?我可能以错误的方式接近这个吗?谢谢

enter image description here

主类:

public class Main extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.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>

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

最佳答案

我想出了如何让这个工作。我没有正确设置 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;

}


}

关于android - 如何在一个屏幕上使用三个 fragment ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13950409/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com