当ViewPager显示片段时,重新启动/刷新onCreateView

时间:2016-01-10 22:39:22

标签: android android-fragments android-viewpager android-adapter

我有以下代码来创建一个包含片段的viewpager:

// Create tabs for viewpager
    FragmentPagerItemAdapter adapter = new FragmentPagerItemAdapter(
            getSupportFragmentManager(), FragmentPagerItems.with(this)
            .add(R.string.tab_stuff, Stuff.class)
            .add(R.string.tab_profile, Profile.class)
            .create());
// Give the viewpager an adapter
    viewPager = (ViewPager) findViewById(R.id.view_pager);
    viewPager.setAdapter(adapter);

我还为用户提供了一个选项,可以通过抽屉上的菜单更改viewpager上的当前页面:

// Navigation view listener
    navigationView.setNavigationItemSelectedListener(new NavigationView
            .OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(MenuItem item) {
            item.setChecked(true);
            switch (item.getItemId()) {

                // Item: shows stuff and then closes the drawer
                case R.id.navdrawer_stuff:

                    viewPager.setCurrentItem(0);
                    drawerLayout.closeDrawers();
                    return true;

当附加片段并且在OnCreateView方法上发生这种棘手的事情:

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                         @Nullable Bundle savedInstanceState) {

    // Check if user is logged in and subbed
    logged = Prefs.getBoolean(LOGGED, false);
    subbed = Prefs.getBoolean(SUB, false);
    meal = Prefs.getBoolean(DAY_DIET, false);
    email = Prefs.getString(USERMAIL, "");

    // If user is logged and subbed
    if (logged && subbed) {
        // Inflate the proper layout
        View view = inflater.inflate(
                R.layout.diets_user_subbed,
                container,
                false
        );
        try {
            currentDayMeal(email);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        checkMealsListener(view);
        return view;
    } else if (logged) {
        View view = inflater.inflate(
                R.layout.no_sub,
                container,
                false
        );
        return view;
    } else {
        View view = inflater.inflate(
                R.layout.no_sub_no_reg,
                container,
                false
        );
        return view;
    }
}

现在,所有这些都像魅力一样工作,直到我更改了记录或子窗口的值,并且OnCreateView不再检查要加载的布局。如果我关闭应用程序并重新启动它,它会工作,但一旦打开它,它就不会重新检查。

有什么想法吗?感谢。

1 个答案:

答案 0 :(得分:0)

  1. 在您的片段中创建片段方法以更改您的布局。即它必须从根布局中删除/添加一些视图,您可以通过Fragment#getView()
  2. 获取
  3. 制作片段以在首选项更改时作出反应。为此添加onSharedPreferenceChanged。在其中调用片段方法1。

    public class FragmentsClassName extends Fragment implements SharedPreferences.OnSharedPreferenceChangeListener
    {
    
        @Override
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            ....
            PreferenceManager.getDefaultSharedPreferences(getActivity()).registerOnSharedPreferenceChangeListener(this);
        }
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            View v = inflater.inflate(R.layout.fragment_recycler_in_swipe, container, false);
            update(v);
            return v;
        }
    
        private void update(View v)
        {
            //TODO update layout here
        }
    
        @Override
        public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)
        {
            Log.i("log", "onSharedPreferenceChanged with key: " + key);
            if (!isAdded())
            {
                return;
            }
            update(getView());
        }
    }