替换Viewpager中的片段

时间:2015-05-22 10:09:46

标签: android android-fragments android-viewpager nested

我能够在AFragment之上获得DFragment,并且在推回DFragment时我正在获得AFragment。但是我面临的问题是当显示AFragment时,BFragment和CFragment会显示各自的布局。但是当我从A遍历到D时,B和C只显示D的布局。

我在Adapter上尝试过notifyDataSetChanged但仍然无法正常工作。请帮忙

我的主要活动

public class MainActivity extends FragmentActivity {

            ViewPager pager;
            ViewPagerAdapter adapter;
            SlidingTabLayout tabs;
            CharSequence tabTitles[] = {"A", "B", "C"};
            int numTabs = 3;

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

                adapter = new ViewPagerAdapter(getSupportFragmentManager(), tabTitles, numTabs);

                pager = (ViewPager) findViewById(R.id.pager);
                pager.setAdapter(adapter);

                tabs = (SlidingTabLayout) findViewById(R.id.tabs);
                tabs.setDistributeEvenly(true);

                tabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
                    @Override
                    public int getIndicatorColor(int position) {
                        return getResources().getColor(R.color.tabsScrollColor);
                    }
                });

                tabs.setViewPager(pager);

            }

        }

这是我的ViewPagerAdapter

public class ViewPagerAdapter extends FragmentStatePagerAdapter {

            CharSequence tabTitles[];
            int numTabs;


            public ViewPagerAdapter(FragmentManager fm,CharSequence tabTitles[], int numTabs) {
                super(fm);

                this.tabTitles = tabTitles;
                this.numTabs = numTabs;

            }

            @Override
            public Fragment getItem(int position) {
                if(position==0)
                    return new AFragment();
                else
                    if(position==1)
                        return new BFragment();
                else
                        return new CFragment();
            }

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

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

这是我的第0个片段

public class AFragment extends Fragment{

        @Override
            public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState){

        View rootView =inflater.inflate(R.layout.a,container,false);
        Button b = (Button)rootView.findViewById(R.id.b);

        b.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                                    ((MainActivity) context).getSupportFragmentManager().beginTransaction().add(R.id.container, new DFragment())
                                    .addToBackStack("").commit();
                        }
                    });

        return rootView;
        }

        }

这是我的第0个片段

之上的新片段
public class DFragment extends Fragment {

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

0 个答案:

没有答案