前置条件
AndroidManifest.xml
<application
...
android:resizeableActivity="true" />
//折叠屏适配核心依赖库
implementation "androidx.window:window:1.1.0-alpha02"
implementation "androidx.window:window:1.1.0-alpha02"
implementation "androidx.window:window-java:1.1.0-alpha02"
代码实现
/**
* 折叠屏状态
*/
enum class FoldState {
//折叠状态
FOLD_NORMAL,
//展开状态
FLAT_NORMAL,
//书本模式
FLAT_BOOK,
//桌面模式
FLAT_TABLE_TOP
}
/**
* 窗口信息适配器
*/
private val mWindowInfoAdapter by lazy {
WindowInfoTrackerCallbackAdapter(WindowInfoTracker.getOrCreate(this))
}
/**
* 折叠状态回调
*/
private val mFoldBack by lazy {
Consumer<WindowLayoutInfo>{
val state= FoldStateManager.covertFoldState(
it,isFoldSpecial()
)
if(mViewModel.foldState.value!=state){
mViewModel.foldState.value=state
mViewModel.isFoldState.value=state== FoldState.FOLD_NORMAL
}
}
}
/**
* 添加回调
*/
override fun onStart() {
super.onStart()
mWindowInfoAdapter.addWindowLayoutInfoListener(
this,Runnable::run,mFoldBack
)
}
/**
* 移除回调
*/
override fun onStop() {
super.onStop()
mWindowInfoAdapter.removeWindowLayoutInfoListener(
mFoldBack
)
}
object FoldStateManager {
/**
* 折叠屏状态
*/
@JvmStatic
var pFoldState: FoldState = FoldState.FOLD_NORMAL
/**
* 是否是折叠状态
*/
@JvmStatic
val isFoldState:Boolean
get() {
return pFoldState== FoldState.FOLD_NORMAL
}
/**
* @param windowLayoutInfo 当系统传递的折叠信息为空的时候,默认是折叠状态,即小屏状态
* @param isSpecial 特殊模式,则会返回书本和桌面模式,默认不返回,只返回展开状态
*/
fun covertFoldState(
windowLayoutInfo: WindowLayoutInfo?,
isSpecial: Boolean = false
): FoldState {
val displayFeatures = windowLayoutInfo?.displayFeatures
var foldState = FoldState.FOLD_NORMAL
//数据不为空的时候
if (!displayFeatures.isNullOrEmpty()) {
for (display in displayFeatures) {
if (display is FoldingFeature) {
foldState =
(display.state == FoldingFeature.State.FLAT || display.state == FoldingFeature.State.HALF_OPENED).flatHandle(
isSpecial,display
)
break
}
}
}
pFoldState=foldState
return foldState
}
private fun Boolean.flatHandle(isSpecial: Boolean, display: FoldingFeature): FoldState {
return if (this) {
isSpecial.specialHandle(display)
} else {
FoldState.FOLD_NORMAL
}
}
private fun Boolean.specialHandle(display: FoldingFeature): FoldState {
return if (!this) {
FoldState.FLAT_NORMAL
} else {
specialFlat(display)
}
}
private fun specialFlat(display: FoldingFeature): FoldState {
return when {
isBookState(display) -> {
FoldState.FLAT_BOOK
}
isTableTopState(display) -> {
FoldState.FLAT_TABLE_TOP
}
isSeparating(display) -> {
if (display.orientation == FoldingFeature.Orientation.HORIZONTAL) {
FoldState.FLAT_TABLE_TOP
} else {
FoldState.FLAT_BOOK
}
}
else -> {
FoldState.FLAT_NORMAL
}
}
}
/**
* 折叠动作
*/
private fun isSeparating(foldingFeature: FoldingFeature?): Boolean {
return foldingFeature != null && foldingFeature.state == FoldingFeature.State.FLAT && foldingFeature.isSeparating
}
/**
* book状态
*/
private fun isBookState(feature: FoldingFeature?): Boolean {
return feature != null && feature.state == FoldingFeature.State.HALF_OPENED && feature.orientation == FoldingFeature.Orientation.VERTICAL
}
/**
* tableTop状态
*/
private fun isTableTopState(feature: FoldingFeature?): Boolean {
return feature != null && feature.state == FoldingFeature.State.HALF_OPENED && feature.orientation == FoldingFeature.Orientation.HORIZONTAL
}
}
解决华为折叠屏获取状态异常问题
//华为maven库
maven { url 'https://developer.huawei.com/repo/' }
implementation "com.huawei.ui.rspuikit:hwrsplinearlayout-androidn:1.0.0.8"
/**
* 是否是展开状态
*/
val isFold= HwRspUtils.checkDeviceState(this)