Is there an example of how to use a TouchDelegate in Android to increase the size of a view's click target?

后端 未结 11 566
误落风尘
误落风尘 2020-11-28 19:01

My understanding is that when you have a view that\'s too small to easily touch, you\'re supposed to use a TouchDelegate to increase the clickable region for that view.

11条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-28 19:17

    emmby's approch didn't work for me but after a little changes it did:

    private void initApplyButtonOnClick() {
        mApplyButton.setOnClickListener(onApplyClickListener);
        final View parent = (View)mApplyButton.getParent();
        parent.post(new Runnable() {
    
            @Override
            public void run() {
                final Rect hitRect = new Rect();
                parent.getHitRect(hitRect);
                hitRect.right = hitRect.right - hitRect.left;
                hitRect.bottom = hitRect.bottom - hitRect.top;
                hitRect.top = 0;
                hitRect.left = 0;
                parent.setTouchDelegate(new TouchDelegate(hitRect , mApplyButton));         
            }
        });
    }
    

    Maybe it can save someone's time

提交回复
热议问题