在做一个流式布局搜索历史UI,当我在给RecyclerView添加HeaderView的时候,报如下异常
ClassCastException: android.support.v7.widget.RecyclerView$LayoutParams cannot be cast to com.google.android.flexbox.FlexItem
就是说,我们添加的HeaderView,无法转换成FlexlItem,但是为了复用,踩坑吧!百度引擎是搜不到,只能起飞了,Google 一下就找到了解决方法,实现自己的 FlexboxLayoutManager
/**
* Created by dzh on 02.23.023.
*/
public class MyFlexboxLayoutManager extends FlexboxLayoutManager{
public MyFlexboxLayoutManager(Context context) {
super(context);
}
public MyFlexboxLayoutManager(Context context, int flexDirection) {
super(context, flexDirection);
}
public MyFlexboxLayoutManager(Context context, int flexDirection, int flexWrap) {
super(context, flexDirection, flexWrap);
}
/**
* 将LayoutParams转换成新的FlexboxLayoutManager.LayoutParams
*/
@Override
public RecyclerView.LayoutParams generateLayoutParams(ViewGroup.LayoutParams lp) {
if (lp instanceof RecyclerView.LayoutParams) {
return new FlexboxLayoutManager.LayoutParams((RecyclerView.LayoutParams) lp);
} else if (lp instanceof ViewGroup.MarginLayoutParams) {
return new FlexboxLayoutManager.LayoutParams((ViewGroup.MarginLayoutParams) lp);
} else {
return new FlexboxLayoutManager.LayoutParams(lp);
}
}
}