简体   繁体   中英

Android - Child order in Custom ViewGroup

I'm writing a custom ViewGroup which is a simple combination of other views. 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 ?


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.

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.

Stupid mistake !!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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