如何动态创建Fragment寻呼机适配器

时间:2015-09-30 06:06:52

标签: android android-fragments fragmentpageradapter

我无法为每个标签创建动态布局,我可以通过此构造函数动态创建标签(公共MyPagerAdapter(FragmentManager fm,int noOfTabs)),但是coudn不会为每个标签创建的视图每个片段应该是动态的。

public class MyPagerAdapter extends FragmentPagerAdapter 
{

        private final String[] TITLES = { "Categories", "Home", "Top Paid", "Top Free", "Top Grossing", "Top New Paid",
                "Top New Free", "Trending" };

        int noOfTabs;

        **public MyPagerAdapter(FragmentManager fm , int noOfTabs) 
        {           
            super(fm);
            this.noOfTabs = noOfTabs;
        }**

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

        @Override
        public CharSequence getPageTitle(int position) {
            return TITLES[position];
        }

        @Override
        public int getCount() {
            return noOfTabs; //TITLES.length;
        }

        @Override
        public Fragment getItem(int position) {
            return SuperAwesomeCardFragment.newInstance(position);
        }

    }

我在这里面临的问题应该是动态的而不是静态的三个片段(FragmentA,FragmentB,FragmentC),它可以是任意数量的片段,具体取决于int noOfTabs:

@Override
public Fragment getItem(int position) {
    Fragment fragment =null;
    switch (position) {
        case 0:
            fragment = new FragmentA();
            break;
        case 1:
            fragment = new FragmentB();
            break;              
        case 2:
            fragment = new FragmentC();
            break;          }
    return fragment;    
}

如果有人可以帮助我,请提前感谢,因为我在图书馆PagerSlidingTabStrip的帮助下使用它。

2 个答案:

答案 0 :(得分:9)

您可以通过在PagerAdapter中存储所有片段及其标题的列表来实现此目的:

/**
 * TabsPagerAdapter.
 *
 * @author Niels
 * @version 1.0
 * @since 11-9-2015
 */
public class TabsPagerAdapter extends FragmentPagerAdapter {

    /**
     * Contains all the fragments.
     */
    private List<Fragment> fragments = new ArrayList<>();

    /**
     * Contains all the tab titles.
     */
    private List<String> tabTitles = new ArrayList<>();

    /**
     * Creates a new PagerAdapter instance.
     *
     * @param fragmentManager The FragmentManager.
     */
    public TabsPagerAdapter(FragmentManager fragmentManager) {
        super(fragmentManager);
    }

    @Override
    public int getCount() {
        return fragments.size();
    }

    @Override
    public Fragment getItem(int position) {
        return fragments.get(position);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return tabTitles.get(position);
    }

    /**
     * Adds the fragment to the list, also adds the fragment's tab title.
     *
     * @param fragment New instance of the Fragment to be associated with this tab.
     * @param tabTitle A String containing the tab title for this Fragment.
     */
    public void addFragment(Fragment fragment, String tabTitle) {
        fragments.add(fragment);
        tabTitles.add(tabTitle);
    }

}

您可以这样使用:

@Override
public void onCreate() {
    adapter = new TabsPagerAdapter(getSupportFragmentManager()); //TabsPagerAdapter = whatever you named the PagerAdapter
    addFragments();
    viewpager.setAdapter(adapter); //viewpager = ViewPager view instance
    strip.setViewPager(viewpager); //strip = PagerSlidingTabStrip view instance
}

/**
 * Adds fragments to the adapter.
 */
private void addFragments() {
    adapter.addFragment(new Fragment1(), "Fragment1");
    adapter.addFragment(new Fragment2(), "Fragment2");
    adapter.addFragment(new Fragment3(), "Fragment3");
    adapter.addFragment(new Fragment4(), "Fragment4");
}

答案 1 :(得分:1)

@nbokmans解决方案可能没问题,但我已经使用容器来实现这一目标。

使用FrameLayout创建一个简单的宿主片段并替换片段对我来说是一个很好的动态解决方案。

使用Otto EventBus让它变得非常棒,因为它可以通过简单的帖子从任何地方进行更改

  @Subscribe
fun changeFragment(type: FragmentTypeEvent){
    val ft = activity.supportFragmentManager.beginTransaction()
    val frag = getFragment(type)
    ft.replace(R.id.container_frame,frag)
    ft.commit()
}