Change view inside TabHost (one action, several views)

right now i am using tabhost in my application with one activity and separate views.

Tab Activity:

super.onCreate(savedInstanceState); setContentView(R.layout.main); TabHost tabs = (TabHost) findViewById(R.id.tabhost); tabs.setup(); TabHost.TabSpec spec = tabs.newTabSpec(TAB_HOTELS); spec.setContent(R.id.tab1); spec.setIndicator("Hotels"); tabs.addTab(spec); spec = tabs.newTabSpec(TAB_LAST_MINUTE); spec.setContent(R.id.tab2); spec.setIndicator("Last Minute"); tabs.addTab(spec); 

Markup

 <?xml version="1.0" encoding="utf-8"?> <TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/tabhost" android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/tab1" android:orientation="vertical"> <ListView android:id="@+id/listaLocalita" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/tab2" android:orientation="vertical" android:paddingTop="60px"> <TextView android:layout_width="fill_parent" android:layout_height="100px" android:text="This is tab 2" android:id="@+id/txt2" /> </LinearLayout> </FrameLayout> </LinearLayout> </TabHost> 

And everything works very well, as long as I use one level depth. Unfortunately, now I need to make one of the tabs to work as follows:

The Load ListView tab with a list of cities, the user clicks on the name of the city, the contents of the tab are replaced with a list of hotels, the user selects a hotel and is always on the same tab, information about the hotel is loaded.

How can I achieve this scenario? LayoutInflater?

Thanks in advance.

+4
source share
1 answer

you can use the intent to set the Activity as the content of your tab, and this action can trigger other actions - listView or any other.

 /** tid1 is firstTabSpec Id. Its used to access outside. */ TabSpec MainTab = tabHost.newTabSpec("tag1"); TabSpec SearchTab = tabHost.newTabSpec("tag2"); TabSpec MessageTab = tabHost.newTabSpec("tag3"); TabSpec ServicesTab = tabHost.newTabSpec("tag4"); TabSpec SettingsTab = tabHost.newTabSpec("tag5"); MainTab.setIndicator("Main", res.getDrawable(R.drawable.anchor_36)); MainTab.setContent(new Intent(this, MainView.class)); SearchTab.setIndicator("Search", res.getDrawable(R.drawable.clock_36)); SearchTab.setContent(new Intent(this, SearchView.class)); MessageTab.setIndicator("Messages", res .getDrawable(R.drawable.dialog_36)); MessageTab.setContent(new Intent(this, MessagesView.class)); ServicesTab.setIndicator("Services", res .getDrawable(R.drawable.cloud_36)); ServicesTab.setContent(new Intent(this, ServicesView.class)); SettingsTab.setIndicator("Settings", res .getDrawable(R.drawable.settings_36)); SettingsTab.setContent(new Intent(this, SettingsView.class)); /** Add tabSpec to the TabHost to display. */ tabHost.addTab(MainTab); tabHost.addTab(SearchTab); tabHost.addTab(MessageTab); tabHost.addTab(ServicesTab); tabHost.addTab(SettingsTab); 

Now in loaded activity you can use code like

 StartActivity(new intent(currentActivity.this, newActivity.class)); 

This will work for you.

0
source

Source: https://habr.com/ru/post/1334978/


All Articles