片段未更新ViewPageAdapter

时间:2018-10-18 14:20:16

标签: android android-fragments android-viewpager

当前,我正在从事一个项目,在该项目中,我实现了viewPager和底部导航视图以更改片段。我也可以左右滑动以更改片段。

当我在导航视图上单击一个项目时,该片段将变为附加的片段。我的底部导航视图是这样设置的:

enter image description here

这个问题很难解释,但这是我的尝试:

这些片段仅在我进入片段“ Home”之后才更新。

这意味着,当我到“家”之后单击“ Oversigt”时,片段“ Oversigt”将更新其视图。但是,如果我不在“主页”和其他片段之一中,则单击片段“ Oversigt”。然后,“ Oversigt”将不会更新其视图。

我的MainActivity:

public void setViewPager(int fragmentNumber) {

    mPager.setCurrentItem( fragmentNumber );

}

public void enableViewPagerSwitch() {

    mPager.addOnPageChangeListener( new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {

            if (prevMenuItem != null) {
                prevMenuItem.setChecked( false );
            } else {
                mBottomNav.getMenu().getItem( 0 ).setChecked( false );
                navigationView.getMenu().getItem( 0 ).setChecked( false );

            }
            Log.d( "page", "onPageSelected: " + position );

            mBottomNav.getMenu().getItem( position ).setChecked( true );
            navigationView.getMenu().getItem( position ).setChecked( true );

            prevMenuItem = mBottomNav.getMenu().getItem( position );
            prevMenuItem = navigationView.getMenu().getItem( position );

        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    } );

    setupViewPager( mPager );

}


public void bot_Navigation() {

    mBottomNav = (BottomNavigationView) findViewById( R.id.nav_bot );

    botNavHelper.disableShiftMode( mBottomNav );

    mBottomNav.setOnNavigationItemSelectedListener( new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {

                case R.id.bot_startside:
                    setViewPager( 0 );
                    break;

                case R.id.bot_datatabel:
                    setViewPager( 1 );
                    break;

                case R.id.bot_sensorOversigt:
                    setViewPager( 2 );
                    break;

                case R.id.bot_kontakt:
                    setViewPager( 3 );
                    break;

                case R.id.bot_logUd:
                    setViewPager( 4 );
                    alertDialog();
                    break;
            }

            return true;
        }
    } );

}

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

    onCreate2();

    bot_Navigation();

}

protected void onCreate2() {

    setContentView( R.layout.activity_main );
    Toolbar toolbar = (Toolbar) findViewById( R.id.toolbar );
    setSupportActionBar( toolbar );

    TextView mTitle = (TextView) toolbar.findViewById( R.id.toolbartitle );

    mTitle.setText( toolbar.getTitle() );

    getSupportActionBar().setDisplayShowTitleEnabled( false );

    mPager = (ViewPager) findViewById( R.id.flMain );

    enableViewPagerSwitch();

    DrawerLayout drawer = (DrawerLayout) findViewById( R.id.drawer_layout );
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close );
    drawer.addDrawerListener( toggle );
    toggle.syncState();

    navigationView = (NavigationView) findViewById( R.id.nav_view );
    navigationView.setNavigationItemSelectedListener( this );

    navigationView.setCheckedItem( R.id.nav_startside );

}

这是ViewPageAdapter:

public class ViewPageAdapter extends FragmentStatePagerAdapter {

private final List<Fragment> mFragmentList = new ArrayList<>();

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

public void addFragment(Fragment fragment) {
    mFragmentList.add( fragment );
}

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

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

}

1 个答案:

答案 0 :(得分:0)

使用SmartFragmentStatePagerAdapter

public abstract class SmartFragmentStatePagerAdapter extends FragmentStatePagerAdapter implements Serializable {
    // Sparse array to keep track of registered fragments in memory
    private SparseArray<Fragment> registeredFragments = new SparseArray<Fragment>();

    public SmartFragmentStatePagerAdapter(FragmentManager fragmentManager) {
        super(fragmentManager);
    }

    // Register the fragment when the item is instantiated
    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        Fragment fragment = (Fragment) super.instantiateItem(container, position);
        registeredFragments.put(position, fragment);
        return fragment;
    }

    // Unregister when the item is inactive
    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        registeredFragments.remove(position);
        super.destroyItem(container, position, object);
    }

    // Returns the fragment for the position (if instantiated)
    public Fragment getRegisteredFragment(int position) {
        return registeredFragments.get(position);
    }

,根据您在哪里创建这些片段,您可能想尝试使用getChildFragmentManager来代替。另一种方法是使用https://developer.android.com/reference/android/support/v4/app/FragmentStatePagerAdapter