1

我有一个托管 FragmentTabHost 的 FragmentActivity。目标是在第一个选项卡中包含可滑动的内容。

所以第一个选项卡承载了 FragmentPagerAdapter 和 ViewPager。该应用程序编译良好,但第一个选项卡中没有显示任何内容,并且不可滑动。

我还是 android 的新手,我被困住了。我希望有人能帮助我。这是我的相关代码:

主要活动:

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);     
    }        
}

页面适配器:

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 个具有相同文本的可滑动页面作为开始。

4

0 回答 0