Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android onclicklistener for tabs

I have been trying to solve this issue now for longer than I care to admit. I'm looking to set up an onClicklistener for my tabs so even if a user is on that tab and they click it the tab reloads. Could someone please point out my error, below is the code i'm using made up from examples from stack overflow so thanks so far! I'm using the getTabHost().setCurrentTab(3) to set it to be run only on the tab 3 but how would I get it so that it calls the specific tab the user clicks on?

I have been using this as a reference: OnClickListener on Tabs not working

public class DamTabs extends TabActivity implements OnClickListener{
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Resources res = getResources(); // Resource object to get Drawables
    TabHost tabHost = getTabHost(); // The activity TabHost


    Intent intent = new Intent(this, Featured.class);
    tabHost.addTab(tabHost.newTabSpec("Tab 1")
            .setIndicator("Featured", res.getDrawable(R.drawable.ic_tab_main))
            .setContent(intent));

    Intent intent2 = new Intent(this, Deals.class);
    tabHost.addTab(tabHost.newTabSpec("Tab 2")
            .setIndicator("Deals", res.getDrawable(R.drawable.ic_tab_setup))
            .setContent(intent2));

    Intent intent3 = new Intent(this, Events.class);
    tabHost.addTab(tabHost.newTabSpec("Tab 3")
            .setIndicator("Events", res.getDrawable(R.drawable.ic_tab_setup))
            .setContent(intent3));

    getTabWidget().getChildAt(3).setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if (getTabHost().getCurrentTabTag().equals(sort)) {
                getTabHost().setCurrentTab(3);
            }
        }
    });

} //End of onCreate
like image 849
James Avatar asked Feb 20 '23 11:02

James


2 Answers

If I understood your problem, I think you should use setOnTabChangedListener() method. Something like this:

mTabHost.setOnTabChangedListener(new OnTabChangeListener() {

        public void onTabChanged(String tabId) {
            Log.d(debugTag, "onTabChanged: tab number=" + mTabHost.getCurrentTab());

            switch (mTabHost.getCurrentTab()) {
            case 0:
                //do what you want when tab 0 is selected
                break;
            case 1:
                //do what you want when tab 1 is selected
                break;
            case 2:
                //do what you want when tab 2 is selected
                break;

            default:

                break;
            }
        }
    });

And remove the implements OnClickListener.

like image 192
amp Avatar answered Feb 23 '23 01:02

amp


getTabWidget().getChildAt(3).setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        if (getTabHost().getCurrentTabTag().equals(sort)) {
            Events.reloadMe()
        }
    }
});

I would create a static method in the Activity you want to reload and call it like this. I think this is the easiest way to solve it. You will need some private static variables in the Activity to use this. And make sure these private static Objects aren't null.

like image 31
Joelmob Avatar answered Feb 23 '23 01:02

Joelmob