使用多个视图在Recyclerview中扩充类碎片时出错

时间:2016-01-20 07:26:58

标签: android google-maps android-fragments

我有一个带有2个布局的Recyclerview,
一个是我的谷歌地图,
两个是关于我必须在地图中指出的额外细节。

我的适配器中的代码是这样的。

public class AdapterTrending extends RecyclerView.Adapter<AdapterTrending.MyTrendingHolder> {
private int viewType = 0;
private Context mContext;
private ArrayList<String> reviewTrending = new ArrayList<>();

public AdapterTrending(Context context, ArrayList<String> reviewTrending) {
    this.mContext = context;
    this.reviewTrending = reviewTrending;
}

@Override
public MyTrendingHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    int listViewItemType = getItemViewType(viewType);
        if (listViewItemType == 0) {
            return new MyGoogleMap(LayoutInflater.from(mContext).inflate(R.layout.custom_googlemap_layout, parent, false));
        } else {
            return new MyTrendings(LayoutInflater.from(mContext).inflate(R.layout.custom_trending_layout, parent, false));
        }
}

@Override
public void onBindViewHolder(MyTrendingHolder holder, int position) {

}

@Override
public int getItemCount() {
    return 10;
}

@Override
public int getItemViewType(int position) {
    if (position == 0) {
        viewType = 0;
    }
    if (position >= 1) {
        viewType = position;
    }
    return viewType;
}


class MyTrendings extends MyTrendingHolder {
    public MyTrendings(View itemView) {
        super(itemView);
    }
}

class MyGoogleMap extends MyTrendingHolder {
    public MyGoogleMap(View itemView) {
        super(itemView);
    }
}

class MyTrendingHolder extends RecyclerView.ViewHolder {
    public MyTrendingHolder(View itemView) {
        super(itemView);
    }
}
}

我有三个标签的tabpager 第一个标签是这个。
第二页和第三页什么都没有 当我滑到第三页并返回第二页时,
发生此错误。

 FATAL EXCEPTION: main
                                                         Process: com.toweelo, PID: 22695
                                                         android.view.InflateException: Binary XML file line #17: Error inflating class fragment
                                                             at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:770)
                                                             at android.view.LayoutInflater.rInflate(LayoutInflater.java:813)
                                                             at android.view.LayoutInflater.rInflate(LayoutInflater.java:821)
                                                             at android.view.LayoutInflater.rInflate(LayoutInflater.java:821)
                                                             at android.view.LayoutInflater.inflate(LayoutInflater.java:511)
                                                             at android.view.LayoutInflater.inflate(LayoutInflater.java:415)
                                                             at com.toweelo.adapter.AdapterTrending.onCreateViewHolder(AdapterTrending.java:31)
                                                             at com.toweelo.adapter.AdapterTrending.onCreateViewHolder(AdapterTrending.java:17)
                                                             at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:5228)
                                                             at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4453)
                                                             at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4363)
                                                             at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1961)
                                                             at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1370)
                                                             at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1333)
                                                             at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:562)
                                                             at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2900)
                                                             at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3071)
                                                             at android.view.View.layout(View.java:16542)
                                                             at android.view.ViewGroup.layout(ViewGroup.java:5302)
                                                             at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
                                                             at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
                                                             at android.view.View.layout(View.java:16542)
                                                             at android.view.ViewGroup.layout(ViewGroup.java:5302)
                                                             at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1627)
                                                             at android.view.View.layout(View.java:16542)
                                                             at android.view.ViewGroup.layout(ViewGroup.java:5302)
                                                             at android.widget.LinearLayout.setChildFrame(LinearLayout.java:2102)
                                                             at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1956)
                                                             at android.widget.LinearLayout.onLayout(LinearLayout.java:1865)
                                                             at android.view.View.layout(View.java:16542)
                                                             at android.view.ViewGroup.layout(ViewGroup.java:5302)
                                                             at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
                                                             at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
                                                             at android.view.View.layout(View.java:16542)
                                                             at android.view.ViewGroup.layout(ViewGroup.java:5302)
                                                             at android.widget.LinearLayout.setChildFrame(LinearLayout.java:2102)
                                                             at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1956)
                                                             at android.widget.LinearLayout.onLayout(LinearLayout.java:1865)
                                                             at android.view.View.layout(View.java:16542)
                                                             at android.view.ViewGroup.layout(ViewGroup.java:5302)
                                                             at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
                                                             at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
                                                             at android.view.View.layout(View.java:16542)
                                                             at android.view.ViewGroup.layout(ViewGroup.java:5302)
                                                             at android.widget.LinearLayout.setChildFrame(LinearLayout.java:2102)
                                                             at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1956)
                                                             at android.widget.LinearLayout.onLayout(LinearLayout.java:1865)
                                                             at android.view.View.layout(View.java:16542)
                                                             at android.view.ViewGroup.layout(ViewGroup.java:5302)
                                                             at android.support.design.widget.CoordinatorLayout.layoutChild(CoordinatorLayout.java:1037)
                                                             at android.support.design.widget.CoordinatorLayout.onLayoutChild(CoordinatorLayout.java:747)
                                                             at android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:761)
                                                             at android.view.View.layout(View.java:16542)
                                                             at android.view.ViewGroup.layout(ViewGroup.java:5302)
                                                             at android.widget.LinearLayout.setChildFrame(LinearLayout.java:2102)
                                                             at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1956)
                                                             at android.widget.LinearLayout.onLayout(LinearLayout.java:1865)
                                                             at android.view.View.layout(View.java:16542)
                                                            at android.view.V  

