ViewPager中的android Fragment无法更新

时间:2016-07-22 05:04:52

标签: android android-fragments android-viewpager

我创建3 Fragment使用ViewPager,这是ViewPagerAdapter:

public class ViewPagerAdapter extends FragmentStatePagerAdapter {
int mNumOfTabs;

public ViewPagerAdapter(FragmentManager fm, int NumOfTabs) {
    super(fm);
    this.mNumOfTabs = NumOfTabs;
}

@Override
public Fragment getItem(int position) {

    switch (position) {
        case 0:
            FragFile tab1 = new FragFile();
            return tab1;
        case 1:
            FragAll tab2 = new FragAll();
            return tab2;
        case 2:
            FragFav tab3 = new FragFav();
            return tab3;
        default:
            return null;
    }
}

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

}

这是Activity1 hold 3 Fragment:

public class Acc1 extends AppCompatActivity {

ViewPager view_pager;

TabLayout tab_layout;

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

    tab_layout = (TabLayout) findViewById(R.id.tab_layout);
    tab_layout.addTab(tab_layout.newTab().setText("File"));
    tab_layout.addTab(tab_layout.newTab().setText("All"));
    tab_layout.addTab(tab_layout.newTab().setText("Fav"));

    view_pager = (ViewPager) findViewById(R.id.viewpager);

    final ViewPagerAdapter adapter = new ViewPagerAdapter
            (getSupportFragmentManager(), tab_layout.getTabCount());

    view_pager.setAdapter(adapter);

    view_pager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tab_layout));

    tab_layout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {

        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            view_pager.setCurrentItem(tab.getPosition());
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {
        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {
        }
    });

}

}

在MainActivity中,转到Activity1 hold 3 Fragment 在Activity1中,Fragment2(FragAll)和Fragment3(FragFav)可以转到Acctivity2 在Activity2中可以编辑数据,这些数据将显示在Fragment3(FragFav)上。

我的问题是:
在Activity2上编辑数据,然后返回Activity1使用backpress时,Fragment3(FragFav)中的数据不会改变。(我认为app hold Fragment状态)。
但是当离开Acitivity1使用backpress到MainActivity,再次转到Activity1时,Fragment3(FragFav)上的数据发生了变化。(同样的事情,如果从Activity2返回Activity1,选择Fragment1(FragFile)并选择Fragment3(FragFav),数据将会改变)
我知道当从Activity2回归到Activity1时我需要刷新或重置Fragment3(FragFav),但我不知道该怎么做。
任何人都可以帮助我吗?

这是更新我跟随Drv:
我在java文件夹中创建了AppConstants类:

public class AppConstants {
public static boolean refreshPage=false;
public int fragment_position=0;

}

在Activity1中,我将代码更改为:

public class Acc1 extends AppCompatActivity {

ViewPager view_pager;

TabLayout tab_layout;

AppConstants ac;

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

    tab_layout = (TabLayout) findViewById(R.id.tab_layout);
    tab_layout.addTab(tab_layout.newTab().setText("File"));
    tab_layout.addTab(tab_layout.newTab().setText("All"));
    tab_layout.addTab(tab_layout.newTab().setText("Fav"));

    view_pager = (ViewPager) findViewById(R.id.viewpager);

    final ViewPagerAdapter adapter = new ViewPagerAdapter
            (getSupportFragmentManager(), tab_layout.getTabCount());

    view_pager.setAdapter(adapter);

    view_pager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tab_layout));

    tab_layout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {

        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            view_pager.setCurrentItem(ac.fragment_position);
            ac.fragment_position=tab.getPosition();
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {
        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {
        }
    });

}

@Override
public void onResume() {
    super.onResume();
    if (AppConstants.refreshPage) {
        AppConstants.refreshPage = false;
        finish();
        startActivity(getIntent());
    }
}
在Activity2中

,我添加了这段代码:

@Override
public void onBackPressed() {
    Intent setIntent = new Intent(Acc2.this, Acc1.class);
    AppConstants.refreshPage=true;
    startActivity(setIntent);
}

