我正在尝试使用fill_parent宽度片段实现一个垂直的RecycleList,其中包含另一个RecyclerView但是水平的。
我收到了这个错误:
android.view.InflateException: Binary XML file line #16: Binary XML file line #16: Error inflating class fragment
多数民众赞成是问题: 1)在列表中实现列表是一种好方法吗? 2)为什么我得到这个例外?
代码:
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)
答案 0 :(得分:2)
在您的RecyclerView适配器中:
//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
}
}
ViewHolderFragment类:
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;
}
}
在my_layout.xml中只有FrameLayout包含片段:
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"></FrameLayout>
希望这有帮助。