Java 如何正确实施onFling方法?
好的,在这段代码中,我得到了你创建的手势检测器类型的实例变量,它将调用FlingeStureListener.class。我得到了一个错误Java 如何正确实施onFling方法?,java,android,Java,Android,好的,在这段代码中,我得到了你创建的手势检测器类型的实例变量,它将调用FlingeStureListener.class。我得到了一个错误超级接口必须是一个接口,但我已经创建了类型为“interface”的接口。当我将id引用到我的xml布局名称时,该id也会给出一个错误。实现FlingestureListener使我的studentGallery类现在看起来像这样,并实现FlingestureListener: public class GalleryStudent extends Activ
超级接口必须是一个接口
,但我已经创建了类型为“interface”的接口。当我将id引用到我的xml布局名称时,该id也会给出一个错误。实现FlingestureListener使我的studentGallery类现在看起来像这样,并实现FlingestureListener:
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\u FRAGMENT)
在本例中,您将片段作为其参数,而我的类不是片段。是否有一种简单的方法将我的类更改为片段以适应此情况?在本例中,onTabSwiperListener也无法解析为类型
。这是我根据您的解释使用的FlingestureListener,我已创建为接口:
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;
}
}
感谢您迄今为止的帮助。
onFling
是一种可供SimpleGestureListener
成员使用的方法。要重写此方法,您需要(1)创建一个扩展SimpleOnGestureListener
的类,或者(2)在现有类中实现一个手势侦听器。然后您可以覆盖onFling
更新
抱歉耽搁了。这是我的实现。在要使用onFling
的活动中,您需要设置一个使用自定义onFling
侦听器的GestureDetector
。你可以看到我使用的是FlingGestureListener
代码如下。我正在收听一个特定图像的触摸,peopleTab,但您应该能够替换任何视图
final GestureDetector gdt = new GestureDetector(getActivity(), FlingGestureListener.getInstance(tabSwipeListener));
final ImageView peopleTab = (ImageView)getActivity().findViewById(R.id.peopleFlag);
peopleTab.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return gdt.onTouchEvent(event);
}
});
然后对于FlingeStureListener,我使用一个单独的类来扩展SimpleOnGestureListener
,因此我可以选择只实现onFling
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;
}
}
我使用这个类的回调来指示何时发生了刷卡。返回的布尔值仅用于指示是否已处理整个触摸事件。希望这能有所帮助
更新2
对于接口,我使用的是特定于我的设置的东西
tabSwipeListener.onTabSwipe(true, PLACES_FRAGMENT);
这首先表明发生了刷卡以及刷卡方向。我在一个3段ViewPager
设置中使用它。从中间的页面向右滑动到位置列表,向左滑动到人员列表。例如,PLACES_FRAGMENT常量是一个整数,它引用ViewPager
中的片段id。传回该整数后,我可以告诉ViewPager
更改以在位置2显示片段,从而有效地移动页面
我不知道如何给你一个代码示例,因为我不知道这是你需要做的。如果您只需要检测刷卡,那么您的接口只需要传回一个布尔值。如果为true,您将知道调用了
onFling
方法,并且满足了您设置的任何阈值标准。然后,您需要在应用程序的上下文中适当地处理该响应。我想我不能再给你任何建议了 onFling
是一种可供SimpleGestureListener
成员使用的方法。要重写此方法,您需要(1)创建一个扩展SimpleOnGestureListener
的类,或者(2)在现有类中实现一个手势侦听器。然后您可以覆盖onFling
更新
抱歉耽搁了。这是我的实现。在要使用onFling
的活动中,您需要设置一个使用自定义onFling
侦听器的GestureDetector
。你可以看到我使用的是FlingGestureListener
代码如下。我正在收听一个特定图像的触摸,peopleTab,但您应该能够替换任何视图
final GestureDetector gdt = new GestureDetector(getActivity(), FlingGestureListener.getInstance(tabSwipeListener));
final ImageView peopleTab = (ImageView)getActivity().findViewById(R.id.peopleFlag);
peopleTab.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return gdt.onTouchEvent(event);
}
});
然后对于FlingeStureListener,我使用一个单独的类来扩展SimpleOnGestureListener
,因此我可以选择只实现onFling
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;
}
}
我使用这个类的回调来指示何时发生了刷卡。返回的布尔值仅用于指示是否已处理整个触摸事件。希望这能有所帮助
更新2
对于接口,我使用的是特定于我的设置的东西
tabSwipeListener.onTabSwipe(true, PLACES_FRAGMENT);
这首先表明发生了刷卡以及刷卡方向。我在一个3段ViewPager
设置中使用它。从中间的页面向右滑动到位置列表,向左滑动到人员列表。例如,PLACES_FRAGMENT常量是一个整数,它引用ViewPager
中的片段id。传回该整数后,我可以告诉ViewPager
更改以在位置2显示片段,从而有效地移动页面
我不知道如何给你一个代码示例,因为我不知道这是你需要做的。如果您只需要检测刷卡,那么您的接口只需要传回一个布尔值。如果为true,您将知道调用了
onFling
方法,并且满足了您设置的任何阈值标准。然后,您需要在应用程序的上下文中适当地处理该响应。我想我不能再给你任何建议了 您需要在实现该方法的东西上使用setOnTouchListener(…)
。视图
类型的任何对象或视图
的子类都可以。返回detector.onTouchEvent(事件)在你的匿名类中,它不知道什么是“检测器”。你能为我进一步澄清一下吗?我不太明白你的解释。@user2815899他说这个方法不在一个视图类中。仍然不确定如何修复这个实现…你需要使用setOnTouchListener(…)