2

我有一个片段,其中有一个包含两个 ScrollViews 的 ViewFlipper。我想检测 ViewFlipper 上的滑动并更改 ScrollView。我试图这样做:

public class HomeFragment extends Fragment implements OnGestureListener 
{
    private static final int SWIPE_MIN_DISTANCE = 120;
    private static final int SWIPE_MAX_OFF_PATH = 250;
    private static final int SWIPE_TRESHOLD_VELOCITY = 200;


    private View thisFragment;

    private Animation slideLeftIn;
    private Animation slideLeftOut;
    private Animation slideRightIn;
    private Animation slideRightOut;

    private GestureDetector detecture;

    private ViewFlipper riddleFlipper;

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
    {
        thisFragment = LayoutInflater.from(getActivity()).inflate(R.layout.home_fragment, null);

        //views
        riddleFlipper = (ViewFlipper) thisFragment.findViewById(R.id.home_fragment_riddle_flipper_ViewFlipper);

        slideLeftIn = AnimationUtils.loadAnimation(getActivity(), R.anim.slide_left_in);
        slideLeftOut = AnimationUtils.loadAnimation(getActivity(), R.anim.slide_left_out);
        slideRightIn = AnimationUtils.loadAnimation(getActivity(), R.anim.slide_right_in);
        slideRightOut = AnimationUtils.loadAnimation(getActivity(), R.anim.slide_right_out);

        //sliding
        detecture = new GestureDetector(getActivity(), this);

        riddleFlipper.setOnTouchListener(new OnTouchListener()
        {

            @Override
            public boolean onTouch(View view, MotionEvent event)
            {
                return detecture.onTouchEvent(event);
            }
        });


        return thisFragment;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public boolean onDown(MotionEvent e)
    {
        return false;
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
    {
        if(Math.abs(e1.getY() - e2.getY()) > 250)
            return false;
        if((e1.getX() - e2.getX()) > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_TRESHOLD_VELOCITY)
        {
            riddleFlipper.setInAnimation(slideLeftIn);
            riddleFlipper.setOutAnimation(slideLeftOut);
            riddleFlipper.showNext();
        }
        else if((e2.getX() - e1.getX()) > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_TRESHOLD_VELOCITY)
        {
            riddleFlipper.setInAnimation(slideRightIn);
            riddleFlipper.setOutAnimation(slideRightOut);
            riddleFlipper.showNext();
        }

        return false;
    }

    @Override
    public void onLongPress(MotionEvent e)
    {

    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
            float distanceY)
    {
        return false;
    }

    @Override
    public void onShowPress(MotionEvent e)
    {

    }

    @Override
    public boolean onSingleTapUp(MotionEvent e)
    {
        return false;
    }

}

似乎 riddleFlipper 没有“感觉”任何触摸事件。

编辑

我已将代码更改为:

public class HomeFragment extends Fragment implements OnGestureListener 
{
    private static final int SWIPE_MIN_DISTANCE = 120;
    private static final int SWIPE_MAX_OFF_PATH = 250;
    private static final int SWIPE_TRESHOLD_VELOCITY = 200;


    private View thisFragment;
    private ImageView userImage;
    private ViewFlipper riddleFlipper;
    private RelativeLayout usersRiddleLinearLayout;
    private RelativeLayout favoriteLinearLayout;

    private Animation slideLeftIn;
    private Animation slideLeftOut;
    private Animation slideRightIn;
    private Animation slideRightOut;

    private GestureDetector detecture;



    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
    {
        thisFragment = LayoutInflater.from(getActivity()).inflate(R.layout.home_fragment, null);

        //views
        riddleFlipper = (ViewFlipper) thisFragment.findViewById(R.id.home_fragment_riddle_flipper_ViewFlipper);
        userImage = (ImageView) thisFragment.findViewById(R.id.home_fragment_user_image_ImageView);
        usersRiddleLinearLayout = (RelativeLayout) thisFragment.findViewById(R.id.home_fragment_users_riddles_LinearLayout);
        favoriteLinearLayout = (RelativeLayout) thisFragment.findViewById(R.id.home_fragment_favorite_riddles_LinearLayout);

        slideLeftIn = AnimationUtils.loadAnimation(getActivity(), R.anim.slide_left_in);
        slideLeftOut = AnimationUtils.loadAnimation(getActivity(), R.anim.slide_left_out);
        slideRightIn = AnimationUtils.loadAnimation(getActivity(), R.anim.slide_right_in);
        slideRightOut = AnimationUtils.loadAnimation(getActivity(), R.anim.slide_right_out);

        //sliding
        detecture = new GestureDetector(getActivity(), this);

        usersRiddleLinearLayout.setOnTouchListener(new OnTouchListener()
        {

            @Override
            public boolean onTouch(View view, MotionEvent event)
            {

                return detecture.onTouchEvent(event);
            }
        });

        favoriteLinearLayout.setOnTouchListener(new OnTouchListener()
        {

            @Override
            public boolean onTouch(View view, MotionEvent event)
            {

                return detecture.onTouchEvent(event);
            }
        });
        return thisFragment;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public boolean onDown(MotionEvent e)
    {
        return true;
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
    {
        if(Math.abs(e1.getY() - e2.getY()) > 250)
            return false;
        if((e1.getX() - e2.getX()) > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_TRESHOLD_VELOCITY)
        {
            Toast.makeText(getActivity(), "a", Toast.LENGTH_SHORT).show();
            riddleFlipper.setInAnimation(slideLeftIn);
            riddleFlipper.setOutAnimation(slideLeftOut);
            riddleFlipper.showNext();
        }
        else if((e2.getX() - e1.getX()) > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_TRESHOLD_VELOCITY)
        {
            Toast.makeText(getActivity(), "b", Toast.LENGTH_SHORT).show();
            riddleFlipper.setInAnimation(slideRightIn);
            riddleFlipper.setOutAnimation(slideRightOut);
            riddleFlipper.showNext();
        }

        return false;
    }

    @Override
    public void onLongPress(MotionEvent e)
    {

    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
            float distanceY)
    {
        return false;
    }

    @Override
    public void onShowPress(MotionEvent e)
    {

    }

    @Override
    public boolean onSingleTapUp(MotionEvent e)
    {
        return false;
    }

}

现在触摸事件被记录,但它永远不会到达onFling. 为什么???

谢谢

4

3 回答 3

2

如果您尝试覆盖onTouchEvent(MotionEvent event)您的活动detecture.onTouchEvent(event)从那里调用而不是使用OnTouchListener怎么办?

于 2013-08-17T13:35:46.457 回答
1
 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.charts,container,false);

    viewFlipper = (ViewFlipper) view.findViewById(R.id.viewFlipper);

    viewFlipper.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            switch (motionEvent.getAction())
            {
                case MotionEvent.ACTION_DOWN:
                    lastX = motionEvent.getX();
                    return true;

                case MotionEvent.ACTION_UP:
                    float currentX = motionEvent.getX();
                    if(lastX < currentX)
                    {
                        if(viewFlipper.getDisplayedChild() == 0) {
                            return true;
                        }
                        viewFlipper.showNext();                           
                    }else if (lastX > currentX){
                        if(viewFlipper.getDisplayedChild() == 1) {
                            return true;
                        }
                        viewFlipper.showPrevious();                            
                    }
                    return true;
            }
            return false;
        }
    });
}

这对我有用。

于 2014-07-17T16:57:44.897 回答
0

我在 onActivityCreated 方法中初始化我的事件处理程序时运气更好。试着把你的触摸听众放在那里。

于 2013-08-23T17:00:53.080 回答