1

好的,在这段代码中,我知道您创建的手势检测器类型的实例变量将调用 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;
    }

}

感谢你目前的帮助。

4

1 回答 1

1

onFlingSimpleOnGestureListener 成员可用的方法。要覆盖此方法,您需要 (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. 传回该整数,然后我可以告诉ViewPager更改以在位置 2 处显示片段,从而有效地移动页面。

我不知道如何给你一个代码示例,因为我不知道这是你需要做的。如果您只需要检测滑动,那么您的界面只需要传回一个布尔值。如果为真,您将知道该onFling方法已被调用,并且您设置的任何阈值标准都已满足。然后,您需要在应用程序的上下文中适当地处理该响应。我想我不能再给你任何建议了。

于 2013-09-27T17:35:39.893 回答