ViewPager不会更新添加的片段

时间:2018-10-08 10:36:03

标签: java android android-fragments android-viewpager firebase-authentication

我在更新ViewPager片段时遇到问题。我们需要向注册用户显示带有数据的片段,当他未注册时,我们需要显示带有消息的片段以进行注册。我使用此方法在MainActivity中对其进行了检查:

@Override
public void setAdapter(boolean isUserExist) {
    Log.d("RegDebug", "In setAdapter");
    mainPagerAdapter.clearData();
    mainPagerAdapter.addFragment(searchFragment, getString(R.string.search_title));
    if (isUserExist) {
        Log.d("RegDebug", "In setAdapter reg");
        mainPagerAdapter.addFragment(new ChatsFragment(), getString(R.string.chats_title));
        mainPagerAdapter.addFragment(new ActionsFragment(), getString(R.string.actions_title));
        Toast.makeText(getApplicationContext(), "Registered!", Toast.LENGTH_SHORT).show();
    } else {
        Log.d("RegDebug", "In setAdapter unreg");
        mainPagerAdapter.addFragment(RegisterFragment.newInstance(Consts.CHATS_TAB_NAME), getString(R.string.chats_title));
        mainPagerAdapter.addFragment(RegisterFragment.newInstance(Consts.ACTIONS_TAB_NAME), getString(R.string.actions_title));
        Toast.makeText(getApplicationContext(), "Unregistered!!!", Toast.LENGTH_SHORT).show();
    }
    mainPagerAdapter.notifyDataSetChanged();

    viewPager.setAdapter(mainPagerAdapter);
}

我使用来自firebase auth的设置值在演示者中调用此方法,检查用户是否存在:

 public void checkForUserExist() {
    if (mainInteractor.isUserExist()) {
        getViewState().setRegAdapter();
    } else getViewState().setUnregAdapter();
}

然后在MainActivity的onCreate中调用presenter方法:

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

    dialogFragment = new FilterDialogFragment();
    searchFragment = new SearchFragment();

    //UI
    toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    viewPager = findViewById(R.id.main_view_pager);
    tabLayout = findViewById(R.id.main_tabs);

    //mainPagerAdapter = new MainPagerAdapter(getSupportFragmentManager());
    mainPresenter.checkForUserExist();

    tabLayout.setupWithViewPager(viewPager);
}

我尝试记录布尔结果,它返回的确切值必须是,但是分页适配器无法更新其内容。MainPagerAdapter的代码:

public class MainPagerAdapter extends FragmentPagerAdapter{

private final List<Fragment> fragmentList = new ArrayList<>();
private final List<String> fragmentTitleList = new ArrayList<>();


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

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

}

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


@Nullable
@Override
public CharSequence getPageTitle(int position) {

    return fragmentTitleList.get(position);
}

public void addFragment(Fragment fragment, String title){
    fragmentList.add(fragment);
    fragmentTitleList.add(title);
}

public void clearData(){
    fragmentList.clear();
    fragmentTitleList.clear();
    notifyDataSetChanged();
    Log.d("RegDebug", " fragmentList size is " + fragmentList.size()
            + " fragmentTitleList size is " + fragmentTitleList.size());
}

}

1 个答案:

答案 0 :(得分:0)

使用ViewPager类的OnPageChangeListener并使用界面从那里通知您当前的片段。