Search in sources :

Example 1 with SceneTransformer

use of redstar.onboarding.adapter.SceneTransformer in project Onboarding by eoinfogarty.

the class MainActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
    SceneTransformer sceneTransformer = new SceneTransformer();
    ScenePagerAdapter scenePagerAdapter = new ScenePagerAdapter(getSupportFragmentManager(), sceneTransformer);
    binding.tutorialPager.setAdapter(scenePagerAdapter);
    // set limit same as number of fragments
    binding.tutorialPager.setOffscreenPageLimit(3);
    binding.tutorialPager.setPageTransformer(true, sceneTransformer);
    TextPagerAdapter textAdapter = new TextPagerAdapter();
    binding.textPager.setAdapter(textAdapter);
    binding.indicator.setViewPager(binding.textPager);
    binding.indicator.setSnap(true);
    binding.indicator.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {

        @Override
        public void onPageSelected(int position) {
        // do nothing
        }

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            // translate up start button
            if (position == 1) {
                binding.start.setVisibility(View.VISIBLE);
                binding.start.setTranslationY(binding.textPager.getBottom() * (1 - positionOffset));
                binding.indicator.setAlpha(1 - positionOffset);
            }
        }
    });
    // to control the two view pagers at once we put a layout above them that intercepts the touches
    binding.touchInterceptorLayout.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View view, MotionEvent event) {
            binding.tutorialPager.onTouchEvent(event);
            binding.textPager.onTouchEvent(event);
            return true;
        }
    });
    binding.start.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            Toast.makeText(getApplicationContext(), "Start clicked", Toast.LENGTH_SHORT).show();
        }
    });
    if (savedInstanceState != null) {
        // re-add the listeners from the scene fragments
        for (Fragment fragment : getSupportFragmentManager().getFragments()) {
            sceneTransformer.addSceneChangeListener((SceneTransformer.SceneChangeListener) fragment);
        }
    }
}
Also used : ViewPager(android.support.v4.view.ViewPager) View(android.view.View) Fragment(android.support.v4.app.Fragment) SceneTransformer(redstar.onboarding.adapter.SceneTransformer) MotionEvent(android.view.MotionEvent) ScenePagerAdapter(redstar.onboarding.adapter.ScenePagerAdapter) TextPagerAdapter(redstar.onboarding.adapter.TextPagerAdapter)

Aggregations

Fragment (android.support.v4.app.Fragment)1 ViewPager (android.support.v4.view.ViewPager)1 MotionEvent (android.view.MotionEvent)1 View (android.view.View)1 ScenePagerAdapter (redstar.onboarding.adapter.ScenePagerAdapter)1 SceneTransformer (redstar.onboarding.adapter.SceneTransformer)1 TextPagerAdapter (redstar.onboarding.adapter.TextPagerAdapter)1