ViewPager始终从第0位开始

时间:2016-05-03 19:46:45

标签: android android-fragments android-viewpager

我在片段中有一个RecyclerView,它位于Activity中托管的ViewPager中。此项目MainActivity中有两个活动,其中列出了项目和DetailActivity,其中显示了有关在第一个活动中选择的项目的详细信息。

当我从DetailsActivity转移到MainActivity时,我总是采用ViewPager Fragment位置0这是第一个片段。如何恢复片段位置?

当我从DetailActivity转移到MainActivity时,Bundle总是空的。

以下是完整的code of the app

MainActivity

public class MainActivity extends AppCompatActivity {

    private ViewPager mViewPager;
    private static final String LOG = MainActivity.class.getSimpleName();
    private static final String VIEWPAGER_POSITION = "currentItem";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_titles);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        mViewPager = (ViewPager) findViewById(R.id.viewpager);
        setupViewPager(mViewPager);

        TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(mViewPager);

        if (savedInstanceState != null) {
            int pos = savedInstanceState.getInt(VIEWPAGER_POSITION, 0);
            Log.d(LOG, "onCreate pos: " + pos);
            mViewPager.setCurrentItem(pos);
        } else {
            Log.d(LOG, "onCreate savedInstanceState is null");
        }
    }

    private void setupViewPager(ViewPager viewPager) {
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
        adapter.addFragment(new PopularFragment(), getResources().getString(R.string.popularity));
        adapter.addFragment(new RatingFragment(), getResources().getString(R.string.rating));
        adapter.addFragment(new FavoriteFragment().newInstance(), getResources().getString(R.string.favorite));
        viewPager.setAdapter(adapter);
    }

    public static class ViewPagerAdapter extends FragmentStatePagerAdapter {
        private final List<Fragment> mFragmentList = new ArrayList<>();
        private final List<String> mFragmentTitleList = new ArrayList<>();

        public ViewPagerAdapter(FragmentManager manager) {
            super(manager);
        }

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

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

        public void addFragment(Fragment fragment, String title) {
            mFragmentList.add(fragment);
            mFragmentTitleList.add(title);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mFragmentTitleList.get(position);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onSaveInstanceState(Bundle bundle) {
        super.onSaveInstanceState(bundle);
        int pos = mViewPager.getCurrentItem();
        Log.d(LOG, "onSaveInstanceState pos: " + pos);
        bundle.putInt(VIEWPAGER_POSITION, mViewPager.getCurrentItem());
    }
}

感谢。

2 个答案:

答案 0 :(得分:0)

我正在拍摄,当您从DetailActivity返回时,请查找

中的Bundle
this.getIntent().getExtras().getInt(VIEWPAGER_POSITION, 0)

更新:

如果您需要保留当前位置,例如onPause中的SharedPreferences,并在onResume中检索它。像:

private static String MY_PAGER_TAG  = "tag_of_shared_vp_position";
    public void onPause(){ 
  super.onPause();
  int pos = getCurrentViewPagerPosition();//custom function, implement it by //yourself
  SharedPreferences sharedPref = this.getPreferences(Context.MODE_PRIVATE);
  SharedPreferences.Editor editor = sharedPref.edit();
  editor.putInt(MY_PAGER_TAG, pos);
  editor.commit();
}

public void onResume(){
  super.onResume();
  SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
  int pos = sharedPref.getInt(MY_PAGER_TAG,0);
  setUpViewPagerOnPosition(pos);
}

答案 1 :(得分:0)

我注意到如果按下物理后退按钮,ViewPager和RecyclerView就会被正确恢复。所以我想要一种方法来设置up按钮行为与onBackPressed()相同。答案已在问题Make the “up” button behave like the “back” button on Android中发布。

我在DetailActivity中进行了以下更改,我的代码开始正常运行。

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    if (id == android.R.id.home) {
        onBackPressed();
        return true;
    }

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}