-1

What I have:

  • I have a fragment
  • I am using SherlockActionBar library
  • I have a FragmentTabHost in Fragment

What i want to know: I want to detect onClick event each tab and perform some action based on it

Fragment1.java:

public class Fragment1 extends SherlockFragment{

    private FragmentTabHost mTabHost;


    //Mandatory Constructor
    public Fragment1() {
    }

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

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

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


        mTabHost = (FragmentTabHost)rootView.findViewById(android.R.id.tabhost);
        mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.realtabcontent);

        mTabHost.addTab(mTabHost.newTabSpec("fragmentb").setIndicator("Rating"),
                RatingAscending.class, null);
        mTabHost.addTab(mTabHost.newTabSpec("fragmentc").setIndicator("Price"),
                PriceAscending.class, null);
        mTabHost.addTab(mTabHost.newTabSpec("fragmentd").setIndicator("Distance"),
                DistanceAscending.class, null);


        return rootView;
    }

}
Blo
  • 11,903
  • 5
  • 45
  • 99
Devrath
  • 42,072
  • 54
  • 195
  • 297

1 Answers1

0

I'm assuming what you want to do is detect when a specific tab has been clicked in your FragmentTabHost. This can be accomplished quite simply by adding a setOnTabChangedListener. Here is an example:

FragmentTabHost t = new FragmentTabHost(getActivity());
    t.setOnTabChangedListener(new OnTabChangeListener() {

        @Override
        public void onTabChanged(String tabId) {
            // TODO your actions go here

        }
    });

}
stewjacks
  • 471
  • 6
  • 12
  • yes that swhat i was looking for .... also can u add more info on answer on ..... say .... my tab is displaying a fragment( ... say fragmentA .... now using your solution i detected the event ...... now how to load fragmentB on that event in same tab – Devrath Apr 24 '14 at 21:38
  • I think you should pose that as another question – stewjacks Apr 25 '14 at 00:11
  • @stewjacks.... Ok please have a look at this question ... i have formulated a new one http://stackoverflow.com/q/23284232/1083093 – Devrath Apr 25 '14 at 04:36