打开相同 activity 时检索 Viewpager 位置
Viewpager position retrieve when same activity in opened
我想要的应用如下:
当用户单击其中一个按钮时,它会转到另一张包含 cardstack view-pager 的卡片 activity,用户将阅读卡片并在卡片完成后刷卡。然后会出现另一张卡片。如果他回到主 activity 并回到卡 activity,那么卡 activity 应该从他离开的卡开始。
这是我的 viewpager 初始化:
//Viewpager initialization
mViewPager = (ViewPager)findViewById(R.id.viewpager);
CardStackAdapter mAdapter = new CardStackAdapter(getSupportFragmentManager());
mViewPager.setPageTransformer(true,new CardStackTransformer());
mViewPager.setOffscreenPageLimit(6);
mViewPager.setPadding(0,0,0,10);
mViewPager.setAdapter(mAdapter);
这是适配器:
public class CardStackAdapter extends FragmentStatePagerAdapter{
public CardStackAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch(position){
case 0:
return new overview1();
case 1:
return new overview2();
case 2:
return new overview2();
case 3:
return new overview2();
case 4:
return new overview2();
case 5:
return new overview2();
default:
break;
}
return null;
}
@Override
public int getCount() {
return 6;
}
}
这是我的全部Activity
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
public class overview extends AppCompatActivity {
private ViewPager mViewPager;
@Override
protected void onSaveInstanceState(Bundle outState) {
outState.putInt("ViewPagerPosition",mViewPager.getCurrentItem());
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_overview);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
this.setTitle("");
this.getSupportActionBar().setHomeAsUpIndicator(R.mipmap.ic_close_white_24dp);
this.getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//Viewpager initialization
mViewPager = (ViewPager)findViewById(R.id.viewpager);
CardStackAdapter mAdapter = new CardStackAdapter(getSupportFragmentManager());
mViewPager.setPageTransformer(true,new CardStackTransformer());
mViewPager.setOffscreenPageLimit(6);
mViewPager.setPadding(0,0,0,10);
mViewPager.setAdapter(mAdapter);
if(savedInstanceState!=null){
int lastPosition = savedInstanceState.getInt("ViewPagerPosition",0);
mViewPager.setCurrentItem(lastPosition);
}
}
public class CardStackAdapter extends FragmentStatePagerAdapter{
public CardStackAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch(position){
case 0:
return new overview1();
case 1:
return new overview2();
case 2:
return new overview2();
case 3:
return new overview2();
case 4:
return new overview2();
case 5:
return new overview2();
default:
break;
}
return null;
}
@Override
public int getCount() {
return 6;
}
}
private class CardStackTransformer implements ViewPager.PageTransformer
{
@Override
public void transformPage(View page, float position)
{
if(position>=0)
{
page.setScaleX(0.93f - 0.015f * position);
page.setScaleY(0.93f);
page.setTranslationX(- page.getWidth()*position);
page.setTranslationY(10 * position);
}
}
}
}
您应该尝试在 onSaveInstanceState() 中保存您的 activity 状态并在 onCreate() 中恢复它:
@Override
protected void onSaveInstanceState(Bundle instanceState) {
super.onSaveInstanceState(instanceState);
instanceState.putInt("viewPagerPosition", viewPager.getCurrentItem());
}
@Override
protected void onCreate(Bundle savedInstanceState) {
//after setting the adapter
if (savedInstanceState != null) {
int lastPosition = savedInstanceState.getInt("viewPagerPosition", 0);
viewPager.setCurrentItem(lastPosition);
}
}
我想要的应用如下: 当用户单击其中一个按钮时,它会转到另一张包含 cardstack view-pager 的卡片 activity,用户将阅读卡片并在卡片完成后刷卡。然后会出现另一张卡片。如果他回到主 activity 并回到卡 activity,那么卡 activity 应该从他离开的卡开始。
这是我的 viewpager 初始化:
//Viewpager initialization
mViewPager = (ViewPager)findViewById(R.id.viewpager);
CardStackAdapter mAdapter = new CardStackAdapter(getSupportFragmentManager());
mViewPager.setPageTransformer(true,new CardStackTransformer());
mViewPager.setOffscreenPageLimit(6);
mViewPager.setPadding(0,0,0,10);
mViewPager.setAdapter(mAdapter);
这是适配器:
public class CardStackAdapter extends FragmentStatePagerAdapter{
public CardStackAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch(position){
case 0:
return new overview1();
case 1:
return new overview2();
case 2:
return new overview2();
case 3:
return new overview2();
case 4:
return new overview2();
case 5:
return new overview2();
default:
break;
}
return null;
}
@Override
public int getCount() {
return 6;
}
}
这是我的全部Activity
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
public class overview extends AppCompatActivity {
private ViewPager mViewPager;
@Override
protected void onSaveInstanceState(Bundle outState) {
outState.putInt("ViewPagerPosition",mViewPager.getCurrentItem());
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_overview);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
this.setTitle("");
this.getSupportActionBar().setHomeAsUpIndicator(R.mipmap.ic_close_white_24dp);
this.getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//Viewpager initialization
mViewPager = (ViewPager)findViewById(R.id.viewpager);
CardStackAdapter mAdapter = new CardStackAdapter(getSupportFragmentManager());
mViewPager.setPageTransformer(true,new CardStackTransformer());
mViewPager.setOffscreenPageLimit(6);
mViewPager.setPadding(0,0,0,10);
mViewPager.setAdapter(mAdapter);
if(savedInstanceState!=null){
int lastPosition = savedInstanceState.getInt("ViewPagerPosition",0);
mViewPager.setCurrentItem(lastPosition);
}
}
public class CardStackAdapter extends FragmentStatePagerAdapter{
public CardStackAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch(position){
case 0:
return new overview1();
case 1:
return new overview2();
case 2:
return new overview2();
case 3:
return new overview2();
case 4:
return new overview2();
case 5:
return new overview2();
default:
break;
}
return null;
}
@Override
public int getCount() {
return 6;
}
}
private class CardStackTransformer implements ViewPager.PageTransformer
{
@Override
public void transformPage(View page, float position)
{
if(position>=0)
{
page.setScaleX(0.93f - 0.015f * position);
page.setScaleY(0.93f);
page.setTranslationX(- page.getWidth()*position);
page.setTranslationY(10 * position);
}
}
}
}
您应该尝试在 onSaveInstanceState() 中保存您的 activity 状态并在 onCreate() 中恢复它:
@Override
protected void onSaveInstanceState(Bundle instanceState) {
super.onSaveInstanceState(instanceState);
instanceState.putInt("viewPagerPosition", viewPager.getCurrentItem());
}
@Override
protected void onCreate(Bundle savedInstanceState) {
//after setting the adapter
if (savedInstanceState != null) {
int lastPosition = savedInstanceState.getInt("viewPagerPosition", 0);
viewPager.setCurrentItem(lastPosition);
}
}