Android Viewpager bounce to half a page

后端 未结 2 1161
南方客
南方客 2020-12-15 14:12

So what i am trying to achieve is user would open to first page of the view pager, and the view pager would bounce to half of the second page and bounce back to the fist pag

2条回答
  •  隐瞒了意图╮
    2020-12-15 14:22

    Adding a note to @Yuraj's answer. Call the method in onWindowFocusChanged when hasFocus==true as follows to avoid indexOutOfBoundsException:

    @Override
    public void onWindowFocusChanged(boolean hasFocus) 
    {
        super.onWindowFocusChanged(hasFocus);
    
        if(hasFocus)
        {
            Handler handler = new Handler();
    
            final Runnable r = new Runnable() 
            {
                public void run() 
                {
                    if(mViewPager.getCurrentItem() == 0)
                    {
                        Context context = Activity_main.this;
                        String filename="Init";
                        SharedPreferences stats;
                        stats = context.getSharedPreferences(filename, 0); 
                        int appOpen = stats.getInt("appOpen", 0);
    
                        if(appOpen <= 5)
                        {
                            animateViewPager(mViewPager, 10, 300);
    
                            appOpen += 1;
                            SharedPreferences.Editor editor = stats.edit();
                            editor.putInt("appOpen", appOpen);
                            editor.commit();
                        }
                    }
                }
            };
    
            handler.postDelayed(r, WAIT_VIEWPAGER_NUDGE);
    
        }
    }
    

提交回复
热议问题