1

in my application i'm using support view pager and PagerTabStrip. into view pager i have some fragment and into one of that i'm using support cardview. after run application all card view items show correctly, but after rotate phone from portrait to landscape, content hide.

enter image description here

enter image description here

UPDATE: MainActivity

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ButterKnife.inject(this);
    Globals.setActivity(this);

    contentFragment = ContentFragment.newInstance(R.drawable.ic_launcher);
    getSupportFragmentManager().beginTransaction()
            .replace(R.id.frame_container, contentFragment)
            .commit();

    current_fragment_id = 0;
    updateFragment(current_fragment_id);

    drawer_adatper =
            new DrawerAdatper(
                    this, R.layout.drawer_item_layout, R.layout.drawer_item_group_layout, drawer_items);
    drawer_list.setAdapter(drawer_adatper);
}

ContentFragment fragment:

public class ContentFragment extends Fragment implements ScreenShotable {

    public static ContentFragment newInstance(int page) {
        ContentFragment fragmentFirst = new ContentFragment();
        Bundle          args          = new Bundle();
        args.putInt("page", page);
        fragmentFirst.setArguments(args);
        return fragmentFirst;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        page = getArguments().getInt("page", 0);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_online_categories, container, false);
        ButterKnife.inject(this, view);
        initializeUi();
        pager_header.setTabIndicatorColor(Color.parseColor(Arrays.asList(pageTabIndicatorColors).get(0)));
        BootStrapPagerAdapter adapterViewPager = new BootStrapPagerAdapter(getActivity().getSupportFragmentManager());
        return view;
    }

    public static class BootStrapPagerAdapter extends FragmentPagerAdapter {
        private static int NUM_ITEMS = 8;
        public BootStrapPagerAdapter(FragmentManager fragmentManager) {
            super(fragmentManager);
        }

        public int getCount() {
            return NUM_ITEMS;
        }
        @Override
        public Fragment getItem(int position) {
            switch (position) {
                case 0:
                    return FragmentContactList.newInstance(0);
            }
        }
        @Override
        public CharSequence getPageTitle(int position) {
            String title = "";
            switch (position) {
                case 0:
                    title = Utils.getString(R.string.section_industry);
                    break;
            }
            return title;
        }
    }
}

FragmentContactList:

public class FragmentContactList extends Fragment {
    private StackTraceElement[] traceElements = Thread.currentThread().getStackTrace();
    private int page;
    RecyclerView rv;
    private List<Person> persons;

    public static FragmentContactList newInstance(int page) {
        FragmentContactList fragmentFirst = new FragmentContactList();
        Bundle              args          = new Bundle();
        args.putInt("page", page);
        fragmentFirst.setArguments(args);
        return fragmentFirst;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        page = getArguments().getInt("page", 0);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.recyclerview_activity, container, false);
        rv = (RecyclerView) view.findViewById(R.id.rv);

        LinearLayoutManager llm = new LinearLayoutManager(getActivity());
        rv.setLayoutManager(llm);
        rv.setHasFixedSize(true);

        initializeData();
        initializeAdapter();
        return view;
    }

    private void initializeData(){
        persons = new ArrayList<>();
        persons.add(new Person("Emma Wilson", "23 years old", R.drawable.icon_inbox));
        persons.add(new Person("Lavery Maiss", "25 years old", R.drawable.icon_inbox));
        persons.add(new Person("Lillie Watts", "35 years old", R.drawable.icon_inbox));
    }

    private void initializeAdapter(){
        RVAdapter adapter = new RVAdapter(persons);
        rv.setAdapter(adapter);
    }
    @Override
    public void onResume() {
        super.onResume();
        getView().invalidate();
    }
}
comrade
  • 4,590
  • 5
  • 33
  • 48
  • I think you may need to implement something to save the instance state of your activity when the device is rotated. Android may not restore the fragment with the one that was there before the rotation. – Paulo Avelar Aug 05 '15 at 17:41
  • i think there's a problem about your activity and viewpager initilazition. can you put your activity code? thanks. – savepopulation Aug 05 '15 at 17:49
  • @savepopulation post updated sir. thanks –  Aug 05 '15 at 17:59
  • can you extend your adapter from "FragmentStatePagerAdapter" and try again? – savepopulation Aug 05 '15 at 18:08
  • @savepopulation my adapter extends from `RecyclerView.Adapter` i dont change it, i think my code doesnt work after change. can i email project for you? –  Aug 05 '15 at 18:17
  • i mean this : public static class BootStrapPagerAdapter extends FragmentPagerAdapter – savepopulation Aug 05 '15 at 18:19
  • @savepopulation thanks sir. problem solved. whats problem `FragmentPagerAdapter` vs `FragmentStatePagerAdapter` –  Aug 05 '15 at 18:32
  • i'm glad that it solved your problem. you can check difference from here: http://stackoverflow.com/questions/18747975/difference-between-fragmentpageradapter-and-fragmentstatepageradapter – savepopulation Aug 05 '15 at 18:43
  • @savepopulation thanks, please reply to topic and let me to accept that. –  Aug 05 '15 at 19:04
  • @mahdipishguy i posted as an answer. thanks. – savepopulation Aug 05 '15 at 19:39

1 Answers1

1

You should change

public static class BootStrapPagerAdapter extends FragmentPagerAdapter

to

public static class BootStrapPagerAdapter extends FragmentStatePagerAdapter

You can check difference between them from here: Difference between FragmentPagerAdapter and FragmentStatePagerAdapter

Community
  • 1
  • 1
savepopulation
  • 11,736
  • 4
  • 55
  • 80