对于我的XML,这是指导我的错误。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical">
    <LinearLayout
     android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <LinearLayout
        android:id="@+id/mapHolder"
        android:layout_width="match_parent"
        android:layout_height="160dp">

        <fragment
            android:id="@+id/googleMap"
            class="com.google.android.gms.maps.SupportMapFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    </LinearLayout>
</LinearLayout>

1 个答案:

答案 0 :(得分:2)

我建议,以编程方式膨胀SupportMapFragment 而不是以XML格式添加片段。

public class AdapterTrending extends RecyclerView.Adapter<AdapterTrending.MyTrendingHolder> {

private FragmentManager mFragmentManager;
private int viewType = 0;
private Context mContext;
private ArrayList<String> reviewTrending = new ArrayList<>();

public AdapterTrending(Context context, FragmentManager fragmentManager, ArrayList<String> reviewTrending) {
    this.mContext = context;
    this.mFragmentManager = fragmentManager;
    this.reviewTrending = reviewTrending;
}

@Override
public MyTrendingHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    int listViewItemType = getItemViewType(viewType);
        if (listViewItemType == 0) {
            return new MyGoogleMap(LayoutInflater.from(mContext).inflate(R.layout.custom_googlemap_layout, parent, false));
        } else {
            return new MyTrendings(LayoutInflater.from(mContext).inflate(R.layout.custom_trending_layout, parent, false));
        }
}

  @Override
    public void onBindViewHolder(ExperienceListHolder holder, int position) {
    // Setup other views here

    SupportMapFragment fragment = SupportMapFragment.newInstance(options);
    FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
            fragmentTransaction
            .replace(R.id.map_container, fragment)
            .addToBackStack(null)
            .commit();
    fragment.getMapAsync(this);
}

@Override
public int getItemCount() {
    return 10;
}

@Override
public int getItemViewType(int position) {
    if (position == 0) {
        viewType = 0;
    }
    if (position >= 1) {
        viewType = position;
    }
    return viewType;
}


class MyTrendings extends MyTrendingHolder {
    public MyTrendings(View itemView) {
        super(itemView);
    }
}

class MyGoogleMap extends MyTrendingHolder {
    public MyGoogleMap(View itemView) {
        super(itemView);
    }
}

class MyTrendingHolder extends RecyclerView.ViewHolder {
    public MyTrendingHolder(View itemView) {
        super(itemView);
    }
}
}

XML现在看起来像

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical">
    <LinearLayout
     android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


    <LinearLayout
        android:id="@+id/map_container"
        android:layout_width="match_parent"
        android:layout_height="160dp">

    </LinearLayout>
</LinearLayout>