Page slide UI enhancement: Slide directly to the next activity

时间:2019-04-08 13:15:49

标签: android android-drawable

I have implemented a introduction slides using a pagination mechanism (OnPageChangeListener interface).

The pages are simple drawable images:

R.drawable.intro_slide_0
R.drawable.intro_slide_1
R.drawable.intro_slide_2

What I wanted is to switch automatically to the next Activity when the last page is show.

In order to achieve that, I had to add a null (0 resource id). When this blank page is shown, I switch to the next Activity.

public class FirstScreenActivity extends AppCompatActivity implements OnPageChangeListener {

private ImageView[] dots;
private int dotsCount;
private ViewPager intro_images;
FirstScreenAdapter mAdapter;
private int[] mImageResources;
private boolean mIsRateMeConfigured;
private LinearLayout pager_indicator;

public View onCreateView(View view, String str, Context context, AttributeSet attributeSet) {
    return super.onCreateView(view, str, context, attributeSet);
}

public View onCreateView(String str, Context context, AttributeSet attributeSet) {
    return super.onCreateView(str, context, attributeSet);
}

public FirstScreenActivity() {

}

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView((int) R.layout.activity_first_screen);

    //The pages to be drawn. The last one is the one that leads to the next activity
    mImageResources = new int[]{R.drawable.intro_slide_0, R.drawable.intro_slide_1, R.drawable.intro_slide_2, 0};

    intro_images = (ViewPager) findViewById(R.id.pager);
    pager_indicator = (LinearLayout) findViewById(R.id.count);
    mIsRateMeConfigured = getIntent().getBooleanExtra("isRateMeConfigured", false);
    mAdapter = new FirstScreenAdapter(FirstScreenActivity.this, mImageResources, mIsRateMeConfigured, mSlidesCase);
    intro_images.setAdapter(mAdapter);
    intro_images.setCurrentItem(0);
    intro_images.setOnPageChangeListener(this);
    setUiPageView();
}

private void setUiPageView() {
    dotsCount = mAdapter.getCount();
    dots = new ImageView[dotsCount];
    for (int i = 0; i < dotsCount; i++) {
        dots[i] = new ImageView(this);
        dots[i].setImageDrawable(getResources().getDrawable(R.drawable.rectengle_nonselect));
        LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        params.setMargins(4, 0, 4, 0);
        pager_indicator.addView(dots[i], params);
    }
    dots[0].setImageDrawable(getResources().getDrawable(R.drawable.rectengle_select));
}

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

@Override
public void onPageSelected(int position) {
    for (int i = 0; i < dotsCount; i++) {
        dots[i].setImageDrawable(getResources().getDrawable(R.drawable.rectengle_nonselect));
    }
    dots[position].setImageDrawable(getResources().getDrawable(R.drawable.rectengle_select));

    //When the last blank page is shown -> switch to the next activity
    if(position == 3) {
        Intent intent = new Intent(FirstScreenActivity.this, NextActivity.class);
        FirstScreenActivity.this.startActivity(intent);
        FirstScreenActivity.this.finish();
    }

}

@Override
public void onPageScrollStateChanged(int state) {
}

@Override
public void onBackPressed() {

}

}

Everything works fine.

But I want to avoid showing a blank page, and if possible to slide directly to the next Activity.

Any clue for such improvement ?

0 个答案:

没有答案