I was in the process of changing an Activity into a Fragment and got the following error as soon as I inflated the RecyclerView.
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
----> View rootView = inflater.inflate(R.layout.layout_containing_recyclerview,
container, false);
java.lang.IllegalStateException: RecyclerView has no LayoutManager
Before I changed my Activity to a Fragment the inflate went just fine.
Some further research showed that removing all my child elements from the recyclerview layout helped solve the problem. However I do not understand why that would change anything and why it did work work with an activity before.
WORKS
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/my_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:scrollbars="vertical" >
</android.support.v7.widget.RecyclerView>
DOES NOT WORK
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/my_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:scrollbars="vertical" >
<View
android:id="@+id/randomview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
</android.support.v7.widget.RecyclerView>
Anything I am missing here?
This question is quite old but for the benefit of people still facing this issue I thought I would leave an answer. I am just learning Android development and this issue was frustrating. Later on I found out that you are not supposed to add any views or view groups under RecyclerView
. The layout that you want to appear under the RecyclerView
needs to be defined separately in an independent resource file.
Then in the adapter implementation (the one that extends RecyclerView.Adapter<>
, in the overridden method public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
when you inflate the layout for the view (this is inflating the layout for each item of the recycler view), you can specify the above separate layout that you created. Android then inflates this layout for every item that needs to be added to the recycler view.
This article helped me see this clearly.
RecyclerView
is a ViewGroup
that requires LayoutManager
that will do the layouting of its children. In your case RecyclerView
needs the child view to be passed to the LayoutManager
, but I'm not sure if it is possible to pass xml reference of View to LayoutManager
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With