我有一个托管FragmentTabHost的FragmentActivity。 目标是在第一个标签中提供可滑动的内容。
因此,第一个标签包含FragmentPagerAdapter和ViewPager。 该应用程序编译正常,但第一个选项卡中没有显示任何内容,并且无法刷卡。
我还是Android的新手而且我被卡住了。我希望有人可以帮助我。 这是我的相关代码:
MainActivity:
public class MainActivity extends FragmentActivity implements OnTabChangeListener
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initTabs();
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private void initTabs()
{
FragmentTabHost tabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
tabHost.setup(this, getSupportFragmentManager(),R.id.realtabcontent);
tabHost.addTab(tabHost.newTabSpec("Übersicht").setIndicator("Übersicht"), OverviewFragment.class, null); // <- shall contain swipeable content
tabHost.addTab(tabHost.newTabSpec("Einstellungen").setIndicator("Einstellungen"), SettingsFragment.class, null); // <- 'static' content
tabHost.setOnTabChangedListener(this);
}
...
}
OverviewFragment(应托管可滑动内容):
public class OverviewFragment extends Fragment
{
ViewPager _pagerView;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
_pagerView = (ViewPager)inflater.inflate(R.layout.tab_overview, container, false).findViewById(R.id.viewpager);
initPager();
return inflater.inflate(R.layout.tab_overview, container, false);
}
private void initPager()
{
FragmentManager fm = getChildFragmentManager();
PageAdapter pageAdapter = new PageAdapter(fm);
_pagerView.setAdapter(pageAdapter);
}
}
PageAdapter:
public class PageAdapter extends FragmentPagerAdapter
{
public PageAdapter(FragmentManager fm)
{
super(fm);
}
@Override
public Fragment getItem(int arg0)
{
MemoryFragment m1 = new MemoryFragment();
return m1;
}
@Override
public int getCount()
{
return 5;
}
}
MemmoryFragment(应该在FragmentPagerController中显示为页面的片段):
public class MemoryFragment extends Fragment
{
@Override
public void onCreate(Bundle savedInstanceState) {
}
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View v = inflater.inflate(R.layout.memmory, container,false);
TextView tv = (TextView ) v.findViewById(R.id.textViewTest);
tv.setText("You are viewing the page Swipe Horizontally left / right");
return v;
}
}
我知道这段代码没有任何意义。它应该只显示5个具有相同文本的可滑动页面。