好的,在这段代码中,我知道您创建的手势检测器类型的实例变量将调用 FlingGestureListener.class。我收到错误a super interface must be an interface
,但我已将其创建为“接口”类型。id 也给出了错误当我将它引用到我的 xml 布局名称时。实现 FlingGestureListner 所以我的 studentGallery 类现在看起来像这样并实现了 FlingGestureListener:
public class GalleryStudent extends Activity implements FlingGestureLitener {
private static final int MAJOR_MOVE = 0;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_galery_student);
final GestureDetector gdt = new GestureDetector(getActivity(),
FlingGestureListener.getInstance(tabSwipeListener));
final ImageView peopleTab = (ImageView)getActivity().findViewById(R.id.activity_galery_student);
peopleTab.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return gdt.onTouchEvent(event);
}
});
Gallery g = (Gallery) findViewById(R.id.gallery);
g.setAdapter(new ImageAdapter(this));
g.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(@SuppressWarnings("rawtypes") AdapterView parent, View v, int position, long id) {
Toast.makeText(GalleryStudent.this, "" + position, Toast.LENGTH_SHORT).show();
}
});
}
当您使用它导航到类时,我在这里感到困惑:tabSwipeListener.onTabSwipe(true, PLACES_FRAGMENT);
在这种情况下,您将片段作为其参数,而我的类不是片段。是否有一种简单的方法可以将我的类更改为片段以适应这种情况?在这种情况下也是onTabSwiperListener cannot be resolved to a type
。这是我从您的解释中使用的 FlingGestureListener,它是我作为接口创建的:
public class FlingGestureListener extends SimpleOnGestureListener {
private static final int SWIPE_MIN_DISTANCE = 80;
private static final int SWIPE_THRESHOLD_VELOCITY = 50;
private static final int PEOPLE_FRAGMENT = 0;
private static final int PLACES_FRAGMENT = 2;
private OnTabSwipedListener tabSwipeListener;
private FlingGestureListener(OnTabSwipedListener tabSwipeListener){
this.tabSwipeListener = tabSwipeListener;
}
/**
* Static factory
* @param listener called when tab swiped
* @return
*/
public static FlingGestureListener getInstance(OnTabSwipedListener listener){
return new FlingGestureListener(listener);
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY){
if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY){
Log.d("SWIPE", "right to left");
tabSwipeListener.onTabSwipe(true, PLACES_FRAGMENT); //sent int to fragment container to switch pager to that view
return true; //Right to left
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY){
Log.d("SWIPE", "left to right");
tabSwipeListener.onTabSwipe(true, PEOPLE_FRAGMENT);
return true; //Left to right
}
//This will test for up and down movement.
if(e1.getY() - e2.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY){
return false; //Bottom to top
} else if (e2.getY() - e1.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY){
return false; //Top to bottom
}
return false;
}
}
感谢你目前的帮助。