2016-08-21 560 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