2016-09-02 70 views
0

我想startActivity(LoginActivity)當第三個片段後。當viewpager的最後一個片段滑動時,如何從片段移動到活動?

在第3個片段中,我從右向左滑動以開始活動。

我發現了一些問題,但:

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

    if(position == tutorialViewPager.getAdapter().getCount() - 1){ 

     Intent intent = new Intent(this, LoginActivity.class); 
     startActivity(intent); 
    } 

} 

在這種情況下,當移動第二個片段與幻燈片,第3段(最後一個片段)第三片段出現一點點變化的活動。

我想改變活動時,我滑動最後一個片段(不是從第二個片段到第3個片段,只是幻燈片第3個片段)。

當幻燈片最後一個片段時,我怎樣才能將片段變形爲特定的活動?

請幫幫我。

public class TutorialActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener { 

    int MAX_PAGE = 3; 
    Fragment cur_fragment = new Fragment(); 

    ViewPager tutorialViewPager; 

    private int selectedPageIndex = -1; 
    private boolean exitWhenScrollNextPage = false; 


    @Override 
    protected void onCreate(@Nullable Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_tutorial_page); 

     tutorialViewPager = (ViewPager) findViewById(R.id.tutorialViewPager); 
     tutorialViewPager.setAdapter(new adapter(getSupportFragmentManager())); 

     tutorialViewPager.addOnPageChangeListener(this); 

    } 

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

     if(position == tutorialViewPager.getAdapter().getCount() - 1){ 

      Intent intent = new Intent(this, LoginActivity.class); 
      startActivity(intent); 
     } 

    } 

    @Override 
    public void onPageSelected(int position) { 

    } 

    @Override 
    public void onPageScrollStateChanged(int state) { 

    } 

    private class adapter extends FragmentPagerAdapter{ 

     public adapter(FragmentManager fm) { 
      super(fm); 
     } 

     @Override 
     public Fragment getItem(int position) { 

      if(position<0 || MAX_PAGE <= position){ 
       return null; 
      } 

      switch (position){ 

       case 0: 
        cur_fragment = new TutorialFragmentA(); 
        break; 

       case 1: 
        cur_fragment = new TutorialFragmentB(); 
        break; 

       case 2: 
        cur_fragment = new TutorialFragmentC(); 
        break; 


      } 
      return cur_fragment; 
     } 

     @Override 
     public int getCount() { 
      return MAX_PAGE; 
     } 
    } 


} 

回答

0

您必須修改ViewPager.OnPageChangeListener方法的實現。這是我使用和設法解決這個問題。

我把這裏轉換到(LoginActivity)。

mViewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() { 
     boolean onLastPageChanged = false; 
     @Override 
     public void onPageSelected(int position) { 
     } 

     @Override 
     public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { 
      int lastFrag = mQuestionCollectionPagerAdapter.getCount() -1; 

      if (onLastPageChanged && position == lastFrag) { 
       //next 
       Log.e(TAG, "Transition Happened"); 
      } 
     } 

     @Override 
     public void onPageScrollStateChanged (int state) { 
      int lastFrag = mQuestionCollectionPagerAdapter.getCount() -1; 

      int currentFragment = mViewPager.getCurrentItem(); 
      if (currentFragment == lastFrag /*&& lastPos==lastIdx*/ && state == 1) { 
       onLastPageChanged = true; 
       Log.e(TAG, "page scroll state >>>>>>"); 
       Intent intent = new Intent(this, LoginActivity.class); 
     startActivity(intent); 
       finish(); 
      } else { 
       onLastPageChanged = false; 
      } 
     } 
    }); 
相關問題