带有可扩展子项的 ViewPager

ViewPager with expandable child(带有可扩展子项的 ViewPager)

本文介绍了带有可扩展子项的 ViewPager的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建可折叠的日历.

I'm trying to create collapsable calendar.

我在 ViewPager 中使用自定义的 calendarView,所以当按下展开/折叠按钮时,我的 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.

问题是当我试图在 ViewPager 中折叠 calendarView 时,他没有改变高度.

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

我试图在 calendarView 上覆盖 onMeasure,使用不同的动画,尝试直接更改 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,还有更多问题

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

我的问题与 这个问题

推荐答案

所以我终于想出了如何解决我的问题,也许会有所帮助.

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.

这篇关于带有可扩展子项的 ViewPager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:带有可扩展子项的 ViewPager

基础教程推荐