简体   繁体   中英

android.support.v7.widget.GridLayoutManager$LayoutParams cannot be cast to android.widget.LinearLayout$LayoutParams

activitymain.xml

<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>

MainActivity.java

 actionWithAdherence=(RecyclerView) findViewById(R.id.action_with_adherence);
        //second parameter 3 represents it has 3 column
        //actionWithAdherence.setLayoutManager(new LinearLayoutManager(this));
        actionWithAdherence.setLayoutManager(new GridLayoutManager(this, 3));

    actionWithAdherence.setAdapter(new DashboardAdherence(this, actionListWithAdherence));

calling the adapter in this class.

Adapter 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>

Adapter onBindAdaper() :

  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);

        }

Issue:

Since in my main activity I am setting the view to the gridlayout but in the adapter using the linear layout. So Android is not able to cast the linear layout to the grid layout. THat causes the exception.

Is there any way I can able to cast the layout?

Use Grid Layout manager params

GridLayoutManager.LayoutParams layoutParams
                = (GridLayoutManager.LayoutParams) adherenceHolder.adherenceSection.getLayoutParams();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

Related Question java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.support.v7.widget.RecyclerView$LayoutParams ClassCastException : java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.support.v4.widget.DrawerLayout ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to com.android.internal.widget.ActionBarOverlayLayout$LayoutParams java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.widget.LinearLayout$LayoutParams java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.support.constraint.ConstraintLayout$LayoutParams Android java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.FrameLayout java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.support.v7.widget.RecyclerView$LayoutParams java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.support.v7.widget.Toolbar
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM