[英]Android touch events with multiple views(such as a ListView in a Viewflliper)
Android touch events with multiple views(such as a ListView in a Viewflipper) 具有多个视图的Android触摸事件(例如Viewflipper中的ListView)
When i put a ListView inside the FlingGallery, the FlingGallery can't work when i Fling my finger. 当我在FlingGallery中放入ListView时,当我用手指滑动时,FlingGallery无法工作。
Write an extension of ViewFlipper and for all of its children set custom touch listener, like that: 编写ViewFlipper的扩展,并为其所有子级设置自定义触摸侦听器,如下所示:
public void addView(View child, int index, android.view.ViewGroup.LayoutParams params) {
child.setOnTouchListener(new DragableViewTouchListener());
}
/**
* To organize horizontal paging every child should use this implementation of OnTouchListener.
*/
private class DragableViewTouchListener implements OnTouchListener {
private static final int DISTANCE_WHEN_DO_SCROLL = 5;
private float historyDistanceX = 0;
private float historyDistanceY = 0;
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_MOVE && event.getHistorySize() > 0) {
historyDistanceX = event.getHistoricalX(event.getHistorySize() - 1) - event.getX();
historyDistanceY = event.getHistoricalY(event.getHistorySize() - 1) - event.getY();
}
boolean actionUp = (event.getAction() == MotionEvent.ACTION_UP) || (event.getAction() == MotionEvent.ACTION_CANCEL);
if (actionUp) {
boolean horizontalStrongerThanVertical = Math.abs(historyDistanceX) > Math.abs(historyDistanceY);
if (Math.abs(historyDistanceX) > DISTANCE_WHEN_DO_SCROLL && horizontalStrongerThanVertical) {
onHorizontalScroll(historyDistanceX);
historyDistanceX = 0;
historyDistanceY = 0;
return true;
}
}
if (event.getAction() == MotionEvent.ACTION_DOWN) {
return true;
}
return false;
}
private void onHorizontalScroll(float distanceX) {
if (distanceX > 0) {
showNext();
}
else {
showPrevious();
}
}
};
The main idea is to separate horizontal motion events (dispatch them to ViewFlipper) and vertical motion events (dispatch them to child (to ListView)) 主要思想是将水平运动事件(将它们分发到ViewFlipper)和垂直运动事件(将它们分发给子级(到ListView))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.