用于管理标签之外的片段“页面”的Android类?

时间:2013-05-16 18:49:13

标签: android android-fragments

我正在创建一个数据输入应用程序,该应用程序当前分为三个活动 - 登录活动,“主要”活动(用户信息,摘要等)以及单个记录的数据输入活动。这些活动中的每一个都有几个不同的“屏幕”(即包含一组视图的整页布局),每个屏幕都是一个片段。

我有两个问题:

  1. 这是一个合适的整体架构吗?活动/片段的划分有点武断,但这些划分对我来说具有语义意义。

  2. 管理活动中片段之间切换的最佳方法是什么?我的主要活动使用ViewPagerFragmentPagerAdapter,因为它基于标签+滑动导航结构,但所有活动都会包含未表示为菜单项的片段。我是否应该使用FragmentPagerAdapter而不是ViewPager(如果是,如何)?我应该编写自己的抽象层来处理转换吗?

  3. 我已经阅读了许多教程和示例,但没有看到使用这种特定模式。由于我在Android开发方面不是很有经验,所以在我尝试编写自己的解决方案之前,我认为最好先询问是否有现成的解决方案。任何建议表示赞赏。谢谢!

    编辑:这是我目前所拥有的,作为在两个“屏幕”之间切换的非常简化的版本 - 标题页和登录页。这显示了我希望它处理的大部分内容(保存片段实例,管理事务,将它们放入布局中)。似乎它仍然在某些方面复制了PagerAdapter和ViewPager的功能,但我不希望它与菜单,标签,滑动等相关联,因为主要导航将通过应用程序中的按钮。我还需要将一些初始数据传递给片段初始化,以获得更复杂的片段。

    public class LoginFragmentSwitcher {
        private int mCurFragIndex;
        private ArrayList<Fragment> mFragList;
    
        public LoginFragmentSwitcher() {
            //set initial index
            mCurFragIndex = 0;
    
            //create fragments
            mFragList = new ArrayList<Fragment>();
            mFragList.add(new TitleFragment());
            mFragList.add(new LoginFragment());
            //TODO: more fragments will be added here
    
            //display the first fragment
            FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
            ft.add(R.id.fragment_container, mFragList.get(0));
            ft.commit();
        }
    
        // Perform a fragment transition to the specified index
        public void showFragment(int newFragIndex) {
            //only switch if you're not already showing the appropriate fragment
            if (newFragIndex != mCurFragIndex) {
                //start the fragment transaction
                FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    
                //TODO: apply transition style if desired
    
                //switch content to the new fragment
                ft.replace(R.id.fragment_container, mFragList.get(newFragIndex));
    
                //register entry in the back stack and complete transaction
                ft.addToBackStack(null);
                ft.commit();
    
                //update index
                mCurFragIndex = newFragIndex;
            }
        }
    }
    

0 个答案:

没有答案
相关问题