Geoplex
Geoplex

Reputation: 297

I'm unable to call a Fragment within onTabSelected of another Fragment that implements ActionTab Listener

I am trying to create a Fragment that has a ListView in the content section, as a default view. There are ActionBar tabs to this view. I'm able to create that so far.

The LaunchListFragment Code

public class LaunchListFragment extends ListFragment implements ActionBar.TabListener {
public static  String LIST_TYPE = "invalid";
GenericListData g_data[] = null;
   private ViewPager mViewPager;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View result = inflater.inflate(R.layout.fragment_launch_list,
            container, false);

    setActionTabs();
    setList();
    return (result);
}

public static LaunchListFragment newInstance(String type) {
    LaunchListFragment frag = new LaunchListFragment();
    Bundle args = new Bundle();
    LIST_TYPE=type;

    args.putString(LIST_TYPE, type);
    frag.setArguments(args);

    return (frag);
}

What I wanted is, when you click the onTabSelected(), would like to change the views and I am using PagerFragment with views generated using an FragmentPagerAdapter

@Override
public void onTabSelected(Tab tab, android.app.FragmentTransaction ft) {

       getChildFragmentManager().beginTransaction()
       .add(android.R.id.content,
            new PagerFragment()).commit();
}

The trouble is, I can't use getChildFragmentManager or getFragmentManager to call the PagerFragment, I get compilation errors to change the PagerFragment to app.Fragment. Even if I change that, it still complaints.

I suspect there is some sort of clash between support.Fragment and app.Fragment.

Is there a different way of approach to acheive this other than using nested Fragments?

Any help appreciated.

Upvotes: 3

Views: 1652

Answers (1)

Mani
Mani

Reputation: 777

I would recommend you to use the viewPager, have your two fragments to show on tab selections(on action bar). Use FragmentStatePagerAdapter or FragmentPagerAdapter depending on the need, then initialise the viewPager and set the adapter. Then in 'onTabSelected()' get tab postion then show the corresponding viewPager's page.

       FragmentPagerAdapter fragmentStagePageAdapter = new SearchStatePagerAdapter(getFragmentManager());


public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    mView =  inflater.inflate(R.layout.venues_tabhost_layout, container, false);

    mViewPager = ( com.edenpod.goinout.view.ViewPager) mView.findViewById(R.id.pager);
    mViewPager.removeAllViews();
    mViewPager.setAdapter(fragmentStagePageAdapter);
    mViewPager.setOffscreenPageLimit(3);
    ----------------
    ---------------
   --------------
    }

    public void onTabChanged(Tab tab, android.app.FragmentTransaction ft) {
    int pos = tab.getCurrentTab();
    if( mViewPager!= null && pos == 0 ) {
        mViewPager.setCurrentItem(pos);
    } else if( mViewPager != null && pos == 1 ) {
        mViewPager.setCurrentItem(pos);
    } else if( mViewPager != null && pos == 2 ) {
        mViewPager.setCurrentItem(pos);
    }
    }

// Example PagerAdapter
public class SearchStatePagerAdapter extends FragmentPagerAdapter{

private ArrayList<Fragment> mFragmentsList;

public SearchStatePagerAdapter(FragmentManager fm) {
    super(fm);
    mFragmentsList = new ArrayList<Fragment>();
    mFragmentsList.add(new VenueSearchFragment());
    mFragmentsList.add(new PeopleSearchFragment());
}

@Override
public int getCount() {
    return mFragmentsList.size();
}

@Override
public Fragment getItem(int position) {
    Fragment fragment = null;
    switch (position) {
        case 0:
            fragment = mFragmentsList.get(position);
            break;
        case 1:
            fragment = mFragmentsList.get(position);
            break;
    }
    return fragment;
}

}

Upvotes: 2

Related Questions