简体   繁体   English

Android-自定义ViewGroup中的子订单

[英]Android - Child order in Custom ViewGroup

I'm writing a custom ViewGroup which is a simple combination of other views. 我正在编写一个自定义ViewGroup ,它是其他视图的简单组合。 However, the Views seem to be in arbitrary order. 但是,视图似乎是任意顺序的。

I know I can implement the getChildDrawingOrder() method but I was curious how the order of the Views was determined in the first place ? 我知道我可以实现getChildDrawingOrder()方法,但我很好奇如何首先确定视图的顺序?


UPDATE: This is a minimum implementation and it works but I don't know why the indicator is always on top no matter what I do 更新:这是一个最低限度的实现,并且可以运行,但是我不知道为什么无论我做什么指标总是总是最重要的

public class PaginatedGallery extends ViewGroup {

ViewPager mPager;
CirclePageIndicator mPagerIndicator;

public PaginatedGallery(Context context) {
    super(context);
    mPager = new ViewPager(context);
    mPagerIndicator = new CirclePageIndicator(context);
    addView(mPager);
    addView(mPagerIndicator);
}

public void setAdapter(PaginatedGalleryAdapter adapter) {
    mPager.setAdapter(adapter);
    mPagerIndicator.setViewPager(mPager);
}

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    final int count = getChildCount();

    for (int i = 0; i< count; i++ ) {
        getChildAt(i).layout(l, t, r, b);
    }

}   
}

I know I'm not implementing onMeasure() and onLayout() is just plain vanilla but I'm just curious as to why indicator is the first child and not the pager. 我知道我没有实现onMeasure()onLayout()只是普通的香草,但我只是好奇为什么指标是第一个孩子而不是寻呼机。

Figured it out. 弄清楚了。

The Views are simply called in the order that they were added to the parent. 视图按照添加到父级的顺序简单地调用。

In my case, one View was above the other on the Z-axis whereas I was thinking that it was above the other on the Y-axis but that's because the Pager has an empty space at the top where the Indicator was seemingly fitting. 在我的情况下,一个视图在Z轴上位于另一个视图上方,而我在想它在Y轴上在另一个视图上方,但这是因为Pager在顶部的空白处似乎指示器很合适。

Stupid mistake !! 愚蠢的错误!

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

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