Android viewPager onPageSelected无法获得与viewPager位置相关的正确数据库ID

时间:2015-02-16 12:08:41

标签: android-fragments android-viewpager ormlite

我创建了用于加载数据库记录的模块,其中id = 1,2,3,4,5,...通过获取viewPage的位置,将其作为位置索引+1。它已在OnPageChangeListener onPageSelected方法中实现。当涉及到测试时,它表明索引在开头显示为0,2,3,4而不是索引1,2,3,4 ..以加载记录。当我向右滑动时,它会加载另一个相同的记录而不是任何记录,甚至将setOffscreenPageLimit设置为零。我不希望这种情况发生

请您告诉我阅读viewPager页面位置的替代方法吗?

以下是我的工作:

    mViewPager = (CustomViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);
        mViewPager.setEnabled(true);
        mViewPager.setOffscreenPageLimit(0);
        mViewPager.setOnPageChangeListener(viewPagerPageChangeListener);
        mViewPager.setCurrentItem(0);



OnPageChangeListener viewPagerPageChangeListener = new OnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {      
            pageId = position + 1;
            overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
            final Animation animationFadeIn = AnimationUtils.loadAnimation(FlightBankActivity.this, R.anim.fade_in);
            for (int i = 0; i < dotsCount; i++) {               
                dots[i].setImageResource(R.drawable.dot_e);
            }
            dots[position].setAnimation(animationFadeIn);
            dots[position].setImageResource(R.drawable.dot_s);

        }

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            ArrayList<FlightProfile> fpList = (ArrayList<FlightProfile>) DatabaseManager.getInstance().getAllFlightProfile();
            System.out.println(fpList.size() + " : is the size of profile");
            for(FlightProfile gfg : fpList){
                System.out.println(gfg.getId()+ " is the profile id");
                System.out.println(gfg.getName()+ " is the profile i]name");
            }
            System.out.println(pageId+ " : is the pageId");  
        }

        @Override
        public void onPageScrollStateChanged(int arg0) {

        }
    };

Fragment.java

@Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {

            Bundle bundle=getArguments(); 
            final int type = bundle.getInt("type");
            System.out.println("type :" + type); 

            fp = DatabaseManager.getInstance().getFlightProfile(pageId);    

...

0 个答案:

没有答案