PagerAdapter#notifyDataSetChange

时间:2017-04-17 14:37:16

标签: android

我的自定义PageAdapter类中有以下错误,该类继承自 FragmentStatePagerAdapter,我有10个选项卡,我的产品被找到,PageAdaptar有2个方法覆盖公共CharSequence getPageTitle,public int getCount()和构造函数。以下错误标志着我: - > 应用程序的PagerAdapter在不调用PagerAdapter#notifyDataSetChanged的情况下更改了适配器的内容!适配器元素的估计数量:0,找到:10寻呼机

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

    <android.support.design.widget.TabLayout
        android:id="@+id/tablayout_menu"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/blanco"
        android:scrollbars="horizontal"
        app:tabGravity="fill"
        app:tabSelectedTextColor="@color/colorPrimaryDark"
        app:tabTextColor="@color/negro"
        app:tabMode="scrollable" />

    <android.support.v4.view.ViewPager
        android:id="@+id/viewPager_menu"
        android:paddingBottom="50dp"
        android:fitsSystemWindows="true"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/tablayout_perfil"/>

</LinearLayout>

立即有我的自定义适配器,根据我返回片段的位置

 public class CostumAdapter2 extends FragmentStatePagerAdapter {

        public CostumAdapter2(FragmentManager fm) {
            super(fm);

        }

        @Override
        public Fragment getItem(int position) {
            Bundle bundle = new Bundle();

            switch (position) {
                case 0:

                    FragmentBebidas fragmentBebidas = new FragmentBebidas();
                    bundle.putInt("ID", Integer.parseInt(listMenu.get(position).getId()));
                    fragmentBebidas.setArguments(bundle);
                    return fragmentBebidas;

                case 1:
                    FragmentBebidas fragmentBebidas2 = new FragmentBebidas();
                    bundle.putInt("ID", Integer.parseInt(listMenu.get(position).getId()));
                    fragmentBebidas2.setArguments(bundle);
                    return fragmentBebidas2;

                case 2:
                    FragmentBebidas fragmentBebidas3 = new FragmentBebidas();
                    bundle.putInt("ID", Integer.parseInt(listMenu.get(position).getId()));
                    fragmentBebidas3.setArguments(bundle);
                    return fragmentBebidas3;


                case 3:
                    FragmentBebidas fragmentBebidas4 = new FragmentBebidas();
                    bundle.putInt("ID", Integer.parseInt(listMenu.get(position).getId()));
                    fragmentBebidas4.setArguments(bundle);
                    return fragmentBebidas4;


                case 4:
                    FragmentBebidas fragmentBebidas5 = new FragmentBebidas();
                    bundle.putInt("ID", Integer.parseInt(listMenu.get(position).getId()));
                    fragmentBebidas5.setArguments(bundle);
                    return fragmentBebidas5;


                case 5:
                    FragmentBebidas fragmentBebidas6 = new FragmentBebidas();
                    bundle.putInt("ID", Integer.parseInt(listMenu.get(position).getId()));
                    fragmentBebidas6.setArguments(bundle);
                    return fragmentBebidas6;


                case 6:
                    FragmentBebidas fragmentBebidas7 = new FragmentBebidas();
                    bundle.putInt("ID", Integer.parseInt(listMenu.get(position).getId()));
                    fragmentBebidas7.setArguments(bundle);
                    return fragmentBebidas7;


                case 7:
                    FragmentBebidas fragmentBebidas8 = new FragmentBebidas();
                    bundle.putInt("ID", Integer.parseInt(listMenu.get(position).getId()));
                    fragmentBebidas8.setArguments(bundle);
                    return fragmentBebidas8;


                case 8:
                    FragmentBebidas fragmentBebidas9 = new FragmentBebidas();
                    bundle.putInt("ID", Integer.parseInt(listMenu.get(position).getId()));
                    fragmentBebidas9.setArguments(bundle);
                    return fragmentBebidas9;
                case 9:
                    FragmentBebidas fragmentBebidas10 = new FragmentBebidas();
                    bundle.putInt("ID", Integer.parseInt(listMenu.get(position).getId()));
                    fragmentBebidas10.setArguments(bundle);
                    return fragmentBebidas10;
            }
            return null;

        }
        @Override
        public CharSequence getPageTitle(int position) {
            return menuTabs.get(position).toString();
        }
        @Override
        public int getCount() {
            return menuTabs.size();
        }


    }

这是执行下一个操作以扩充包含选项卡

