滾動視圖觸摸處理中的HorizontalScrollView我有一個滾動視圖圍繞我的整個布局,使整個屏幕是可滾動的。我在這個ScrollView中擁有的第一個元素是HorizontalScrollView塊,它具有可以水平滾動的功能。我在水平滾動視圖中添加了一個ontouch偵聽器,以處理觸摸事件,并強(qiáng)制視圖“快照”到action_up事件上最接近的圖像。所以我想要的效果就像股票Android的主屏幕,你可以從一個屏幕滾動到另一個屏幕,當(dāng)你舉起手指的時候它會點(diǎn)擊到一個屏幕上。這一切都很好,除了一個問題:我需要從左到右滑動幾乎完全水平,一個action_up永遠(yuǎn)注冊。如果我最不垂直地滑動(我認(rèn)為很多人傾向于在手機(jī)上來回滑動),我將收到action_Cancel而不是action_up。我的理論是,這是因?yàn)樗綕L動視圖在滾動視圖中,而滾動視圖正在劫持垂直觸摸以允許垂直滾動。如何從水平滾動視圖中禁用滾動視圖的觸摸事件,但仍然允許在滾動視圖的其他地方正常垂直滾動?下面是我的代碼示例:public class HomeFeatureLayout extends HorizontalScrollView {private ArrayList<ListItem> items = null;
private GestureDetector gestureDetector;View.OnTouchListener gestureListener;private static final int SWIPE_MIN_DISTANCE = 5;
private static final int SWIPE_THRESHOLD_VELOCITY = 300;private int activeFeature = 0;
public HomeFeatureLayout(Context context, ArrayList<ListItem> items){
super(context);
setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
setFadingEdgeLength(0);
this.setHorizontalScrollBarEnabled(false);
this.setVerticalScrollBarEnabled(false);
LinearLayout internalWrapper = new LinearLayout(context);
internalWrapper.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
internalWrapper.setOrientation(LinearLayout.HORIZONTAL);
addView(internalWrapper);
this.items = items;
for(int i = 0; i< items.size();i++){
LinearLayout featureLayout = (LinearLayout) View.inflate(this.getContext(),R.layout.homefeature,null);
TextView header = (TextView) featureLayout.findViewById(R.id.featureheader);
ImageView image = (ImageView) featureLayout.findViewById(R.id.featureimage);
TextView title = (TextView) featureLayout.findViewById(R.id.featuretitle);
3 回答

叮當(dāng)貓咪
TA貢獻(xiàn)1776條經(jīng)驗(yàn) 獲得超12個贊
public class CustomScrollView extends ScrollView { private GestureDetector mGestureDetector; public CustomScrollView(Context context, AttributeSet attrs) { super(context, attrs); mGestureDetector = new GestureDetector(context, new YScrollDetector()); setFadingEdgeLength(0); } @Override public boolean onInterceptTouchEvent(MotionEvent ev) { return super.onInterceptTouchEvent(ev) && mGestureDetector.onTouchEvent(ev); } // Return false if we're scrolling in the x direction class YScrollDetector extends SimpleOnGestureListener { @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { return Math.abs(distanceY) > Math.abs(distanceX); } }}

慕無忌1623718
TA貢獻(xiàn)1744條經(jīng)驗(yàn) 獲得超4個贊
我簡化了代碼(不需要GestureDetector)達(dá)到同樣的效果:
public?class?VerticalScrollView?extends?ScrollView?{ ????private?float?xDistance,?yDistance,?lastX,?lastY; ????public?VerticalScrollView(Context?context,?AttributeSet?attrs)?{ ????????super(context,?attrs); ????} ????@Override ????public?boolean?onInterceptTouchEvent(MotionEvent?ev)?{ ????????switch?(ev.getAction())?{ ????????????case?MotionEvent.ACTION_DOWN: ????????????????xDistance?=?yDistance?=?0f; ????????????????lastX?=?ev.getX(); ????????????????lastY?=?ev.getY(); ????????????????break; ????????????case?MotionEvent.ACTION_MOVE: ????????????????final?float?curX?=?ev.getX(); ????????????????final?float?curY?=?ev.getY(); ????????????????xDistance?+=?Math.abs(curX?-?lastX); ????????????????yDistance?+=?Math.abs(curY?-?lastY); ????????????????lastX?=?curX; ????????????????lastY?=?curY; ????????????????if(xDistance?>?yDistance) ????????????????????return?false; ????????} ????????return?super.onInterceptTouchEvent(ev); ????}}
- 3 回答
- 0 關(guān)注
- 576 瀏覽
添加回答
舉報
0/150
提交
取消