Replace snippet that was added as tabhost tab

I'm having the following problem with my Android app:

I have a TabHost containing some snippets as tabs. These fragments were added programmatically. Now, I would like to replace the contents of the tab with another snippet. And I don't know how to achieve this. I found how to replace a snippet that was added via xml, but this I am not trying to achieve.

Thanks in advance for any suggestions.

EDIT: Requested source (only relevant parts, if more needed tell me)

I have a method to initialize a tabust and add the tabs / fragments programmatically:

private void initialiseTabHost(Bundle args) {
    mTabHost = (TabHost)findViewById(android.R.id.tabhost);
    mTabHost.setup();

    addUserListTab(args);
    //methods to add other tabs


    mTabHost.setOnTabChangedListener(this);
}

private void addUserListTab(Bundle args){
    TabInfo tabInfo = null;
    LayoutParams tabIconParams = new LayoutParams(200, 110);
    tabIconParams.setMargins(0, 0, 0, 0);
    View userIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator,mTabHost, false);
    ImageView userIcon = (ImageView) userIndicator.findViewById(R.id.tab_icon);  
    userIcon.setLayoutParams(tabIconParams);
    TabSpec specUser = mTabHost.newTabSpec("user");
    specUser.setIndicator(userIndicator);
    userIcon.setImageDrawable(getResources().getDrawable(R.drawable.selector_user));
    userIcon.setScaleType(ImageView.ScaleType.FIT_CENTER);

    AddTab(this, this.mTabHost, specUser, ( tabInfo = new TabInfo("user", ListUserFragment.class, args)));
    this.mapTabInfo.put(tabInfo.tag, tabInfo);

}
private static void AddTab(MainActivity activity, TabHost tabHost, TabHost.TabSpec tabSpec, TabInfo tabInfo) {
    // Attach a Tab view factory to the spec
    tabSpec.setContent(activity.new TabFactory(activity));
    tabHost.addTab(tabSpec);
}

      

The problem is I don't understand how I can replace the ListUserFragment that I added programmatically to the tabhost. When I pass the container to the tabhost, how can I specify which tab of the four should currently contain the new snippet, and of course the old should be removed / hidden.

+3


source to share


1 answer


You need to get FragmentManager

and start FragmentTransaction

to replace the current one Fragment

with another.

 getFragmentManager().beginTransaction().replace(R.id.container, new Fragment()).commit();

      



Be sure to read the official snippet description. ...

+1


source







All Articles