【问题标题】:ViewPager with expandable child带有可扩展子项的 ViewPager
【发布时间】:2014-10-29 17:07:20
【问题描述】:

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

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

目标是在展开时滑动月份(按我的意愿工作)并在折叠时滑动周。

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

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

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

我正在使用 little sn-p 将 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);
}

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

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

我找到了thisthisthis 以及更多问题

我的问题与this question 非常相似

【问题讨论】:

    标签: android android-viewpager height expandable


    【解决方案1】:

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

    这是我的 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 子,它会正确测量它的高度,它会测量页面何时更改,但会有一点延迟。

    【讨论】:

    • 非常感谢,您的解决方案确实有帮助。对它进行了一些修改以与 FragmentPagerAdpter 一起使用。
    • 嘿,我想试试你的解决方案,但我想知道如何使用你的适配器,谢谢
    • 一如既往地使用您的寻呼机适配器,只需添加我上面提供的代码。
    • @MonicaM 你能分享一下你是怎么用 FragmentPagerAdapter 做的吗?
    • @John61590 那段代码我跳过了,你必须自己初始化。
    猜你喜欢
    • 2015-12-15
    • 2015-10-16
    • 1970-01-01
    • 2015-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-18
    • 1970-01-01
    相关资源
    最近更新 更多