在FragmentActivity中刷新片段

时间:2017-01-29 00:18:41

标签: android android-fragments android-fragmentactivity

我的应用有一个带有tabBar的fragmentActivity,可控制视图寻呼机并包含3个片段,当我更新User中的课程fragmentB时,我需要在fragmentC中显示它。 我的问题是每次添加新用户时如何刷新fragmentC

在我写这个问题之前,我尝试了这个问题的所有解决方案:

1。Update ViewPager dynamically?

2。refresh fragment at reload

3。Update Fragment from ViewPager

4。ViewPager PagerAdapter not updating the View

5。How to update fragment content from activity (viewpager)?

这是我的代码

FragmentActivity:

public class MainActivityTab extends FragmentActivity  {


    public SectionsPagerAdapter mSectionsPagerAdapter;
    public ViewPager mViewPager;
    public static MainActivityTab instance = null;


    public static MainActivityTab getInstance(){
        return instance;
    }

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_tab);
        getWindow().setStatusBarColor(Color.rgb(191,76,12));
        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
        mViewPager = (ViewPager) findViewById(R.id.container);
        mViewPager.setAdapter(mSectionsPagerAdapter);
        final TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(mViewPager);
        tabLayout.getTabAt(0).setIcon(R.drawable.icon_A);
        tabLayout.getTabAt(1).setIcon(R.drawable.icon_B);
        tabLayout.getTabAt(2).setIcon(R.drawable.icon_C);
        mViewPager.setCurrentItem(1,false);
    }

FragmentStatePagerAdapter:

 public class SectionsPagerAdapter extends FragmentStatePagerAdapter {

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

        @Override
        public Fragment getItem(int position) {
            switch (position){
                case 0:
                    return FragmentA.newInstance();
                case 1:
                    return FragmentA.newInstance();
                case 2:
                    return FragmentC.newInstance();
                default:
                    return null;
            }
        }

        @Override
        public int getItemPosition(Object object) {
            return POSITION_NONE;
        }

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

        @Override
        public CharSequence getPageTitle(int position) {
            return null;
        }
    }

FragmentB:

public class FragmentB extends Fragment {

    EditText name;
    EditText age;
    Button btnSave;

    public static FragmentB newInstance() {
        FragmentB fragment = new FragmentB();
        return fragment;
    }
    public FragmentB() {}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_new2, container, false);
        name = (EditText) rootView.findViewById(R.id.name);
        age = (EditText) rootView.findViewById(R.id.age);
        btnSave = (Button) rootView.findViewById(R.id.btnSave);
        btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UserManager.getInstance().creteUser(name.getText().toString(),age.getText().toString());
            }
        });
        return rootView;
    }
}

FragmentC:

public class FragmentC extends Fragment {
    TextView nameTxt;
    TextView ageTxt;

    public static FragmentC newInstance(){
        FragmentC instance = new FragmentC();
        return instance;
    }
    public FragmentC(){

    }
@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
     View rootView = inflater.inflate(R.layout.fragment_new3, container, false);
    nameTxt = (TextView) rootView.findViewById(R.id.nameTxt);
    ageTxt = (TextView) rootView.findViewById(R.id.ageTxt);
    showUser();
    return rootView;
    }
    public void showUser(){

        if(UserManager.getInstance().getUser().getName()!=null){
            nameTxt.setText(UserManager.getInstance().getUser().getName().toString());
            ageTxt.setText(UserManager.getInstance().getUser().getAge().toString());
        }
    }

}

2 个答案:

答案 0 :(得分:0)

你可以创建一个回调,如:

public interface UserChangedCallback{
  void onUserChanged();
}

并在要访问用户的每个视图/片段上实现此回调:

@Override
public void onUserChanged() {
  adapter.notifyDataSetChanged();
  // or if you don't have an adapter refresh your textfields or whatever you want
}

通过UserManager.getInstance().registerCallback(this)在用户管理器上注册回调。在内部,用户管理器必须将回调添加到内部列表。

在用户管理器中创建一个私有函数:

private void notifyCallbacks() {
  for(UserChangedCallback callback : registeredCallbacks) {
    callback.onUserChanged();
  } 
}

如果添加/修改/删除用户,则必须始终调用此功能。例如:

public void addUser(User user) {
  users.add(user);
  notifiCallbacks();
}

现在每个视图都会被通知并刷新。

重要!

不要忘记从onDestroy()上的用户管理器取消注册回调,以避免内存泄漏。

答案 1 :(得分:0)

找到解决方案:

我在方法onPageSelected中使用,当位置为2时,我的片段分离,然后附加片段。 工作得很好!

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

                }

                @Override
                public void onPageSelected(int position) {
                    if(position==2){
                        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction().add(R.id.fragmant_new3, FragmentC.newInstance());
                        fragmentTransaction.detach(FragmentC.newInstance());
                        fragmentTransaction.attach(FragmentC.newInstance());
                        fragmentTransaction.commit();

                    }
                }

                @Override
                public void onPageScrollStateChanged(int state) {

                }
            });