ViewPager与可扩展的孩子孩子、ViewPager

2023-09-06 13:23:41 作者:飘児づ

我想创建可折叠的日历。

I'm trying to create collapsable calendar.

我使用的是自定义的calendarView在ViewPager,所以当展开/折叠按钮pressed我calendarView动画折叠所有日历周,除了唯一的一个。

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.

下面calendarView是ListView的一些事件,当用户倒塌日历 - ListView的高度必须修改

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

问题是当我试图瓦解calendarView在ViewPager,他并没有改变高度。

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

calendarView与孩子意见的LinearLayout,当它不ViewPager,它的动画和大小的改变。

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

我用小片段设置ViewPager高度回事儿

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

我试图覆盖onMeasure上calendarView,使用不同的动画,试图直接更改ViewPager布局,但没有运气。

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.

我发现this, 这,this,和许多其他问题。

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

我的问题是非常相似的this问题

My question is very similar to this question

推荐答案

所以最后我想通了,如何解决我的问题,也许这将是有益的。

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

这是我的PagerAdapter:

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

}

这是我ViewPager类

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

有了这个,我可以折叠/展开ViewPager孩子的,这将是正确测量它的高度,它的措施时,页面被改变了小的延迟。

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.