如何正确实现onFling方法?

时间:2013-09-27 17:27:21

标签: java android

好的,在这段代码中,我得到了你的创建实例变量类型的手势检测器,它将调用FlingGestureListener.class。我得到错误a super interface must be an interface但是我创建它的类型为“interface” “。当我将它引用到我的xml布局名称时,id也会出错。实现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;
    }

}

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

onFlingSimpleOnGestureListener class成员可以使用的方法。要覆盖此方法,您需要(1)创建扩展SimpleOnGestureListener的类或(2)在现有类中实现手势监听器。然后,您可以覆盖onFling

<强>更新

抱歉延误。这是我的实施。在您要使用onFling的活动中,您需要设置使用自定义GestureDetector侦听器的onFling。您可以看到我使用的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);
        }
    });

然后对于FlingGestureListener,我使用了一个扩展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方法,并且已满足您设置的任何阈值条件。然后,您需要在应用程序的上下文中适当地处理该响应。我不认为我能给你更多的建议。

相关问题