ViewPager with expandable child

后端 未结 1 1581
心在旅途
心在旅途 2020-12-15 14:02

I\'m trying to create collapsable calendar.

I\'m using custom calendarView in ViewPager, so when expand/collapse button pressed my calendarView animate collapsing al

相关标签:
1条回答
  • 2020-12-15 14:41

    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.

    0 讨论(0)
提交回复
热议问题