简体   繁体   English

重新加载整个视图后,如何保存tablayout项的状态和相关的viewpager内容位置?

[英]How do I save the state of my tablayout item and associated viewpager content position after reloading the whole view?

I have a tablayout associated with a viewpager. 我有一个与viewpager关联的tablayout。 Everytime I pull to refresh, currently I try to reload the entire page (calling finish and starting the activity again).Say I have 4 items in my tab layout (dynamic) , I would like to have it such that, everytime I refresh, I can save the state of the item selected prior to refresh and then display it after the entire screen is reloaded. 每次我拉动刷新,目前我尝试重新加载整个页面(调用完成并再次启动活动)。我的标签布局中有4个项目(动态),我想这样,每次刷新时,我可以在刷新之前保存所选项目的状态,然后在重新加载整个屏幕后显示它。

I tried a couple of things recommended online but had no luck. 我在线推荐了几件但没有运气的东西。

Here's the code I can potentially use, but not sure how to go about it : 这是我可能使用的代码,但不知道如何去做:

 void selectPage(int pageIndex)
    {
        mViewPager.setCurrentItem(pageIndex);
        mTabLayout.setupWithViewPager(mViewPager);
    }

Also, 也,

@BindView(R.id.lists_view_pager)
    ViewPager mViewPager;

    @BindView(R.id.lists_refresh)
    SwipeRefreshLayout mRefresh;

    @BindView(R.id.tab_layout)
    TabLayout mTabLayout;

On create I call: 在创建我打电话:

mRefresh.setOnRefreshListener(this::refreshassistant);

refreshassistant is as follows : refreshassistant如下:

 public void refreshassistant(){
        finish();
        startActivity(getIntent());
        mPagerAdapter.notifyDataSetChanged();
        overridePendingTransition(0, 0);
        mPresenter.onRefresh();
    }

I would like something like if I select tab 2, it shows content of tab 2 and then when I refresh/reload the entire page, i am able to get back tab 2 and its contents. 我希望如果我选择选项卡2,它显示选项卡2的内容,然后当我刷新/重新加载整个页面时,我能够返回选项卡2及其内容。 Any idea how to go about it? 知道如何去做吗?

Thanks in advance! 提前致谢!

You can use following two methods to store and retrieve your values from your pages: 您可以使用以下两种方法来存储和检索页面中的值:

Setting values in Preference: 在偏好中设置值:

public static void setDefaults(String key, String value, Context context) {
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
    SharedPreferences.Editor editor = preferences.edit();
    editor.putString(key, value);
    editor.commit();
}

Retrive data from Preferences: 从首选项中检索数据:

public static String getDefaults(String key, Context context) {
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
    return preferences.getString(key, null); //if no value found returns null
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM