TabHost does not show content at the beginning

I have a TabActivity with two tabs. the layout of the action is as follows:

<?xml version="1.0" encoding="utf-8"?> <TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android: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" android:padding="5dp"> <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" android:padding="5dp"> <ListView android:id="@+id/list" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </FrameLayout> </LinearLayout> </TabHost> 

The layout consists of a ListView, which is populated accordingly in setOnTabChangedListener (). no problems filling out the list and displaying it.

My problem is that the list view is not displayed when the action starts, although I find it by identifier and populate it; it is filled only after changing tabs.

and the code in onCreate (..) looks like this:

 l = (ListView) findViewById(R.id.list); l.setAdapter(new CommentsAdapter(this, (JSONArray) obj)); TabSpec spec; spec = tabHost.newTabSpec("0").setIndicator("0", res.getDrawable(R.drawable.tab_recent)).setContent( R.id.list); tabHost.addTab(spec); spec = tabHost.newTabSpec("1").setIndicator("1", res.getDrawable(R.drawable.tab_pop)).setContent( R.id.list); tabHost.addTab(spec); tabHost.setOnTabChangedListener(new OnTabChangeListener() { @Override public void onTabChanged(String tabId) { // TODO Auto-generated method stub int tab = Integer.valueOf(tabId); showDialog(WAIT_DIALOG); switch (tab) { // recent case 0: //refill the list break; // popular case 1: //refill the list break; } } }); tabHost.setCurrentTab(0); 

any clues?

+4
source share
3 answers

I had a similar problem. I noticed that if you do

 tabHost.setCurrentTab(0); 

then onTabChanged does not start. BUT if you do

 tabHost.setCurrentTab(1); 

then this is a list and all is good. Why, why is this happening, for me it's a little bit.

Use this cheat to launch the application on the first tab: in onCreate (), after you first created the tab: tabHost.setCurrentTab (1); Then call: tabHost.setCurrentTab (0);

+8
source

You can try using a different layout, say

 spec = tabHost.newTabSpec("0").setIndicator("0", res.getDrawable(R.drawable.tab_recent)).setContent( R.id.list1); tabHost.addTab(spec); spec = tabHost.newTabSpec("1").setIndicator("1", res.getDrawable(R.drawable.tab_pop)).setContent( R.id.list2); tabHost.addTab(spec); 
+1
source

you just perform another action, in which there is a list view, and put below

 Intent i1 = new Intent(tabs.this,listDisplay.class); spec=tabHost.newTabSpec("0").setIndicator("0",res.getDrawable(R.drawable. tab_recent)).setContent(i1); 

Also set tabHost.setCurrentTab(0); before ontabChangeListener Hope this helps ..

Hope this can solve the problem.

-2
source

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


All Articles