在第3段片段之后我想要startActivity(LoginActivity)
。
在位置第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);
}
}
在这种情况下,当用幻灯片将第二个片段移动到第三个片段时,第三个片段(最后一个片段)出现了一点点并改变了Activity。
当我滑动最后一个片段时,我想更改Activity(不是从第二个片段到第三个片段,只是滑动第三个片段)。
当滑动最后一个片段时,如何将片段转换为特定的活动?
请帮帮我。
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 :(得分: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;
}
}
});