的片段的位置
ahBottomNavigation.setOnTabSelectedListener(new AHBottomNavigation.OnTabSelectedListener() {
            @Override
            public boolean onTabSelected(int position, boolean wasSelected) {
                // Do something cool here...
                switch (position) {

                    case 0:

                        //seteando el texto del menu
                        tituloActivity.setText("Menú");
                        iconBack.setVisibility(View.INVISIBLE);
                        //Toast.makeText(getApplicationContext(), "Menú", Toast.LENGTH_LONG).show();
                        FragmentManager fragmentManager = getSupportFragmentManager();
                        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                        MenuViewController menuViewController = new MenuViewController();
                        /**menuViewController.setArguments(bundle);**/
                        fragmentTransaction.add(R.id.myFragemnt,menuViewController);
                        fragmentTransaction.replace(R.id.myFragemnt, menuViewController);
                        fragmentTransaction.commit();


                        return true;
                    case 1:

在这部分中我有setOnTabSelectedListener事件

  rootView = inflater.inflate(R.layout.menu_layout, container, false);
        tabLayout = (TabLayout) rootView.findViewById(R.id.tablayout_menu);
        viewPager = (ViewPager) rootView.findViewById(R.id.viewPager_menu);
        CostumAdapter2  costumAdapter =  new CostumAdapter2(getActivity().getSupportFragmentManager());
        viewPager.setAdapter(costumAdapter);
        costumAdapter.notifyDataSetChanged();
        tabLayout.setupWithViewPager(viewPager);
        tabLayout.post(new Runnable() {
            @Override
            public void run() {
                tabLayout.setupWithViewPager(viewPager);
            }
        });

        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());
            }

        });

        return rootView;
    }
> 04-17 09:25:56.041: E/AndroidRuntime(30450): FATAL EXCEPTION: main
> 04-17 09:25:56.041: E/AndroidRuntime(30450): Process:
> com.blueicon.romerito, PID: 30450 04-17 09:25:56.041:
> E/AndroidRuntime(30450): java.lang.IllegalStateException: The
> application's PagerAdapter changed the adapter's contents without
> calling PagerAdapter#notifyDataSetChanged! Expected adapter item
> count: 0, found: 10 Pager id: com.blueicon.romerito:id/viewPager_menu
> Pager class: class android.support.v4.view.ViewPager Problematic
> adapter: class
> com.blueicon.romerito.controller.menuprincipal.fragmentosmenu.MenuViewController$CostumAdapter2
> 04-17 09:25:56.041: E/AndroidRuntime(30450):    at
> android.support.v4.view.ViewPager.populate(ViewPager.java:1167) 04-17
> 09:25:56.041: E/AndroidRuntime(30450):  at
> android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:668)
> 04-17 09:25:56.041: E/AndroidRuntime(30450):    at
> android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:630)
> 04-17 09:25:56.041: E/AndroidRuntime(30450):    at
> android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:611)
> 04-17 09:25:56.041: E/AndroidRuntime(30450):    at
> android.support.design.widget.TabLayout$ViewPagerOnTabSelectedListener.onTabSelected(TabLayout.java:2178)
> 04-17 09:25:56.041: E/AndroidRuntime(30450):    at
> android.support.design.widget.TabLayout.dispatchTabSelected(TabLayout.java:1155)
> 04-17 09:25:56.041: E/AndroidRuntime(30450):    at
> android.support.design.widget.TabLayout.selectTab(TabLayout.java:1148)
> 04-17 09:25:56.041: E/AndroidRuntime(30450):    at
> android.support.design.widget.TabLayout.selectTab(TabLayout.java:1118)
> 04-17 09:25:56.041: E/AndroidRuntime(30450):    at
> android.support.design.widget.TabLayout.populateFromPagerAdapter(TabLayout.java:916)
> 04-17 09:25:56.041: E/AndroidRuntime(30450):    at
> android.support.design.widget.TabLayout.setPagerAdapter(TabLayout.java:900)
> 04-17 09:25:56.041: E/AndroidRuntime(30450):    at
> android.support.design.widget.TabLayout.setupWithViewPager(TabLayout.java:813)
> 04-17 09:25:56.041: E/AndroidRuntime(30450):    at
> android.support.design.widget.TabLayout.setupWithViewPager(TabLayout.java:774)
> 04-17 09:25:56.041: E/AndroidRuntime(30450):    at
> android.support.design.widget.TabLayout.setupWithViewPager(TabLayout.java:752)
> 04-17 09:25:56.041: E/AndroidRuntime(30450):    at
> com.blueicon.romerito.controller.menuprincipal.fragmentosmenu.MenuViewController$1.run(MenuViewController.java:70)
> 04-17 09:25:56.041: E/AndroidRuntime(30450):    at
> android.os.Handler.handleCallback(Handler.java:739) 04-17
> 09:25:56.041: E/AndroidRuntime(30450):  at
> android.os.Handler.dispatchMessage(Handler.java:95) 04-17
> 09:25:56.041: E/AndroidRuntime(30450):  at
> android.os.Looper.loop(Looper.java:158) 04-17 09:25:56.041:
> E/AndroidRuntime(30450):    at
> android.app.ActivityThread.main(ActivityThread.java:7229) 04-17
> 09:25:56.041: E/AndroidRuntime(30450):  at
> java.lang.reflect.Method.invoke(Native Method) 04-17 09:25:56.041:
> E/AndroidRuntime(30450):    at
> com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
> 04-17 09:25:56.041: E/AndroidRuntime(30450):    at
> com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

