首先,您可以使用我的方法真正简化您的刷卡代码droid查询图书馆:
//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];
}
}
});
您可以找到有关 Fragment 交易的更多信息here.
另外,考虑保留一个int offset
跟踪的变量+/-
从零偏移。例如,您可以从一个实例中获取已经实例化的片段ArrayList
,然后只需替换掉那个mArrayList.get(offset)
,当向右猛击时,做offset++
,以及向左滑动的“offset--”。
Edit
根据评论中的要求,此代码可用于处理滑动和子图像单击:
包括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);
}
});