ViewPager with expandable child(带有可扩展子项的 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
基础教程推荐
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01