Swipe to switch screens

This time I would like to explain about ** ViewPager **.

Introduction

This time I would like to use ** ViewPager **, which allows you to switch ** View ** by swiping. That is the screen that switches when you swipe sideways. I will post the URL of what it actually looks like, so if you don't know, please check it out. https://media.giphy.com/media/pVZA3Zuhmf8uM8Vkd5/giphy.gif The source is also available on GitHub. https://github.com/minton0721/ViewPagerSample

STEP1 Editing Main Activity

First of all, add ViewPager to MainActivity.

MainActivity.xml


<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <android.support.v4.view.ViewPager
        android:id="@+id/homePage"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="vertical">
    <android.support.v4.view.PagerTabStrip
        android:id="@+id/pager_tab_strip"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:background="#fff"
        android:textColor="#FF5F5F"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"/>
    </android.support.v4.view.ViewPager>

</android.support.constraint.ConstraintLayout>

** PagerTabStrip ** will display the tab title. You can delete it if you don't need it, but without it, you won't know what page it is, so we'll implement it here. Be sure to put it in the ViewPager if you need it.

STEP2 Creating Adapter

MyFragmentStatePagerAdapter.java


public class MyFragmentStatePagerAdapter extends FragmentPagerAdapter {

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

    @Override
    public Fragment getItem(int i) {

        switch(i){
            case 0:
                return new Fragment0();
            case 1:
                return new Fragment1();
            default:
                return new Fragment2();
        }

    }

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

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position){
            case 0:
                return "Screen 1";
            case 1:
                return "Screen 2";
            default:
                return "Screen 3";

        }
    }

}

STEP3 Editing Main Activity

MainActivity.java


public class MainActivity extends AppCompatActivity {
    ViewPager viewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        viewPager = findViewById(R.id.homePage);
        viewPager.setAdapter(new MyFragmentStatePagerAdapter(getSupportFragmentManager()));

    }
}

Connect to MyFragmentStatePagerAdapter with setAdapter.

STEP4 Invitation of Fragment

Each fragment.java


class Fragment0 extends Fragment {
    View rootView;
    @Override
    public View onCreateView(LayoutInflater inflater,
                             ViewGroup container,
                             Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment0, container, false);
        return rootView;
    }

}

I will call the screen with CreateView. You may not be good at explaining it, but that's it. I want to be able to explain better ...

Recommended Posts

Swipe to switch screens
Switch from slim3-gen to slim3-gen-jsr269
Switch from JSP + JSTL to Thymeleaf
To switch JVM for each project
Switch from Eclipse to VS Code
to_ ○
[Swift5] UIButton Tap to switch tabs (applicable)
How to switch thumbnail images with JavaScript
How to switch Java versions on Mac
How to switch between multiple Java versions