TabWidget.OnTabSelectionChanged cannot be resolved to a type
-
23-06-2021 - |
Frage
I'm trying to create a custom TabHost
. And I want to call the setTabSelectionListener()
method of TabWidget
.
But I have the following error:
TabWidget.OnTabSelectionChanged cannot be resolved to a type
Here is my test code:
public class CustomTabHost {
public CustomTabHost(Activity activity) {
TabWidget tab_widget = (TabWidget) activity.findViewById(R.id.tab_widget);
tab_widget.setTabSelectionListener(new TabWidget.OnTabSelectionChanged() {
public void onTabSelectionChanged(int tabIndex, boolean clicked) {
}
});
}
}
I'm using Eclipse and Android 2.2.
Thanks in advance!
Lösung
Instead of using the setTabSelectionListener why dont you use the .OnchangeTabListener as shown : How to use TabHost.OnTabChangeListener in android?
public class ListFragment extends Fragment implements OnTabChangeListener{
View v;
TabHost tabHost;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
v = inflater.inflate(R.layout.main_menu_libraries, container, false);
tabHost=(TabHost) v.findViewById(R.id.tabHost);
tabHost.setup();
TabSpec spec1=tabHost.newTabSpec("Tab 1");
spec1.setContent(R.id.tab1);
spec1.setIndicator("About",getResources().getDrawable(R.drawable.android));
TabSpec spec2=tabHost.newTabSpec("Tab 2");
spec2.setContent(R.id.tab2);
spec2.setIndicator("Blog",getResources().getDrawable(R.drawable.android));
tabHost.addTab(spec1);
tabHost.addTab(spec2);
tabHost.setOnTabChangedListener(this);
return v;
}
@Override
public void onTabChanged(String arg0) {
if (tabHost.getCurrentTab()==0)
{
//your code here
}
if (tabHost.getCurrentTab()==1)
{
//your code here
}
Also in the part where it says //your code here ... try calling a new class which extends Fragment and inflate the particular fragment with the menu !
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow