当前位置值在viewpager getCurrentItem方法中是陈旧的

时间:2013-08-02 07:11:03

标签: android android-viewpager

我有查看寻呼机来显示日复一日的数据。然后我设置当前项目位置。这是编码..

viewPager = (ViewPager) findViewById(R.id.pager);
    pagerAdapter = new DashboardViewAdapter(getSupportFragmentManager(), selectedDate);

    daoManager = OpenHelperManager.getHelper(this, DAOManager.class);

    viewPager.setAdapter(pagerAdapter);

    Date d = pagerAdapter.getDate(365);
    Log.i("pageAdapter", d.toString());

    viewPager.setCurrentItem(365);
    Log.i("viewAdapter", viewPager.getCurrentItem()+"");

    viewPager.setOnPageChangeListener(new OnPageChangeListener(){

        public void onPageScrollStateChanged(int arg) {

        }

        public void onPageScrolled(int arg0, float arg1, int arg2) {
            // TODO Auto-generated method stub

        }

        public void onPageSelected(int position) {
            Date d = pagerAdapter.getDate(viewPager.getCurrentItem());
            monthTitleTextView.setText(formatter.format(d));
            selectedDate = d;
            Log.i("onPageSelected", selectedDate.toString());
            Log.i("onPageSelected", viewPager.getCurrentItem()+"");
        }

    });

我有onSaveInstanceState方法来存储selectedDate

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putSerializable(DAOManager.SELECTED_DATE, selectedDate);
    Log.i("onSaveInstanceState", selectedDate.toString());
}

我运行编码,得到了这个结果。

08-02 13:03:17.868: I/pageAdapter(16770): Fri Aug 02 00:00:00
 GMT+06:30 2013 08-02 13:03:17.868: I/viewAdapter(16770): 365

我滑了一天。我得到了这个结果。

08-02 13:07:17.278: I/onPageSelected(16770): Sat Aug 03 00:00:00
GMT+06:30 2013 08-02 13:07:17.278: I/onPageSelected(16770): 366

之后我旋转屏幕以改变方向。结果是这样的。

08-02 13:10:27.558: I/onSaveInstanceState(16770): Sat Aug 03 00:00:00 GMT+06:30 2013
08-02 13:10:27.748: I/dateList (16770): 731 from Fri Aug 03 00:00:00 GMT+06:30 2012-- to Sun Aug 03 00:00:00 GMT+06:30 2014
08-02 13:10:27.748: I/pageAdapter(16770): Sat Aug 03 00:00:00 GMT+06:30 2013
08-02 13:10:27.748: I/viewAdapter(16770): 365
08-02 13:10:27.888: I/onPageSelected(16770): Sun Aug 04 00:00:00 GMT+06:30 2013
08-02 13:10:27.888: I/onPageSelected(16770): 366

我的问题是,在屏幕方向改变后,即使我在oncreate方法中设置位置,当前项目位置也是陈旧的。我想知道如何刷新视图寻呼机的当前项目位置。

感谢。

1 个答案:

答案 0 :(得分:0)

试试这个,它可以帮到你

 mIndicator.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                Toast.makeText(SampleCirclesWithListener.this, "Changed to page " + position, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });