Reputation: 1474
I'm trying to implement a vertical RecycleList with fill_parent width fragments which contains another RecyclerView but horizontal.
I'm getting this error:
android.view.InflateException: Binary XML file line #16: Binary XML file line #16: Error inflating class fragment
Thats is the question: 1) Is it a good way to implement a list inside a list? 2) Why am I getting this exception?
Code:
VerticalList (fragment_recommendation_layout.xml)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorAppBg"
android:orientation="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<android.support.v7.widget.RecyclerView
android:id="@+id/cardList"
tools:listitem="@layout/fragment_recommendation_vertical_list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Vertical list item (fragment_recommendation_vertical_list.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="18dp"
android:orientation="vertical">
<TextView
android:id="@+id/rowTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="18dp"
android:textSize="20dp"
android:text="Row Title" />
<fragment
android:id="@+id/horizontal_fragment"
android:layout_width="match_parent"
android:name="br.com.leanworks.fiquepordentro.view.recommendations.RecommendationHorizontalFragment"
android:layout_height="259dp" />
</LinearLayout>
Vertical adapter(RecommendationVerticalListViewAdapter.java)
@Override
public RecommendationItemViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
View itemView;
RecommendationItemViewHolder recommendationItemViewHolder;
itemView = LayoutInflater.
from(viewGroup.getContext()).
inflate(R.layout.fragment_recommendation_vertical_list, viewGroup, false);
recommendationItemViewHolder = new RecommendationItemViewHolder(itemView);
return recommendationItemViewHolder;
}
Exception:
10-18 06:08:12.936 12006-12006/br.com.leanworks.fiquepordentro E/AndroidRuntime: FATAL EXCEPTION: main
Process: br.com.leanworks.fiquepordentro, PID: 12006
android.view.InflateException: Binary XML file line #16: Binary XML file line #16: Error inflating class fragment
at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at br.com.leanworks.fiquepordentro.view.recommendations.RecommendationVerticalListViewAdapter.onCreateViewHolder(RecommendationVerticalListViewAdapter.java:78)
at br.com.leanworks.fiquepordentro.view.recommendations.RecommendationVerticalListViewAdapter.onCreateViewHolder(RecommendationVerticalListViewAdapter.java:25)
at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:5833)
Upvotes: 0
Views: 1027
Reputation: 276
In your RecyclerView's adapter:
//where FRAGMENT is your int id
@Override
public int getItemViewType(int position) {
if (listItems.get(position) instanceof MyClass)
return FRAGMENT;
...
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
RecyclerView.ViewHolder viewHolder = null;
LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
switch (viewType) {
case FRAGMENT:
View fragView = inflater.inflate(R.layout.my_layout, viewGroup, false);
viewHolder = new ViewHolderFragment(fragView);
break;
...
}
return viewHolder;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
switch (viewHolder.getItemViewType()) {
case FRAGMENT:
final ViewHolderFragment holder = (ViewHolderFragment) viewHolder;
//bad idea
}
}
The ViewHolderFragment class:
public class ViewHolderFragment extends RecyclerView.ViewHolder {
//framelayout to contain the fragment
private FrameLayout frameLayout;
public ViewHolderFragment(View v) {
super(v);
frameLayout = (FrameLayout) v.findViewById(R.id.container);
}
public FrameLayout getFrameLayout() {
return frameLayout;
}
public void setFrameLayout(FrameLayout frameLayout) {
this.frameLayout = frameLayout;
}
}
In your my_layout.xml is only the FrameLayout to contain the fragment:
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"></FrameLayout>
Hope this helps.
Upvotes: 2