【问题标题】:Android: child view outside of its parent doesn't respond to click eventsAndroid:其父级之外的子视图不响应点击事件
【发布时间】:2016-08-21 06:26:26
【问题描述】:

为了产生效果,我放大了子视图,导致子视图超出其父视图。我在子视图中有一个按钮,它在缩放之前有效,但在缩放之后不起作用。出了什么问题?见下图:

对于缩放孩子,我使用以下代码:

            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"
        />

【问题讨论】:

  • 请在您的活动或片段中提供您的代码以及您的布局的xml
  • 缩放视图时,如果您点击其原始(未缩放)位置,按钮是否响应?
  • @Barend 正如我在问题中提到的那样:“它在缩放之前有效,但在缩放之后不起作用”
  • @Barend 。我改变了图像
  • @Ya Hoo 你找到解决方案了吗?我遇到了同样的问题

标签: android parent-child android-view onclicklistener


【解决方案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();

并注意 XML 文件中父视图的 android:clipChildren="false" 行。

【讨论】:

  • 这是行不通的,因为即使你使用了触摸代理,你也不能超过具有可触摸区域的父视图的边界。这意味着如果在其视图边界之外完成点击,则父级不会注册点击,因此无法转发到按钮。
  • @Csharpest 在这种情况下,您总是可以调用祖父母而不是父母:final View parent = (View) childView.getParent().getParent(); 我测试了它。有用。在我的回答中改变了它。
  • 该死,我不知道,我错过了。您甚至可以将父变量称为“grandParent”,然后:P。感谢您的信息
  • @Csharpest 好点,改变了我的答案,现在是 grandParent :) 如果我的答案有用,您可以将其标记为有用 :)
  • 我如何将其标记为有用 :D 如果你的意思是“接受”,那么对不起,因为我不是问题的作者
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多