当前位置: 首页 > news >正文

Android-自适用高度的ViewPager

需求

在项目中,我们常常遇到需要动态调整 ViewPager 的高度,以适应其内容大小的需求。默认情况下,ViewPager 的高度是固定的,无法根据每个页面的内容高度进行调整。这会导致在内容高度不一致时,出现不必要的空白区域或者内容被裁剪的情况。为了解决这个问题,我们设计了一个 AutoHeightViewPager,能够根据当前显示页面的内容高度动态调整自身的高度,保证内容完整且没有多余的空白。

效果

在这里插入图片描述
在这里插入图片描述

实现思路

1. 动态高度调整

首先,我们需要一个自定义的 ViewPager 类 AutoHeightViewPager,这个类可以根据当前页面的内容高度来动态调整自身的高度。通过重写 onMeasure 方法,可以在滑动过程中动态计算页面的高度并调整布局。

2. 监听页面滑动事件

为了平滑过渡,我们需要监听页面的滑动过程,并计算滑动比例,将当前页面的高度与下一个页面的高度按比例过渡,实现平滑过渡效果。

3. 自定义 Adapter

自定义的 PagerAdapter 必须实现一个接口 AutoHeightPager,用于获取指定位置页面的 View,这样我们可以测量页面内容的高度。

实现代码

1. AutoHeightViewPager

package com.yxlh.androidxy.demo.ui.viewpager.vpimport android.content.Context
import android.util.AttributeSet
import android.view.View
import androidx.viewpager.widget.PagerAdapter
import androidx.viewpager.widget.ViewPagerinterface AutoHeightPager {fun getView(position: Int): View?
}class AutoHeightViewPager @JvmOverloads constructor(context: Context,attrs: AttributeSet? = null
) : ViewPager(context, attrs) {private var lastWidthMeasureSpec: Int = 0private var currentHeight: Int = 0private var lastPosition: Int = 0private var isScrolling: Boolean = falseinit {addOnPageChangeListener(object : SimpleOnPageChangeListener() {override fun onPageScrolled(position: Int,positionOffset: Float,positionOffsetPixels: Int) {if (positionOffset == 0f) {isScrolling = falserequestLayout()return}val srcPosition = if (position >= lastPosition) position else position + 1val destPosition = if (position >= lastPosition) position + 1 else positionval srcHeight = getViewHeight(srcPosition)val destHeight = getViewHeight(destPosition)currentHeight = (srcHeight + (destHeight - srcHeight) *if (position >= lastPosition) positionOffset else 1 - positionOffset).toInt()isScrolling = truerequestLayout()}override fun onPageScrollStateChanged(state: Int) {if (state == SCROLL_STATE_IDLE) {lastPosition = currentItem}}})}override fun setAdapter(adapter: PagerAdapter?) {require(adapter == null || adapter is AutoHeightPager) { "PagerAdapter must implement AutoHeightPager." }super.setAdapter(adapter)}private fun getViewHeight(position: Int): Int {val adapter = adapter as? AutoHeightPager ?: return 0return run {val view = adapter.getView(position) ?: return 0view.measure(lastWidthMeasureSpec,MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED))view.measuredHeight}}override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {lastWidthMeasureSpec = widthMeasureSpecvar heightSpec = heightMeasureSpecif (isScrolling) {heightSpec = MeasureSpec.makeMeasureSpec(currentHeight, MeasureSpec.EXACTLY)} else {getViewHeight(currentItem).takeIf { it > 0 }?.let {heightSpec = MeasureSpec.makeMeasureSpec(it, MeasureSpec.EXACTLY)}}super.onMeasure(widthMeasureSpec, heightSpec)}
}

2. AutoHeightPagerAdapter

package com.yxlh.androidxy.demo.ui.viewpagerimport android.view.View
import android.view.ViewGroup
import androidx.viewpager.widget.PagerAdapter
import com.yxlh.androidxy.demo.ui.viewpager.vp.AutoHeightPagerclass AutoHeightPagerAdapter : PagerAdapter(), AutoHeightPager {private val viewList = mutableListOf<View>()override fun instantiateItem(container: ViewGroup, position: Int): Any {val view = viewList[position]val parent = view.parent as? ViewGroupparent?.removeView(view)container.addView(view)return view}override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {container.removeView(`object` as View)}fun setViews(views: List<View>) {viewList.clear()viewList.addAll(views)notifyDataSetChanged()}override fun getView(position: Int): View? {return viewList.getOrNull(position)}override fun getCount(): Int {return viewList.size}override fun isViewFromObject(view: View, `object`: Any): Boolean {return view == `object`}
}

