不重新加载片段ViewPager

时间:2015-07-24 14:10:08

标签: java android xml android-layout android-fragments

我对片段和ViewPager 有疑问,我的ViewPager由五个标签组成,每个标签都是Fragment

在第一次加载时我对这些加载没有问题,但是当我打开另一个片段时 (在主要前面),返回到主要的我所在的片段,以及那些已被删除(不可见)。

对于修复,我需要在中央页面加载之前“离开”两页。

所有内容仅适用于使用Android Studio的API 21(5.0.1)。

MainActivity.java:

public class MainActivity extends FragmentActivity {

    private MainActivityFragment frag_MainActivity;
    private CreditsFragment frag_Credits;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        frag_MainActivity = new MainActivityFragment(this);
        frag_Credits = new CreditsFragment();
        switchFrag("main");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_home) {
            switchFrag("main");
            return true;
        } else if (id == R.id.action_credits) {
            switchFrag("credits");
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public void switchFrag(String view){
        Fragment frag = new Fragment();
        if(view.equals("main")){
            getActionBar().show();
            frag = new MainActivityFragment(this);
        } else if(view.equals("credits")){
            getActionBar().hide();
            frag = new CreditsFragment();
        }
        FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction()
                .replace(R.id.frame_container, frag)
                .addToBackStack(null).commit();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
                switchFrag("main");
            return true;
        }
        return super.onKeyUp(keyCode, event);
    }

    public void closeApp() {
        finish();
        System.exit(0);
    }

XML(activity_main.xml):

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <FrameLayout
        android:id="@+id/frame_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        </FrameLayout>
</RelativeLayout>

MainActivityFragment.java:

public class MainActivityFragment extends Fragment implements ActionBar.TabListener {
    private ViewPager viewPager;
    private MyAdapter mAdapter;
    private ActionBar actionBar;
    private String[] tabs = { "Lun", "Mar", "Merc", "Gio", "Ven"};

    MainActivity main;

    public MainActivityFragment(MainActivity instance) {
        main = instance;
    }
    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        viewPager = (ViewPager) main.findViewById(R.id.pager);
        actionBar = main.getActionBar();
        mAdapter = new MyAdapter(main.getSupportFragmentManager());

        viewPager.setAdapter(mAdapter);
        actionBar.setHomeButtonEnabled(false);
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        if(actionBar.getTabCount() == 0){
            for (String tab_name : tabs) {
                actionBar.addTab(actionBar.newTab().setText(tab_name)
                        .setTabListener(this));
            }
        }
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                actionBar.setSelectedNavigationItem(position);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
            }
        });
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_main, container, false);
    }
    @Override
    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {}
    @Override
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
        viewPager.setCurrentItem(tab.getPosition());
    }
    @Override
    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {}

XML(fragment_settings.xml):

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivityFragment">
    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</RelativeLayout>

如果需要适配器,请执行以下操作:

public class MyAdapter extends FragmentPagerAdapter {
    public MyAdapter(FragmentManager fm) {
        super(fm);
    }

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

    @Override
    public android.support.v4.app.Fragment getItem(int position) {
        switch(position){
            case 0:
                return new LunFragment();
            case 1:
                return new MarFragment();
            case 2:
                return new MerFragment();
            case 3:
                return new GioFragment();
            case 4:
                return new VenFragment();
        }
        return null;
    }
}

1 个答案:

答案 0 :(得分:1)

mAdapter = new MyAdapter(main.getSupportFragmentManager());替换为mAdapter = new MyAdapter(main.getChildFragmentManager());。您需要将片段创建为MainActivityFragment的内部片段。