3

I am working on an application where I have to change the view of one tab when tab is changed. I am doing it following way using OnTabChangedListener. I am able to replace view. But when I change my tab to some other tab, previous view remain tapable in the background? Why is it so? In front, new view is populated but If I tap anywhere on the screen where previous view's controls were, the functionality is executed. I am not getting where may the problem lie. Please help me out. Thanks!

public class FragmentTabs extends FragmentActivity implements OnTabChangeListener {

private FragmentTabHost mTabHost;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    this.requestWindowFeature(Window.FEATURE_NO_TITLE);

    setContentView(R.layout.fragment_tabs);

    mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);

    mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);

    mTabHost.setOnTabChangedListener(this);

    if (Database.getSharedObject(getApplicationContext()).getAppSettings().getListView() == 1) {
        mTabHost.addTab(mTabHost.newTabSpec("home").setIndicator("", getResources().getDrawable(R.drawable.home_tab)), HomeFragment.class, null);

    } else {
        mTabHost.addTab(mTabHost.newTabSpec("home").setIndicator("", getResources().getDrawable(R.drawable.home_tab)), CalendarViewFragment.class, null);
    }
    mTabHost.addTab(mTabHost.newTabSpec("groups").setIndicator("", getResources().getDrawable(R.drawable.groups_tab)), GroupFragment.class, null);
    mTabHost.addTab(mTabHost.newTabSpec("templates").setIndicator("", getResources().getDrawable(R.drawable.templates_tab)), TemplateFragment.class, null);
    mTabHost.addTab(mTabHost.newTabSpec("settings").setIndicator("", getResource
    mTabHost.getTabWidget().getChildAt(0).getLayoutParams().height = height;
    mTabHost.getTabWidget().getChildAt(1).getLayoutParams().height = height;
    mTabHost.getTabWidget().getChildAt(2).getLayoutParams().height = height;
    mTabHost.getTabWidget().getChildAt(3).getLayoutParams().height = height;
    // Database.getSharedObject(getApplicationContext());

    mTabHost.setCurrentTab(0);
}

@Override
protected void onResume() {
    super.onResume();

}

@Override
public void onTabChanged(String arg0) {

    mTabHost.getCurrentTabView().invalidate();

    if (mTabHost.getCurrentTab() == 0) {
        if (Database.getSharedObject(getApplicationContext()).getAppSettings().getListView() == 1) {

            HomeFragment homeFragment = new HomeFragment();
            mTabHost.getCurrentTabView().invalidate();
            getSupportFragmentManager().beginTransaction().replace(R.id.realtabcontent, homeFragment).addToBackStack(null).commit();
        } else {

            CalendarViewFragment calendarViewFragment = new CalendarViewFragment();
            mTabHost.getCurrentTabView().invalidate();
            getSupportFragmentManager().beginTransaction().replace(R.id.realtabcontent, calendarViewFragment).addToBackStack(null).commit();

        }

    }

}

}

Usama Sarwar
  • 8,922
  • 7
  • 54
  • 80
  • I have this issue too. I'll answer if I get it figured out but I know exactly the problem you are having. I think it might have to do with the fact that we are not using `android.R.id.content` for our tabs but I haven't tested it so it may not be the issue at all. Did you make any progress in the last three weeks? – dcow Apr 10 '13 at 16:08
  • It also might have to do with the fact that we call `setContentView` which they don't do in the developer docs (for using `OnTabChangedListener` with the ActionBar anyway). I don't think it should matter that you're using a TabHost and I'm using the ActionBar (I bet the ActionBar just uses a TabHost anyway. Perhaps the issue lies in the FragmentManager.. – dcow Apr 10 '13 at 16:10

1 Answers1

0

Add android:clickable="true" to your new tab's root layout. It will prevent click events to be passed through the layouts behind.

minipif
  • 4,756
  • 3
  • 30
  • 39