无法将 LinearLayoutManager 转换为 GridLayoutManager
Unable to cast LinearLayoutManager to GridLayoutManager
if (mLayoutManager instanceof GridLayoutManager) {
((GridLayoutManager) mLayoutManager).setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
return listResultAdapter.isHeaderOrFooter(position) ? ((GridLayoutManager) mLayoutManager).getSpanCount() : 1;
}
});
}
上面的代码崩溃说 java.lang.ClassCastException: android.support.v7.widget.LinearLayoutManager cannot be cast to android.support.v7.widget.GridLayoutManager
我发现下面一行导致了崩溃
((GridLayoutManager) mLayoutManager).getSpanCount()
避免崩溃的解决方案是什么
mLayoutManager 声明为 LinearLayoutManager
您还应该在 getSpanSize
:
中进行 class 检查
public int getSpanSize(int position) {
return mLayoutManager instanceof GridLayoutManager && listResultAdapter.isHeaderOrFooter(position)
? ((GridLayoutManager) mLayoutManager).getSpanCount() : 1;
}
if (mLayoutManager instanceof GridLayoutManager) {
((GridLayoutManager) mLayoutManager).setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
@Override
public int getSpanSize(int position) {
return listResultAdapter.isHeaderOrFooter(position) ? ((GridLayoutManager) mLayoutManager).getSpanCount() : 1;
}
});
}
上面的代码崩溃说 java.lang.ClassCastException: android.support.v7.widget.LinearLayoutManager cannot be cast to android.support.v7.widget.GridLayoutManager
我发现下面一行导致了崩溃
((GridLayoutManager) mLayoutManager).getSpanCount()
避免崩溃的解决方案是什么
mLayoutManager 声明为 LinearLayoutManager
您还应该在 getSpanSize
:
public int getSpanSize(int position) {
return mLayoutManager instanceof GridLayoutManager && listResultAdapter.isHeaderOrFooter(position)
? ((GridLayoutManager) mLayoutManager).getSpanCount() : 1;
}