2014-09-04 66 views
4

我正在尝试创建可折叠的日历。ViewPager with expandable child

我在ViewPager中使用自定义的calendarView,所以当展开/折叠按钮按下我的calendarView动画时,折叠所有日历周(除了只有一个)。

目标是在扩展时滑动月份(按照我的需要工作),并在折叠时刷卡几周。

在calendarView下面是ListView的一些事件,当用户折叠日历时--listView的高度必须改变。

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

calendarView是一个带有子视图的LinearLayout,当它不在ViewPager中时,它会动画并更改大小。

我使用小片段来ViewPager高度设置为它的孩子

@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布局,但没有运气。

请帮我解决这个问题,也许有一个提示吧。

我发现thisthisthis,以及更多的问题

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

回答

6

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

这里是我的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类

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孩子的,这将是正确测量它的高度,它的措施当页面稍微延迟改变时。

+0

非常感谢,您的解决方案确实有帮助。修改它有点与FragmentPagerAdpter一起工作。 – 2015-07-20 06:04:12

+0

嘿,我想尝试你的解决方案,但我想知道如何使用你的适配器,谢谢 – Tony 2016-02-24 14:26:00

+0

一如既往地使用你的寻呼机适配器只需添加我上面提供的代码。 – Jonez 2016-02-25 16:16:25