我正在尝试创建一个应用,用户可以在其中滑动并更改他们在屏幕上看到的片段。我无法使用视图寻呼机,因为我希望用户能够永久地滑动到不同的片段。这是我片段中的探测器:
class MyGestureDetector extends SimpleOnGestureListener {
@Override
public boolean onDown(MotionEvent e) {
return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE & Math.abs(velocityX) > 10) {
left();
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE & Math.abs(velocityX) > 10) {
right();
}
return false;
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY){
if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE & distanceX > distanceY) {
left();
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE & distanceX > distanceY) {
right();
}
return false;
}
}
public void right(){
mCallback.dateNumber(true);
sportView.setText("Loading");
}public void left(){
mCallback.dateNumber(false);
sportView.setText("Loading");
}
在我的活动中,这是我添加的用于更改片段的监听器:
@Override
public void dateNumber(Boolean left_right) {
//true == right
//false == left
if(left_right == false){
day = day + 1;
Fragment1 rightFragment = new Fragment1();
Bundle args = new Bundle();
args.putInt("day", day);
rightFragment.setArguments(args);
android.support.v4.app.FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, rightFragment);
transaction.addToBackStack(null);
transaction.commit();
}else if(left_right == true){
day = day - 1;
Fragment1 leftFragment = new Fragment1();
Bundle args = new Bundle();
args.putInt("day", day);
leftFragment.setArguments(args);
android.support.v4.app.FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, leftFragment);
transaction.addToBackStack(null);
transaction.commit();
}
left_right = null;
}
我知道滑动手势始终被识别,但有时新片段无法打开。有谁知道为什么?
答案 0 :(得分:3)
首先,您可以使用droidQuery真正简化您的滑动代码:
//global variables
private boolean isSwiping = false;
private SwipeDetector.Direction swipeDirection = null;
private View v;//set to the parent layout of the fragments.
//swipe-handling code
$.with(v).swipe(new Function() {
@Override
public void invoke($ droidQuery, Object... params) {
if (params[0] == SwipeDetector.Direction.START)
isSwiping = true;
else if (params[0] == SwipeDetector.Direction.STOP) {
if (isSwiping) {
isSwiping = false;
if (swipeDirection != null) {
switch(swipeDirection) {
case DOWN :
//TODO: Down swipe complete, so do something
break;
case UP :
//TODO: Up swipe complete, so do something
break;
case LEFT :
//TODO: Left swipe complete, so do something
break;
case RIGHT :
//TODO: Right swipe complete, so do something (such as):
day++;
Fragment1 rightFragment = new Fragment1();
Bundle args = new Bundle();
args.putInt("day", day);
rightFragment.setArguments(args);
android.support.v4.app.FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container, rightFragment);
transaction.addToBackStack(null);
transaction.commit();
break;
default :
break;
}
}
}
}
else {
swipeDirection = (SwipeDetector.Direction) params[0];
}
}
});
您可以在片段交易here上找到更多信息。
另外,请考虑保留int offset
变量,以便跟踪+/-
偏移量为零。例如,您可以从ArrayList
获取已经实例化的片段,然后只更换mArrayList.get(offset)
处的片段,当向右移动时,执行offset++
和'offset-- `左手滑动。
根据评论中的要求,此代码可用于处理滑动和子图像点击:
在主版块(SwipeInterceptorView
)中加入res/layout/main.xml
:
<?xml version="1.0" encoding="utf-8"?>
<self.philbrown.SwipeInterceptorView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swipe_view"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</self.philbrown.SwipeInterceptorView>
您需要拥有类变量:
SwipeInterceptorView view;//instantiated in onCreate
ImageView fragImage;//must be instantiated when the new Fragment is transitioned in
接下来,在onCreate
中包含以下组件:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//set main view to the main layout
setContentView(R.layout.main);
//get a reference to the content view
view = (SwipeInterceptorView) findViewById(R.id.swipe_view);
//add Swiper
view.setSwipeListener(new SwipeListener() {
public void onUpSwipe(View v) {
//TODO handle up swipe
}
public void onRightSwipe(View v) {
//TODO handle right swipe
}
public void onLeftSwipe(View v) {
//TODO handle left swipe
}
public void onDownSwipe(View v) {
//TODO handle down swipe
}
});
view.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return super.onTouch(v, event);
}
});
}
当转换包含ImageView的新Fragment时,您需要引用它并更新滑动拦截器的onTouch方法:
fragImage = (ImageView) fragment/* references the now non-null, on-display fragment */.getView().findViewById(R.id.yourImageId);
int[] origin = new int[2];
fragImage.getLocationOnScreen(origin);
final Rect bounds = new Rect(origin[0], origin[1], fragImage.getRight(), fragImage.getBottom());
view.setOnTouchListener(new View.OnTouchListener() {
@Override
public void onTouch(View v, MotionEvent event) {
if (bounds.contains(event.getRawX(), event.getRawY())) {
return false;//now clicks will be handled by the Image.
}
return v.onTouchEvent(event);
}
});
答案 1 :(得分:0)
使用Android支持资源库v4中的ViewPager和FragmentPagerAdapter。