简体   繁体   English

Android:其父视图之外的子视图不响应点击事件

[英]Android: child view outside of its parent doesn't respond to click events

For having effects I scale up the child view which cause child view to go out side of its parent view. 为了产生效果,我放大了子视图,使子视图超出其父视图的一侧。 I have a button in child view, it works before scaling but after scaling doesn't work. 我在子视图中有一个按钮,该按钮在缩放前有效,但在缩放后不起作用。 what is going wrong? 怎么了? see image below: 见下图:

外面的按钮不起作用

for scaling child I use this code: 为了缩放孩子,我使用以下代码:

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

the parent is a ViewPager : 父级是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"
        />

This should do the trick: 这应该可以解决问题:

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

Though I'm not sure whether it will work with Animation , but for scaling you could use something like that instead: 虽然我不确定它是否可以与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();

And pay attention to the line android:clipChildren="false" for your parent view in XML-file. 并注意XML文件中父视图的行android:clipChildren="false"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM