ViewPager with expandable child
Asked Answered
N

1

5

I'm trying to create collapsable calendar.

I'm using custom calendarView in ViewPager, so when expand/collapse button pressed my calendarView animate collapsing all calendar weeks except only one.

Goal is to swipe month when expanded (which works as i want) and swipe weeks when collapsed.

Below calendarView is ListView with some events, when user collapse calendar - listView height must change.

Problem is when i trying to collapse calendarView in ViewPager, he doesn't change height.

calendarView is a LinearLayout with child views, when it is not in ViewPager, it animating and changing size.

I'm using little snippet to set ViewPager height to it children

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

    int height = 0;
    for(int i = 0; i < getChildCount(); i++) {
        View child = getChildAt(i);
        child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
        int h = child.getMeasuredHeight();
        if(h > height) height = h;
    }

    heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);

    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}

I'm tried to override onMeasure on calendarView, used different animations, tried to change ViewPager layout directly but no luck.

Please help me with this problem, maybe there is a tip for it.

I found this, this, this, and a lot more questions

My question is very similar to this question

Nog answered 4/9, 2014 at 20:6 Comment(0)
N
9

So finally i figured out how to resolve my problem, maybe it will be helpfull.

here is my PagerAdapter:

class CustomAdapter extends PagerAdapter {

    private View mCurrentView;

    ...

    // Saving current active item
    @Override
    public void setPrimaryItem(ViewGroup container, int position, Object object) {
        mCurrentView = (View) object;
    }

     public View getCurrentItem() {
        return mCurrentView;
    }

}

And this is my ViewPager class

class CustomViewPager extends ViewPager {

    private CustomViewPagerAdapter mAdapter;

    ...

    // Set ViewPager height to currentItem
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        int height = 0;
        View v = mAdapter.getCurrentItem();
        if(v != null) {
            v.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
            height = v.getMeasuredHeight();
        }

        heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}

With this i can collapse/expand ViewPager childs and it will be measure its height correctly, and it measures when page is changed with a little delay.

Nog answered 7/9, 2014 at 12:48 Comment(6)
Thanks a lot, your solution really did help. Modified it a bit to work with FragmentPagerAdpter as well.Convent
Hey man i want to try your solution but i m wondering how to use your adapter , ThanksThiele
Use your pager adapter as always just add code that i provided above.Nog
@MonicaM Can you share how did you do it with FragmentPagerAdapter please.Glycerol
@John61590 I skipped that code, you must initialize it by yourself.Nog
@Nog you can't really pass it into the ViewPager. It is kind of awkward. So, I don't know what you are trying to do.Feverwort

© 2022 - 2024 — McMap. All rights reserved.