在选项卡片段中设置ViewPager

时间:2013-03-29 12:46:00

标签: java android android-fragments android-viewpager android-tabs

我在Android ActionBar标签中设置ViewPager时遇到问题。 我是开发应用程序的新手,并尝试通过反复试验使一切工作正常,但这个很难。

我想拥有什么
两个操作栏选项卡各自包含ViewPager和一些片段。

到目前为止我做了什么
MainActivity:

public class MainActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // load ActionBar
        ActionBar actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        actionBar.setCustomView(R.layout.actionbar_view);
        actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
        actionBar.setDisplayShowHomeEnabled(true);

        tabA = actionBar.newTab().setText("Tab A");
        tabB = actionBar.newTab().setText("Tab B");
        Fragment fragmentA = new FragmentA();
        Fragment fragmentB = new FragmentB();
        tabA.setTabListener(new MyTabListener(fragmentA));
        tabB.setTabListener(new MyTabListener(fragmentB));
        actionBar.addTab(tabA);
        actionBar.addTab(tabB);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    class MyTabListener implements ActionBar.TabListener {
        public Fragment frag;

        public MyTabListener(Fragment frag) {
            this.frag = frag;
        }

        public void onTabReselected(Tab tab, FragmentTransaction ft) {
            // on tab reselected
        }

        public void onTabSelected(Tab tab, FragmentTransaction ft) {
            // need to create own FragmentTransaction cause ActionBar doesn'T support 'android.support.v4'
            android.support.v4.app.FragmentTransaction fft = mActivity.getSupportFragmentManager().beginTransaction();
            fft.replace(R.id.main_fragment_container, frag);
            fft.commit();
        }

        public void onTabUnselected(Tab tab, FragmentTransaction ft) {
            // need to create own FragmentTransaction cause ActionBar doesn'T support 'android.support.v4'
            android.support.v4.app.FragmentTransaction fft = mActivity.getSupportFragmentManager().beginTransaction();
            fft.remove(frag);
            fft.commit();
        }
    }

}

FragmentA.class

public class FragmentA extends Fragment {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
        return inflater.inflate(R.layout.layout_A, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        ViewPager mViewPager = (ViewPager) view.findViewById(R.id.tabA_pager);
        mViewPager.setAdapter(new MyPagerAdapter(getChildFragmentManager()));
    }
}

MyPagerAdapter.class

public class MyPagerAdapter extends FragmentPagerAdapter {

    public MyPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        Fragment f = new Fragment();
        switch (position) {
        case 0:
            f = LayoutOne.newInstance(null);
            break;
        case 1:
            f = LayoutTwo.newInstance(null);
            break;
        case 2:
            f = LayoutThree.newInstance(null);
            break;
        }
        return f;
    }

    @Override
    public int getCount() {
        return 3;
    }
}

LayoutOne.class

public class LayoutOne extends Fragment {

    public static Fragment newInstance(Context context) {
        LayoutOne f = new LayoutOne();
        return f;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        TextView root = (TextView) inflater.inflate(R.layout.layout_one, null);
        return root;
    }
}



问题是什么
应用程序启动没有问题,但是标签A的片段加载似乎搞砸了,因为测试文本没有显示。 当我切换到标签B然后回到标签A时,我得到FC,说:     java.lang.IllegalStateException:活动已被销毁     ...     在FragmentA.onViewCreated 所以我猜ViewPager适配器存在问题。

我做了一些搜索,但找不到包含此显式布局的工作版本。 有人在6个月前发布了similar question,但没有解决方案。

我的设置有什么问题?

0 个答案:

没有答案