How to change the focus of tabs on a Tabhost?

Asked

Viewed 495 times

1

I have the following code.

private void TabsCadastro(int visualizar,int origem)
{
    TabSpec abaCLiente,abaEndereco,abaContrato;
    TabHost tabHost = getTabHost();


    abaCLiente = tabHost.newTabSpec("tag1");
    abaCLiente.setContent(R.id.clientes);
    abaCLiente.setIndicator("Cliente");


    abaEndereco = tabHost.newTabSpec("tag2");
    abaEndereco.setContent(R.id.endereco);
    abaEndereco.setIndicator("Endereço");


    abaContrato = tabHost.newTabSpec("tag3");
    abaContrato.setContent(R.id.contrato);
    abaContrato.setIndicator("Contrato");
    tabHost.setFocusableInTouchMode(true);


    if(origem == 0)
    {
        tabHost.addTab(abaCLiente);
        tabHost.addTab(abaEndereco);
        tabHost.addTab(abaContrato);
    }
    tabHost.getTabWidget().setEnabled(false);

    tabHost.setCurrentTab(0);

}

Would you like to know how to change the focus of tabs? I would like to create a button to change the focus instead of clicking tabs for this. I will put a button in each tab to navigate between them I tried to use

    setFocused(boolean),
    setFocusable(boolean),

but I couldn’t change the focus.

  • 1

    You have to use the setCurrentTab() to change tab, something like: MainActivity.TabHost.setCurrentTab(1);.

  • I don’t think you should use Tabhost anymore, because it’s too old-fashioned. Use Viewpager with that library here: https://github.com/bitjjj/PagerSlidingTitleIconTabStrip

1 answer

1


This is what I usually do:

private void initialiseTabHost(Bundle args) {
    tabHost = (TabHost) findViewById(android.R.id.tabhost);
    tabHost.setup();
    AddTab(this, this.tabHost, this.tabHost.newTabSpec("Tab1")
            .setIndicator(addButton("Tab1")));
    AddTab(this, this.tabHost, this.tabHost.newTabSpec("Tab2")
            .setIndicator(addButton("Tab2")));
    AddTab(this, this.tabHost, this.tabHost.newTabSpec("Tab3")
            .setIndicator(addButton("Tab3")));
    tabHost.setOnTabChangedListener(this);
}

public Button addButton(String texto) {
    Button button = new Button(getApplicationContext());
    RelativeLayout.LayoutParams layoutParamsRelative = new RelativeLayout.LayoutParams(
            0, 0);
    button.setLayoutParams(layoutParamsRelative);
    button.setText(texto);
    return button;
}

private void intialiseViewPager() {
    List<Fragment> fragments = new Vector<Fragment>();
    fragments.add(new Fragment1());
    fragments.add(new Fragment2());
    fragments.add(new Fragment3());

    pagerAdapter = new PagerAdapter(super.getSupportFragmentManager(),
            fragments);
    viewPager = (ViewPager) super.findViewById(R.id.viewpager);
    viewPager.setAdapter(pagerAdapter);
    viewPager.setOnPageChangeListener(this);
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.