【问题标题】:Child views aren't clickable after scaling the rootview缩放根视图后,子视图不可点击
【发布时间】:2017-12-17 18:47:12
【问题描述】:

我正在缩放rootView,即LinearLayout 中的fragment ViewPager,但子视图不是clickable

这是rootView

public class CarouselLinearLayout extends LinearLayout {
private float scale = CarouselPagerAdapter.BIG_SCALE;

public CarouselLinearLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public CarouselLinearLayout(Context context) {
    super(context);
}

public void setScaleBoth(float scale) {
    this.scale = scale;
    this.invalidate();
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    // The main mechanism to display scale animation, you can customize it as your needs
    int w = this.getWidth();
    int h = this.getHeight();
    h = h - ((h / 2) / 2) / 2;
    canvas.scale(scale, scale, w / 2, h);// / 2
}

}

这是我缩放rootview的相关代码。

LinearLayout linearLayout = (LinearLayout) inflater.inflate(R.layout.pager_fragment_dashboard, container, false);
        CarouselLinearLayout root = (CarouselLinearLayout) linearLayout.findViewById(R.id.root_container);
        root.setScaleBoth(scale);

就是这个样子。

每个圆圈都是PagerView的页面。 1 - 2 - 3

第 2 页中的视图可点击,但第 1 页和第 3 页中的视图不可点击。我该如何解决这个问题?

【问题讨论】:

    标签: java android android-viewpager android-animation android-view


    【解决方案1】:

    您应该在您的 xml 中将特定的 Linear Layout 设置为 clickable,如下所示:

    android:clickable="true"
    

    例子:

                         <LinearLayout
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginLeft="5dp"
                            android:clickable="true"
                            android:gravity="center"
                            android:orientation="vertical">
                        </LinearLayout>
    

    【讨论】:

    • 这没有任何作用。如果我添加此属性,那么它会使页面不响应点击事件。
    【解决方案2】:

    如果您有一个包含三页的ViewPager,并且应该缩放中心一页,那么为此目的使用ViewPager.PageTransformer

    检查这个问题:ViewPager with mutiple visible children and selected bigger.

    基本上你覆盖transformPage(View view, float position) 并为中心页面进行转换。

    对于正常缩放,请始终在setScaleBoth() 中调用setScaleX()setScaleY(),而不是在onDraw() 中缩放画布。

    【讨论】:

    • 我试过这样做,但这搞砸了用户界面。我只在屏幕上看到一个圆圈。我想显示 3 个圆圈,中间的一个应该比左右大,还有轮播效果。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多