第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

滾動視圖觸摸處理中的HorizontalScrollView

滾動視圖觸摸處理中的HorizontalScrollView

森林海 2019-06-25 11:27:18
滾動視圖觸摸處理中的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個贊

我想明白了。在我的ScrollView上,我需要覆蓋onInterceptTouchEvent方法,以便只在Y運(yùn)動大于X運(yùn)動時攔截觸摸事件。ScrollView的默認(rèn)行為似乎是在有Y運(yùn)動時攔截觸摸事件。因此,通過修復(fù),ScrollView只會在用戶有意向Y方向滾動,并且在這種情況下將action_Cancel傳遞給子用戶時,才會攔截該事件。

下面是包含HorizontalScrollView的滾動視圖類的代碼:

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);
        }
    }}


查看完整回答
反對 回復(fù) 2019-06-25
?
慕無忌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);
????}}


查看完整回答
反對 回復(fù) 2019-06-25
  • 3 回答
  • 0 關(guān)注
  • 576 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號