Java 无法将android.support.v7.widget.GridLayoutManager$LayoutParams强制转换为android.widget.LinearLayout$LayoutParams
activitymain.xmlJava 无法将android.support.v7.widget.GridLayoutManager$LayoutParams强制转换为android.widget.LinearLayout$LayoutParams,java,android,android-layout,android-linearlayout,grid-layout,Java,Android,Android Layout,Android Linearlayout,Grid Layout,activitymain.xml <android.support.v7.widget.RecyclerView android:background="@color/primary_text" android:layout_marginStart="@dimen/activity_horizontal_margin" an
<android.support.v7.widget.RecyclerView
android:background="@color/primary_text"
android:layout_marginStart="@dimen/activity_horizontal_margin"
android:layout_below="@+id/adherence_text"
android:id="@+id/action_with_adherence"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
在此类中调用适配器
适配器xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/adherence_section"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto">
<at.grabner.circleprogress.CircleProgressView
android:layout_gravity="center"
app:cpv_maxValue="100"
app:cpv_textSize="15dp"
app:cpv_textColor="@color/white"
app:cpv_unit="%"
app:cpv_unitSize="10dp"
app:cpv_unitColor="@color/white"
app:cpv_unitScale="1"
app:cpv_rimColor="@color/secondary_background"
app:cpv_rimWidth="10dp"
app:cpv_barWidth="4dp"
android:id="@+id/action_adherence"
android:layout_width="70dp"
android:layout_height="70dp" />
<TextView
android:layout_gravity="center"
android:id="@+id/action_name"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Small"
android:layout_marginTop="5dp"
android:text="Exercise"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
问题:
因为在我的主要活动中,我将视图设置为gridlayout,但在适配器中使用线性布局。因此Android无法将线性布局转换为网格布局。这导致了例外
有什么方法可以强制转换布局吗?使用网格布局管理器参数
GridLayoutManager.LayoutParams layoutParams
= (GridLayoutManager.LayoutParams) adherenceHolder.adherenceSection.getLayoutParams();
使用GridLayout.LayoutParams代替LinearLayout.LayoutParams已更改,但现在出现另一个错误“java.lang.ClassCastException:android.support.v7.widget.GridLayoutManager$LayoutParams不能强制转换为android.widget.GridLayout$LayoutParams”
if ((position+1)%3==0){
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)adherenceHolder.adherenceSection.getLayoutParams();
params.setMargins(0, 0, 0, 1);
adherenceHolder.adherenceSection.setLayoutParams(params);
}
else
{
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)adherenceHolder.adherenceSection.getLayoutParams();
params.setMargins(0, 0, 1, 1);
adherenceHolder.adherenceSection.setLayoutParams(params);
}
GridLayoutManager.LayoutParams layoutParams
= (GridLayoutManager.LayoutParams) adherenceHolder.adherenceSection.getLayoutParams();