将 LayoutManager 动态更改为 FlexboxLayoutManager 时崩溃
Crash when dynamically change LayoutManager to FlexboxLayoutManager
我必须同时使用 LinearLayoutManager
或 FlexbotLayoutManager
取决于孩子在 RecyclerView 中的大小。
当我动态地将 LinearLayoutManager
更改为 FlexbotLayoutManager
时:
recyclerView.layoutManager =
FlexibleFlexboxLayoutManager(context).apply {
flexWrap = FlexWrap.NOWRAP
flexDirection = FlexDirection.ROW
}
我遇到了这个错误:
java.lang.ClassCastException:
android.support.v7.widget.RecyclerView$LayoutParams cannot be cast to
com.google.android.flexbox.FlexItem
at com.google.android.flexbox.FlexboxHelper.calculateFlexLines(FlexboxHelper.java:439)
at com.google.android.flexbox.FlexboxHelper.calculateHorizontalFlexLines(FlexboxHelper.java:243)
at com.google.android.flexbox.FlexboxLayoutManager.updateFlexLines(FlexboxLayoutManager.java:955)
at com.google.android.flexbox.FlexboxLayoutManager.onLayoutChildren(FlexboxLayoutManager.java:731)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3924)
at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:3336)
at android.view.View.measure(View.java:22071)
如何修复?
问题是 FlexboxLayoutManager
仅覆盖 generateLayoutParams(Context c, AttributeSet attrs)
但不会覆盖 generateLayoutParams(ViewGroup.LayoutParams lp)
所以解决方案是实施该方法:
class SafeFlexboxLayoutManager : FlexboxLayoutManager {
constructor(context: Context) : super(context)
constructor(context: Context, flexDirection: Int) : super(context, flexDirection)
constructor(context: Context, flexDirection: Int, flexWrap: Int) : super(context, flexDirection, flexWrap)
constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int, defStyleRes: Int) : super(
context,
attrs,
defStyleAttr,
defStyleRes
)
override fun generateLayoutParams(lp: ViewGroup.LayoutParams): RecyclerView.LayoutParams {
return FlexboxLayoutManager.LayoutParams(lp)
}
}
我必须同时使用 LinearLayoutManager
或 FlexbotLayoutManager
取决于孩子在 RecyclerView 中的大小。
当我动态地将 LinearLayoutManager
更改为 FlexbotLayoutManager
时:
recyclerView.layoutManager =
FlexibleFlexboxLayoutManager(context).apply {
flexWrap = FlexWrap.NOWRAP
flexDirection = FlexDirection.ROW
}
我遇到了这个错误:
java.lang.ClassCastException: android.support.v7.widget.RecyclerView$LayoutParams cannot be cast to com.google.android.flexbox.FlexItem at com.google.android.flexbox.FlexboxHelper.calculateFlexLines(FlexboxHelper.java:439) at com.google.android.flexbox.FlexboxHelper.calculateHorizontalFlexLines(FlexboxHelper.java:243) at com.google.android.flexbox.FlexboxLayoutManager.updateFlexLines(FlexboxLayoutManager.java:955) at com.google.android.flexbox.FlexboxLayoutManager.onLayoutChildren(FlexboxLayoutManager.java:731) at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3924) at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:3336) at android.view.View.measure(View.java:22071)
如何修复?
问题是 FlexboxLayoutManager
仅覆盖 generateLayoutParams(Context c, AttributeSet attrs)
但不会覆盖 generateLayoutParams(ViewGroup.LayoutParams lp)
所以解决方案是实施该方法:
class SafeFlexboxLayoutManager : FlexboxLayoutManager {
constructor(context: Context) : super(context)
constructor(context: Context, flexDirection: Int) : super(context, flexDirection)
constructor(context: Context, flexDirection: Int, flexWrap: Int) : super(context, flexDirection, flexWrap)
constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int, defStyleRes: Int) : super(
context,
attrs,
defStyleAttr,
defStyleRes
)
override fun generateLayoutParams(lp: ViewGroup.LayoutParams): RecyclerView.LayoutParams {
return FlexboxLayoutManager.LayoutParams(lp)
}
}