Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Viewpager inside of Viewpager not showing layout

I have been tasked with creating a very unorthodox layout that can only be accomplished with a viewpager inside the second pane of another viewpager, thankfully the outer viewpager needs to be locked so the task was a little more realistic, however the unexpected issue arose where after the inner viewpager was loaded onCreateView is never called, I can see that it is sliding side to side so the inner viewpager is setup but it will not load any inner view for some odd reason, any help will go a long way thanks

Main Activity

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home_fragment);

    pagePosition = 0;

    setupViews();

}

private void setupViews() {

    //setup images to be changed later

    ivCategory = (ImageView) findViewById(R.id.ivCatergory);
    ivHome = (ImageView) findViewById(R.id.ivHome);
    ivProfile = (ImageView) findViewById(R.id.ivProfile);

    //setup the viewpager
    pager = (CustomViewPager) findViewById(R.id.viewpager);

    //add fragments to the fragment array
    pageList.add(NavigationFragment.newInstance(0, getApplicationContext(), this));
    pageList.add(NavigationFragment.newInstance(1, getApplicationContext(), this));
    pageList.add(NavigationFragment.newInstance(0, getApplicationContext(), this));


    // initialize the page adapter
    pageAdapter = new FragmentHomeAdapter(getSupportFragmentManager(), pageList);

    //set the page adapter to the viewpager
    pager.setAdapter(pageAdapter);

    //set homepage to be first
    pager.setCurrentItem(1);


    //disable swiping to mimic iphone behaviour
    pager.setPagingEnabled(false);


}

viewpager

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

    int pageType = getArguments().getInt(PAGE_TYPE);

    View v;


    if (pageType == 1) {//all inner fragments will happen here

        v = inflater.inflate(R.layout.fragment_container, container, false);

        //setup the inner viewpager
        pager = (CustomViewPager) v.findViewById(R.id.viewpager);

        pageList.add(HomeFragment.newInstance(0, context, getActivity()));
        pageList.add(HomeFragment.newInstance(1, context, getActivity()));

        // initialize the page adapter
        pageAdapter = new FragmentHomeAdapter(getActivity().getSupportFragmentManager(), pageList);

        //set the page adapter to the viewpager
        pager.setAdapter(pageAdapter);

        pager.setPagingEnabled(true);

    }


    return v;
}

inner viewpager

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

    View v;

    v = inflater.inflate(R.layout.fragment_list_feed, container, false);

    lv = (ListView) v.findViewById(R.id.lvFeed);

    listAdapter = new ListHomeAdapter(getArguments().getInt(PAGE_TYPE), feedListItems, getActivity());

    //add header
    header = (ViewGroup) inflater.inflate(R.layout.list_item_header, lv, false);
    lv.addHeaderView(header, null, false);

    lv.setAdapter(listAdapter);

    if (getArguments().getInt(PAGE_TYPE) == 0) {

        new getFeedListItems().execute();

    } else {

        new getFeedListItems().execute();

    }

    Toast.makeText(context, "This never shows",
            Toast.LENGTH_SHORT).show();


    return v;
}
like image 557
Edmund Rojas Avatar asked Nov 28 '22 13:11

Edmund Rojas


1 Answers

When showing Fragments inside another Fragment, use getChildFragmentManager() instead of getSupportFragmentManager()

Initialize your pageAdapter this way:

pageAdapter = new FragmentHomeAdapter(getChildFragmentManager(), pageList);
like image 88
Tiozao Avatar answered Dec 04 '22 13:12

Tiozao