如标题中所述,我有一个从 a 加载的ExpandableListView
内部TabHost
Fragment
我可以在ExpandableListView
没有的情况下正确显示,TabHost
但是一旦我添加了TabHost
它就不会显示。
我也得到了它,这样当我更改选项卡时,列表将显示,但在初始加载视图时,ExpandableListView
它不存在。
这是代码:
public class Categories extends Fragment {
SearchView searchView; //Not doing anything yet
LayoutInflater inflater;
ExpandableListView expandableListView;
TabHost tabHost;
private ArrayList<String> parentItems = new ArrayList<String>();
private ArrayList<Object> childTitles = new ArrayList<Object>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
{
LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.categories_tabs, null);
searchView = (SearchView) layout.findViewById(R.id.searchView);
this.inflater = inflater;
tabHost = (TabHost) layout.findViewById(R.id.tabhost); // The activity TabHost
tabHost.setup();
TabHost.TabSpec spec; // Reusable TabSpec for each intent;
try
{
//setup tab name, display text, and layout
spec = tabHost.newTabSpec(getResources().getString(R.string.events))
.setIndicator(" " + getResources().getString(R.string.events) + " ")
.setContent(R.id.categoriesLayout); //must be id, not layout or xml
//add tab
tabHost.addTab(spec);
spec = tabHost.newTabSpec(getResources().getString(R.string.venues))
.setIndicator(" " + getResources().getString(R.string.venues) + " ")
.setContent(R.id.categoriesLayout); //must be id, not layout or xml
tabHost.addTab(spec);
//finish adding tabs
tabHost.getTabWidget();
}
catch(Exception e)
{
Log.v(getActivity().getResources().getString(R.string.app_name), "Failed to load tab host");
}
tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener()
{
@Override
public void onTabChanged(String s)
{
displayTab();
}
});
return layout;
}
private void displayTab()
{
//set parentItems - not shown
setGroupParents();
//set childTitles - not shown
setChildData();
MyExpandableAdapter adapter = new MyExpandableAdapter(parentItems, childTitles, childDescriptions);
adapter.setInflater(inflater, getActivity());
expandableListView.setAdapter(adapter);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState)
{
super.onViewCreated(view, savedInstanceState);
expandableListView = (ExpandableListView) view.findViewById(R.id.list);
displayTab();
}
运行该displayTab
功能会显示ExpandableListView
切换选项卡时的信息,但我需要它在加载屏幕后立即显示列表。我究竟做错了什么?
附录
好的,所以现在我真的很困惑......如果我把
tabHost.setCurrentTab(1);
tabHost.setCurrentTab(0);
在我onStart()
的ExpandableListView
节目中,但如果我只是把
displayTab();
在那里,它没有显示。所有OnTabChangeListener
正在做的就是运行displayTab
......