如何在Recycler视图中加载多个片段

时间:2017-02-02 06:43:43

标签: android android-fragments android-recyclerview

我在片段中使用了recycleler视图,在recycleler视图中使用加载6个不同的片段。 6个不同的片段加载成功但是当重新加载主片段然后应用程序崩溃。

崩溃日志

    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)

适配器

@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);
    }

片段示例

<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>

3 个答案:

答案 0 :(得分:1)

我得到了答案

@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);
    }

答案 1 :(得分:0)

请用此替换您的inflater并尝试

Button

答案 2 :(得分:0)

使用

在浏览视图之前删除视图
if (view != null) {
       ViewGroup vg_parent = (ViewGroup) view.getParent();
       if (vg_parent!= null)
           vg_parent.removeView(view);
}