How to wrap height of Android ViewPager2 to height of current item?(如何将 Android ViewPager2 的高度包装到当前项目的高度?)
问题描述
这个问题是针对新的 ViewPager2 类的.
This question is for the new ViewPager2 class.
有一个 旧 ViewPager 的类似问题,但解决方案需要扩展 ViewPager.但是,ViewPager2 是最终版本,因此无法扩展.
There is a similar question for the old ViewPager, but the solution requires extending ViewPager. However, ViewPager2 is final so cannot be extended.
在我的情况下,我有一个包含三个不同片段的 ViewPager2.其中一个片段比其他两个片段高得多,这意味着当您滑动到其中一个较短的片段时,会有很多空白空间.那么如何有效地将 ViewPager2 包裹到当前 Fragment 的高度呢?
In my situation, I have a ViewPager2 that contains three different fragments. One of these fragments is much taller than the other two, which means when you swipe to one of the shorter fragments, there is a lot of empty space. So how do I effectively wrap the ViewPager2 to the height of the current fragment?
推荐答案
解决方案是在 ViewPager2
的每个片段中添加以下内容:
The solution is to add the following in each fragment that is part of the ViewPager2
:
override fun onResume() {
super.onResume()
binding.root.requestLayout()
}
binding.root
来自 ViewBinding/DataBinding 但它是布局的主要容器,即 ConstraintLayout
、LinearLayout
等.
binding.root
is from ViewBinding/DataBinding but it's is the main container of your layout, i.e. ConstraintLayout
, LinearLayout
, etc.
这篇关于如何将 Android ViewPager2 的高度包装到当前项目的高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何将 Android ViewPager2 的高度包装到当前项目的高度?


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