簡體   English   中英

帶片段的RecycleView

[英]RecycleView with fragments

我正在嘗試使用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)

在您的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>

希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM