ViewPager显示重复数据

时间:2013-09-27 04:39:48

标签: android android-fragments android-viewpager

我遇到以下问题, 第一个viewpager错误地显示第二个viewpager的内容 当我滑动到第二个viewpager时,内容是相同的,正确显示第二个viewpager 解决这个问题。我必须滑到第三个,以便FragmentStatePagerAdapter可以重新加载 然后正确显示第一个视图的内容。

package com.example.android.effectivenavigation;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class MainActivity extends FragmentActivity {
    static int myViewPagerItem;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyPageAdapter mFragmentStatePagerAdapter = new MyPageAdapter(getSupportFragmentManager());
        setContentView (R.layout.refuel_viewpager);
        ViewPager pager = (ViewPager) findViewById(R.id.pager);
        pager.setAdapter(mFragmentStatePagerAdapter);
    }
    public static class MyPageAdapter extends FragmentStatePagerAdapter {
        public MyPageAdapter(FragmentManager fm) {
            super(fm);
        }
        @Override
        public Fragment getItem(int i) {
            myViewPagerItem = i;
            return new ViewFragment();
        }
        @Override
        public int getCount() {
            return 10;
        }
    }
    public static class ViewFragment extends Fragment {
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            View v = inflater.inflate(R.layout.view, container, false);
            TextView text1 = (TextView) v.findViewById(R.id.text1);
            text1.setText("Page: " + myViewPagerItem);
            return v;
        }
    }

}

1 个答案:

答案 0 :(得分:1)

这样做

public class MainActivity extends FragmentActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyPageAdapter mFragmentStatePagerAdapter = new MyPageAdapter(getSupportFragmentManager());
        setContentView (R.layout.refuel_viewpager);
        ViewPager pager = (ViewPager) findViewById(R.id.pager);
        pager.setAdapter(mFragmentStatePagerAdapter);
    }
    public  class MyPageAdapter extends FragmentStatePagerAdapter {
        public MyPageAdapter(FragmentManager fm) {
            super(fm);
        }
        public int getItemPosition(Object object) {
            return POSITION_NONE;
        }
        @Override
        public Fragment getItem(int i) {
            return new ViewFragment(i);
        }
        @Override
        public int getCount() {
            return 10;
        }
    }
    public  class ViewFragment extends Fragment {
        int pageno;
        public ViewFragment(int pageNo){
            this.pageno=pageNo;
        }
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            View v = inflater.inflate(R.layout.view, container, false);
            TextView text1 = (TextView) v.findViewById(R.id.text1);
            text1.setText("Page: " + pageno);
            return v;
        }
    }

}