开发者

Change content of tab on button click

I have been reading a lot of comments on how it is more lightweight to use views instead of intents in setContent when using tabs.

Since I will be creating an app with tabs, I am trying to implement this instead of having intents inside setContent. However, I am having a difficult time looking for examples or straightforward tutorials on swapping the current view with another one.

Say I have TAB_A, with some entry fields and a button. When the user clicks on the button, a TextView will display "Hello, user!" in the same TAB_A. Of course this is an oversimplified example, but it will surely point me in the right direction.

EDIT: I have a tab called TAB_A with a button in it. If the user clicks on that button, TAB_A will now display "Hello,开发者_如何学编程 user!" instead of the button.

Here is a sample of my TabSpec.

TabHost.TabSpec spec = tabHost.newTabSpec(getString(R.string.tabspec_tag_search))
                .setIndicator(getString(R.string.tabspec_indic_search),
                        res.getDrawable(R.drawable.tab_ic_search))
                .setContent(new TabContentFactory() {
                @Override
                public View createTabContent(String tag) {
                   switch (doWhat){
                    case ACTIVITY_SPLASH_PAGE:
                                       return showButton;
                    case ACTIVITY_HELLO:
                                       return showHello;
                   }
                       return null;
                }
            });
        tabHost.addTab(spec);

Where ShowButton and ShowHello are classes both extending LinearLayout. Both are initialized in onCreate like this: ShowButton showButton = new ShowButton(this);

Inside the showButton class, I have this listener:

viewHolder.button.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent intent = new Intent(context, Search247.class);
        intent.putExtra(EXTRA_DO_WHAT, ACTIVITY_HELLO);
        context.startActivity(intent);
    }
});

When the app first starts, the user sees the view showButton. When the user clicks on the button, I want the tab to change its contents to showHello.

The question then is: Is there any way to update the contents of the tabspec without using startActivity?

Thanks as always!


Found the answer:

ViewGroup viewGroup = (ViewGroup) mView.getParent();
viewGroup.removeView(mView);
addView(showResults);

Where mView is the initial view in the tab. Now just trying to figure out how to make the app remember to show mView when BACK is pressed from the new view.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