2016-10-18 16 views
0

私は、RecyclerViewではなく水平であるfill_parent幅の断片を持つ垂直RecycleListを実装しようとしています。 enter image description here断片化したRecycleView

私はこのエラーを取得してい

: 1)それは、リスト内のリストを実装するための良い方法です:

android.view.InflateException: Binary XML file line #16: Binary XML file line #16: Error inflating class fragment 

ザッツが問題でしょうか? 2)なぜこの例外が発生するのですか?

コード:あなたのRecyclerViewのアダプタで

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) 

答えて

2

//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; 
} 

}

0あなたのmy_layout.xmlで

は、フラグメントを含むことだけでframeLayoutです:

<FrameLayout 
    android:id="@+id/container" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"></FrameLayout> 

は、この情報がお役に立てば幸いです。

+0

ほとんどの場合、このコードで置き換えられるフラグメントは1つだけです。また、.replace()は、フラグメントではなくIDを要求します。 GetItemViewTypeとスイッチでオーバーライドを使用しませんでした。そこに問題はありますか?あなたの時間をありがとう! –

+0

いいえ。リサイクラービューには1つのタイプの行しか含まれていない場合、戻りタイプを切り替える必要はありません。申し訳ありませんが、私の悪い:holder.getFrameLayout()。getId()は仕事をする必要があります。 – elmontoya7

+0

お返事ありがとうございます。不幸にもそれはまだ動作しません。 :((FragmentActivity)mContext).getSupportFragmentManager()。beginTransaction()。replace(rssItemViewHolder.frameLayout.getId()、新しいRecommendationHorizo​​ntalFragment()、 "TAG")。commitAllowingStateLoss();間違った電話?私は1つのフラグメントだけが置き換えられるようになっています。 –

関連する問題