Gautam Gautam - 4 years ago 90
Android Question

How to load multiple fragments in Recycler view

I am using recycler view inside fragment and load 6 different fragment in recycler view. 6 different fragment load successfully but when reload main fragment then got app crashed.

Crash log

android.view.InflateException: Binary XML file line #13: Binary XML file line #13: Error inflating class fragment
Caused by: android.view.InflateException: Binary XML file line #13: Error inflating class fragment
Caused by: java.lang.IllegalArgumentException: Binary XML file line #13: Duplicate id 0x7f1101c1, tag null, or parent id 0x7f1101c0 with another fragment for dashboard.DashBoardDeviceUsageFragment
at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:3420)
at android.support.v4.app.FragmentController.onCreateView(FragmentController.java:120)
at android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:378)
at android.support.v4.app.BaseFragmentActivityHoneycomb.onCreateView(BaseFragmentActivityHoneycomb.java:33)
at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:79)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:777)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:858)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
at dashboard.RecyclerListAdapter.onCreateViewHolder(RecyclerListAdapter.java:52)
at dashboard.RecyclerListAdapter.onCreateViewHolder(RecyclerListAdapter.java:28)


Adapter

@Override
public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

View view;
switch (viewType) {
case 0:
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.dashboard_device_usage_layout, parent, false);
break;

case 1:
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.dashboard_app_usage_layout, parent, false);
break;

case 2:
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.dashboard_fitness_layout, parent, false);
break;

case 3:
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.dashboard_inapp_fragment, parent, false);
break;

case 4:
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.dashboard_place_layout, parent, false);
break;

case 5:
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.dashboard_sleep_layout, parent, false);
break;

case 6:
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.dashboard_graph_layout, parent, false);
break;

default:
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.dashboard_device_usage_layout, parent, false);
}
return new ItemViewHolder(view);
}


Fragment sample

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_drag_drop_device"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/layout_margin_8"
app:cardCornerRadius="2dp"
app:cardElevation="3dp"
android:background="@color/white">

<fragment
android:id="@+id/drag_dashboard_device_fragment"
class="dashboard.DashBoardDeviceUsageFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</fragment>
</android.support.v7.widget.CardView>

Answer Source

I got my answer

@Override
    public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        FragmentManager fragmentManager = mContext.getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        switch (viewType) {
            case 0:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.dashboard_device_usage_layout, parent, false);
                DashBoardDeviceUsageFragment deviceUsageFragment = new DashBoardDeviceUsageFragment();
                transaction.replace(R.id.drag_dashboard_device_fragment, deviceUsageFragment, "dashboardFragment");
                transaction.commit();
                break;
            case 1:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.dashboard_app_usage_layout, parent, false);
                DashBoardAppUsageFragment appUsageFragment = new DashBoardAppUsageFragment();
                transaction.replace(R.id.drag_dashboard_app_fragment, appUsageFragment, "dashboardFragment");
                transaction.commit();
                break;

            case 2:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.dashboard_fitness_layout, parent, false);
                DashBoardHealthFragment healthFragment = new DashBoardHealthFragment();
                transaction.replace(R.id.drag_dashboard_health_fragment, healthFragment, "dashboardFragment");
                transaction.commit();
                break;

            case 3:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.dashboard_inapp_fragment, parent, false);
                break;

            case 4:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.dashboard_place_layout, parent, false);
                DashboardPlaceFragment placeFragment = new DashboardPlaceFragment();
                transaction.replace(R.id.drag_dashboard_place_fragment, placeFragment, "dashboardFragment");
                transaction.commit();
                break;

            case 5:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.dashboard_sleep_layout, parent, false);
                DashBoardSleepFragment sleepFragment = new DashBoardSleepFragment();
                transaction.replace(R.id.drag_dashboard_sleep_fragment, sleepFragment, "dashboardFragment");
                transaction.commit();
                break;

            case 6:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.dashboard_graph_layout, parent, false);
                DashBoardGraphFragment graphFragment = new DashBoardGraphFragment();
                transaction.replace(R.id.drag_dashboard_graph_fragment, graphFragment, "dashboardFragment");
                transaction.commit();
                break;

            default:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.dashboard_device_usage_layout, parent, false);
        }
        return new ItemViewHolder(view);
    }
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download