gpt4 book ai didi

android - 按下后退按钮时保存 tabLayout 位置

转载 作者:行者123 更新时间:2023-11-30 01:27:43 24 4
gpt4 key购买 nike

当用户按下“返回”按钮时,如何保存和返回标签位置?

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
ViewPager vpPager = (ViewPager) findViewById(R.id.viewpager);
adapterViewPager = new MyPagerAdapter(getSupportFragmentManager());
vpPager.setAdapter(adapterViewPager);

// Give the TabLayout the ViewPager
tabLayout = (TabLayout) findViewById(R.id.sliding_tabs);
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
tabLayout.setTabMode(TabLayout.MODE_FIXED);
tabLayout.setupWithViewPager(vpPager);

// Attach the page change listener inside the activity
vpPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {

}

@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt(POSITION, tabLayout.getSelectedTabPosition());
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
viewPager.setCurrentItem(savedInstanceState.getInt(POSITION));
}

我是否使用 onBackPressed() 来执行此操作?非常感谢您的帮助。谢谢!

最佳答案

您可以按以下方式使用后退键:

@Override
public void onBackPressed() {
int pos = mViewPager.getCurrentItem();
}

当你回到屏幕时你可以设置寻呼机的位置

@Override
public void onResume(){
super.onResume();
// put your code here...

if(mViewPager !=null)

mViewPager.setCurrentItem(pos);

}

关于android - 按下后退按钮时保存 tabLayout 位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36214982/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com