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

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

滾動(dòng)視圖觸摸處理中的HorizontalScrollView

滾動(dòng)視圖觸摸處理中的HorizontalScrollView

森林海 2019-06-25 11:27:18
滾動(dòng)視圖觸摸處理中的HorizontalScrollView我有一個(gè)滾動(dòng)視圖圍繞我的整個(gè)布局,使整個(gè)屏幕是可滾動(dòng)的。我在這個(gè)ScrollView中擁有的第一個(gè)元素是HorizontalScrollView塊,它具有可以水平滾動(dòng)的功能。我在水平滾動(dòng)視圖中添加了一個(gè)ontouch偵聽器,以處理觸摸事件,并強(qiáng)制視圖“快照”到action_up事件上最接近的圖像。所以我想要的效果就像股票Android的主屏幕,你可以從一個(gè)屏幕滾動(dòng)到另一個(gè)屏幕,當(dāng)你舉起手指的時(shí)候它會(huì)點(diǎn)擊到一個(gè)屏幕上。這一切都很好,除了一個(gè)問題:我需要從左到右滑動(dòng)幾乎完全水平,一個(gè)action_up永遠(yuǎn)注冊(cè)。如果我最不垂直地滑動(dòng)(我認(rèn)為很多人傾向于在手機(jī)上來回滑動(dòng)),我將收到action_Cancel而不是action_up。我的理論是,這是因?yàn)樗綕L動(dòng)視圖在滾動(dòng)視圖中,而滾動(dòng)視圖正在劫持垂直觸摸以允許垂直滾動(dòng)。如何從水平滾動(dòng)視圖中禁用滾動(dòng)視圖的觸摸事件,但仍然允許在滾動(dòng)視圖的其他地方正常垂直滾動(dòng)?下面是我的代碼示例: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個(gè)贊

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

下面是包含HorizontalScrollView的滾動(dòng)視圖類的代碼:

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


查看完整回答
反對(duì) 回復(fù) 2019-06-25
?
慕無忌1623718

TA貢獻(xiàn)1744條經(jīng)驗(yàn) 獲得超4個(gè)贊

我簡(jiǎn)化了代碼(不需要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);
????}}


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

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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