2016-08-21 65 views
3

爲了產生效果,我放大了導致子視圖在其父視圖外側的子視圖。我在子視圖中有一個按鈕,它在縮放之前工作,但在縮放之後不起作用。發生了什麼問題?見下圖:Android:父級以外的子視圖不響應點擊事件

button outside doesn't work

縮放孩子,我用這個代碼:

  childView.bringToFront(); 
      Animation a = new Animation() { 
       @Override 
       protected void applyTransformation(float t, Transformation trans) { 
        float scale = 1f * (1 - t) + SCALE_UP_FACTOR * t; 
        childView.setScaleX(scale); 
        childView.setScaleY(scale); 
       } 

       @Override 
       public boolean willChangeBounds() { 
        return true; 
       } 
      }; 
      a.setDuration(ANIM_DURATION); 
      a.setInterpolator(new Interpolator() { 
       @Override 
       public float getInterpolation(float t) { 
        t -= 1f; 
        return (t * t * t * t * t) + 1f; // (t-1)^5 + 1 
       } 
      }); 
      childView.startAnimation(a); 

父是ViewPager

 <ViewPager 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/invoice_list_view_pager" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="#f5f5f5" 
     android:layout_gravity="center" 
     android:clipChildren="false" 
     android:clipToPadding="false" 
     /> 
+0

請在您的活動或片段中提供您的代碼,並提供您的佈局的xml –

+0

如果在縮放視圖時點擊其原始(未縮放)位置,該按鈕是否會響應? – Barend

+0

@Barend正如我在提到的問題:「它在縮放之前工作,但縮放後不起作用」 – Mneckoee

回答

1

這應該做的伎倆:

final View grandParent = (View) childView.getParent().getParent(); 
grandParent.post(new Runnable() { 
    public void run() { 
     Rect offsetViewBounds = new Rect(); 
     childView.getHitRect(offsetViewBounds); 

     // After scaling you probably want to append your view to the new size. 
     // in your particular case it probably could be only offsetViewBounds.right: 
     // (animDistance - int value, which you could calculate from your scale logic) 
     offsetViewBounds.right = offsetViewBounds.right + animDistance; 

     // calculates the relative coordinates to the parent 
     ((ViewGroup)parent).offsetDescendantRectToMyCoords(childView, offsetViewBounds); 
     grandParent.setTouchDelegate(new TouchDelegate(offsetViewBounds, childView)); 
    } 
}); 

雖然我不知道它是否會與Animation工作,但由於縮放你可以使用類似的東西來代替:

float scale = ...; // your scale logic 

ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(childView, 
     PropertyValuesHolder.ofFloat("scaleX", scale), 
     PropertyValuesHolder.ofFloat("scaleY", scale)); 
animator.setDuration(ANIM_DURATION); 
animator.start(); 

並注意行android:clipChildren="false"對XML文件的父視圖。

+0

這不起作用,因爲即使你使用觸摸代表,也不能超出具有可觸摸區域的父視圖的邊界。這意味着父母如果在其視線範圍外完成了點擊操作,父母不會註冊,因此無法將其轉發給按鈕。 – Csharpest

+1

@Csharpest在這種情況下,你總是可以致電盛大父母,而不是父母的: '''最後查看父=(查看)childView.getParent()的getParent();''' 我測試了它。有用。改變了我的答案。 – Sergey

+0

該死的,我不知道,我錯過了。你甚至可以調用你的父變量「grandParent」,然後:P。感謝信息 – Csharpest