Q:

android.support.v4.widget.NestedScrollView cannot be cast to android.support.v7.widget.RecyclerView

Good afternoon,
I am trying to create a fragment with a RecyclerView including items.
Above those items, I need to put a header with some filters.
Here is the fragment's xml (fragment_products_list) :
<?xml version="1.0" encoding="utf-8"?>

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="12sp"
            android:id="@+id/filters_frame">
            <include layout="@layout/expandable_filters"/>
        </RelativeLayout>

        <android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/recyclerview"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </LinearLayout>
</android.support.v4.widget.NestedScrollView>

Here is the OnCreateView in my Fragment java file (ListFragment.java) :
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    RecyclerView rv = (RecyclerView) inflater.inflate(
            R.layout.fragment_products_list, container, false);

    //Product filters
    expandFiltersButton = (ImageButton) getView().findViewById(R.id.expand_filters);
    expandableZone = (LinearLayout) getView().findViewById(R.id.expandable_zone);

    expandFiltersButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {

            if(expandableZone.isShown()){
                expandableZone.setVisibility(View.GONE);
                expandFiltersButton.setImageResource(R.drawable.ic_more_24dp);
            }else{
                expandableZone.setVisibility(View.VISIBLE);
                expandFiltersButton.setImageResource(R.drawable.ic_less_24dp);
            }
        }
    });

    setupRecyclerView(rv);

    mRecyclerView = rv;
    loadUpdates();
    return rv;
}

I have this error code when I am trying to run the app :
android.support.v4.widget.NestedScrollView cannot be cast to android.support.v7.widget.RecyclerView

What is the problem and how can I solve it?
Thanks for your help!

A:

Try this:
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(
            R.layout.fragment_products_list, container, false);

    //Product filters
     RecyclerView rv = (RecyclerView) view.findViewById(R.id.recyclerview);
    expandFiltersButton = (ImageButton) view.findViewById(R.id.expand_filters);
    expandableZone = (LinearLayout) view.findViewById(R.id.expandable_zone);

    expandFiltersButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {

            if(expandableZone.isShown()){
                expandableZone.setVisibility(View.GONE);
                expandFiltersButton.setImageResource(R.drawable.ic_more_24dp);
            }else{
                expandableZone.setVisibility(View.VISIBLE);
                expandFiltersButton.setImageResource(R.drawable.ic_less_24dp);
            }
        }
    });

    setupRecyclerView(rv);

    mRecyclerView = rv;
    loadUpdates();
    return view;
}