2 个答案:

答案 0 :(得分:0)

更新CostumAdapter2,如下所示:

public class CostumAdapter2 extends FragmentStatePagerAdapter {

        final int PAGE_COUNT = 10;
        private String tabTitles[] = new String[] {"TAB0", "TAB1", "TAB2", "TAB3", "TAB4", "TAB5", "TAB6", "TAB7", "TAB8", "TAB9"};

        public CostumAdapter2(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            Bundle bundle = new Bundle();

            switch (position) {
                case 0:

                    FragmentBebidas fragmentBebidas = new FragmentBebidas();
                    bundle.putInt("ID", Integer.parseInt(listMenu.get(position).getId()));
                    fragmentBebidas.setArguments(bundle);
                    return fragmentBebidas;

                case 1:
                    FragmentBebidas fragmentBebidas2 = new FragmentBebidas();
                    bundle.putInt("ID", Integer.parseInt(listMenu.get(position).getId()));
                    fragmentBebidas2.setArguments(bundle);
                    return fragmentBebidas2;

                case 2:
                    FragmentBebidas fragmentBebidas3 = new FragmentBebidas();
                    bundle.putInt("ID", Integer.parseInt(listMenu.get(position).getId()));
                    fragmentBebidas3.setArguments(bundle);
                    return fragmentBebidas3;


                case 3:
                    FragmentBebidas fragmentBebidas4 = new FragmentBebidas();
                    bundle.putInt("ID", Integer.parseInt(listMenu.get(position).getId()));
                    fragmentBebidas4.setArguments(bundle);
                    return fragmentBebidas4;


                case 4:
                    FragmentBebidas fragmentBebidas5 = new FragmentBebidas();
                    bundle.putInt("ID", Integer.parseInt(listMenu.get(position).getId()));
                    fragmentBebidas5.setArguments(bundle);
                    return fragmentBebidas5;


                case 5:
                    FragmentBebidas fragmentBebidas6 = new FragmentBebidas();
                    bundle.putInt("ID", Integer.parseInt(listMenu.get(position).getId()));
                    fragmentBebidas6.setArguments(bundle);
                    return fragmentBebidas6;


                case 6:
                    FragmentBebidas fragmentBebidas7 = new FragmentBebidas();
                    bundle.putInt("ID", Integer.parseInt(listMenu.get(position).getId()));
                    fragmentBebidas7.setArguments(bundle);
                    return fragmentBebidas7;


                case 7:
                    FragmentBebidas fragmentBebidas8 = new FragmentBebidas();
                    bundle.putInt("ID", Integer.parseInt(listMenu.get(position).getId()));
                    fragmentBebidas8.setArguments(bundle);
                    return fragmentBebidas8;


                case 8:
                    FragmentBebidas fragmentBebidas9 = new FragmentBebidas();
                    bundle.putInt("ID", Integer.parseInt(listMenu.get(position).getId()));
                    fragmentBebidas9.setArguments(bundle);
                    return fragmentBebidas9;
                case 9:
                    FragmentBebidas fragmentBebidas10 = new FragmentBebidas();
                    bundle.putInt("ID", Integer.parseInt(listMenu.get(position).getId()));
                    fragmentBebidas10.setArguments(bundle);
                    return fragmentBebidas10;
            }

            return null;    
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return tabTitles[position];
        }

        @Override
        public int getCount() {
            return PAGE_COUNT;
        }

        @Override
        public int getItemPosition(Object object) {
            return POSITION_NONE;
        }
}

仅供参考,根据您的需要更改tabTitles(TAB0, TAB1.....);

更新onCreateView(),如下所示:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.menu_layout, container, false);

    tabLayout = (TabLayout) rootView.findViewById(R.id.tablayout_menu);
    viewPager = (ViewPager) rootView.findViewById(R.id.viewPager_menu);

    CostumAdapter2  costumAdapter =  new CostumAdapter2(getActivity().getSupportFragmentManager());
    viewPager.setAdapter(costumAdapter);
    tabLayout.setupWithViewPager(viewPager);

    tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            viewPager.setCurrentItem(tab.getPosition());
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {
            viewPager.setCurrentItem(tab.getPosition());

        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {
            viewPager.setCurrentItem(tab.getPosition());
        }

    });

    return rootView;
}

希望这会有所帮助〜

答案 1 :(得分:0)

您将MenuViewController.CostumAdapter2声明为内部类。当您更改MenuViewController.menuTabs的内容时,您还必须在每个适配器上调用notifyDataSetChanged()

由于您在MenuViewController中甚至没有引用它,这可能很难。

尝试在MenuViewController内将适配器实现为 Singleton ,并在更改notifyDataSetChanged()时调用menuTabs