3. Activity 代码

package com.yxlh.androidxy.demo.ui.viewpagerimport android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import com.yxlh.androidxy.R
import com.yxlh.androidxy.demo.ui.viewpager.vp.AutoHeightViewPagerclass VpActivity : AppCompatActivity() {private var mAutoHeightVp: AutoHeightViewPager? = nullprivate var mAdapter: AutoHeightPagerAdapter? = nulloverride fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.activity_vp)val viewList: MutableList<View> = ArrayList()viewList.add(LayoutInflater.from(this).inflate(R.layout.view_demo_1, null))viewList.add(LayoutInflater.from(this).inflate(R.layout.view_demo_2, null))mAutoHeightVp = findViewById(R.id.viewpager)mAutoHeightVp?.setAdapter(AutoHeightPagerAdapter().also { mAdapter = it })mAdapter?.setViews(viewList)mAutoHeightVp?.setCurrentItem(1)}
}

4. 布局 XML

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"android:layout_width="match_parent"android:layout_height="match_parent"><androidx.core.widget.NestedScrollViewandroid:layout_width="match_parent"android:layout_height="wrap_content"app:layout_constraintTop_toTopOf="parent"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><com.yxlh.androidxy.demo.ui.viewpager.vp.AutoHeightViewPagerandroid:id="@+id/viewpager"android:layout_width="match_parent"android:layout_height="wrap_content" /><ImageViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:adjustViewBounds="true"android:scaleType="fitXY"android:src="@drawable/vp_content" /></LinearLayout></androidx.core.widget.NestedScrollView></androidx.constraintlayout.widget.ConstraintLayout>

总结

通过自定义 ViewPager 的动态高度适配功能,我们可以解决内容高度不一致导致的布局问题。这种方案可以适应不同页面内容的高度变化,实现平滑的过渡效果,非常适用于动态内容展示的场景。

http://www.lryc.cn/news/425087.html

相关文章:

  • 代码随想录day38|| 322零钱兑换 279完全平方数 139单词拆分
  • Cesium天空盒子(Skybox)制作(js代码)和显示
  • JAVA中的缓冲流BufferedInputStream
  • WindowContainerTransaction类详解(一)
  • 安装NFS扩展
  • 计算机网络——运输层(进程之间的通信、运输层端口,UDP与TCP、TCP详解)
  • 代码随想录算法训练营第一天 | 二分查找
  • python相关知识
  • Visual Studio 2022 LNK2001无法解析的外部符号 _wcscat_s 问题记录
  • Java高并发处理机制
  • 7 数据存储单位,整型、浮点型、字符型、布尔型数据类型,sizeof 运算符
  • 导游职业资格考试真题题库
  • 【Rust】使用开源项目搭建瓦片地图服务
  • 【面试宝典】mysql常见面试题总结(上)
  • 第1章 初识C语言
  • 【考研数学】定积分应用——旋转体体积的计算(一文以蔽之)
  • PHP移动端商城分销全平台全端同步使用
  • TLE8386-2EL:汽车级DC-DC转换器中文资料书
  • EasyRecovery17中文mac苹果电脑版数据恢复软件 永久免费破解版下载
  • Ubuntu 22.04 安装 VirtualBox7
  • NPM使用教程:从入门到精通
  • 模电实验3 - 单电源集成运放交流耦合放大器
  • 海对外经贸大学学报
  • 数字化营销在公域场景中的无限可能
  • 聚观早报 | 一加13配置细节曝光;谷歌首推人工智能手机
  • C++ 11相关新特性(lambda表达式与function包装器)
  • FastAPI部署大模型Llama 3.1
  • C++拾趣——编译器预处理宏__COUNTER__的应用场景
  • 使用HTML和cgi实现网页登录功能
  • Java流程控制01:用户交互Scanner