我需要以编程方式设置RecyclerView的顶部边距,但是我遇到了以下异常:
java.lang.RuntimeException: Unable to resume activity java.lang.ClassCastException: android.view.ViewGroup$LayoutParams cannot be cast to android.support.v7.widget.RecyclerView$LayoutParams
这是我的代码:
RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams)recyclerView.getLayoutParams();
int marginTopDp = (int)getResources().getDimension(R.dimen.margin);
int marginTopPx = (int) (marginTopDp * getResources().getDisplayMetrics().density + 0.5f);
layoutParams.setMargins(0, marginTopPx, 0, 0);
recyclerView.setLayoutParams(layoutParams);
如果我按照堆栈跟踪所示使用 ViewGroup.LayoutParams layoutParams = recyclerView.getLayoutParams
,那么我就无法调用 setMargin
方法,因为该方法对于 ViewGroup.LayoutParams
不存在。
任何帮助将不胜感激。
RecyclerView
的父ViewGroup
是什么? - Bryan Dunlapandroid.support.v4.widget.SwipeRefreshLayout
。 - VIN