但是当选择Fragment2(FragAll)和Fragment3(FragFile)时,我在" view_pager.setCurrentItem(ac.fragment_position)中得到错误;"

3 个答案:

答案 0 :(得分:0)

你应该使用一些全局标志如下:

public class AppConstants{

 public static boolean refreshPage=false;
 public static int fragment_position=0;

}

在您的Activity2的背板中,写下如下:

AppConstants.refreshPage=true;

在Activity1的onResume中,写如下:

if (AppConstants.refreshPage) {
        AppConstants.refreshPage = false;
        finish();
        startActivity(getIntent());
}

在您的Activity1中,保存您选择的标签位置,如下所示:

YOURTABLAYOUT.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            AppConstants.fragment_position=tab.getPosition();
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }
    });

在onCreate中写如下:

 YOURVIEWPAGER.setCurrentItem(AppConstants.fragment_position);

答案 1 :(得分:0)

您可以通过调用碎片方法更新数据。

首先在片段中创建一个方法,例如

public void onUpdateData(Boolean isUpdated) {
    if (isUpdated) {
        Toast.makeText(context, "Updated", Toast.LENGTH_SHORT).show();
        //update your data

    }
}

然后在Activity中注册你的片段并传递上下文(this)

FragFav mFragFav = new FragFav(this);

只是不要忘记在片段中创建构造函数以避免Null对象异常。

public FragFav(Context context) {
    this.context = context;
}

现在,在数据更新的地方调用此方法,并在Fragment中触发方法。

mFragFav.onUpdateData(true);

答案 2 :(得分:0)

您可以使用以下代码段: 在onPageSelected上通知FragmentStatePagerAdapter。

 if (mSectionsPagerAdapter != null) {
                            mSectionsPagerAdapter.notifyDataSetChanged();
                        }

主要代码:

SectionsPagerAdapter mSectionsPagerAdapter = new SectionsPagerAdapter(getActivity(),getChildFragmentManager());

            // Set up the ViewPager with the sections adapter.
            mViewPager = (ViewPager)view.findViewById(R.id.container);
            mViewPager.setAdapter(mSectionsPagerAdapter);
            mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                }

                @Override
                public void onPageSelected(int position) {
                    Log.e("changed","mViewPager "+position);
                    try {
                        if (mSectionsPagerAdapter != null) {
                            mSectionsPagerAdapter.notifyDataSetChanged();
                        }
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }

                @Override
                public void onPageScrollStateChanged(int state) {

                }
            });

SectionsPagerAdapter

public class SectionsPagerAdapter extends FragmentStatePagerAdapter {

        Context mContext;
        FragmentManager fm1;
        public SectionsPagerAdapter(Context context, FragmentManager fm) {
            super(fm);
            fm1=fm;
//            if(multiple_actions.isExpanded()){
//                multiple_actions.collapse();
//            }
            mContext = context;
        }

        @Override
        public Fragment getItem(int position) {

            if(mViewPager.getCurrentItem()==0){
                return ActivityListing.newInstance(0,"preview");
            }else if(mViewPager.getCurrentItem()==1){
                return ActivityListing.newInstance(1,"current");
            }else if(mViewPager.getCurrentItem()==2){
                return ActivityListing.newInstance(2,"next");
            }else {
                return null;
            }
        }
        @Override
        public int getCount() {
            // Show 3 total pages.
            return 3;
        }

        @Override
        public CharSequence getPageTitle(int position) {

            switch (position) {
                case 0:
                    return getActivity().getResources().getString(R.string.preview);
                case 1:
                    return getActivity().getResources().getString(R.string.current);
                case 2:
                    return getActivity().getResources().getString(R.string.next);
            }
            return null;
        }
        @Override
        public int getItemPosition(Object object) {
            if(fm1.getFragments().contains(object))
                return POSITION_NONE;
            else
                return POSITION_UNCHANGED;
        }

    }