我有一个片段,其中有一个包含两个 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
. 为什么???
谢谢