android – Swipe between fragment with ViewPager and without TabLayout-ThrowExceptions

Exception or error:

I have 3 fragments (totally different from each other) and one activity (MainActivity). What I would like to do is to be able to swipe between them (with finger, not with buttons) with a transition like a TabLayout.

According to what I saw, I can do it using ViewPager. But the problem is that ViewPager uses TabLayout.

There is a way to swipe betweens fragments, using Viewpager, without TabLayout ?

How to solve:

This code will help you

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>

<android.support.v4.view.ViewPager
    android:id="@+id/view_pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

Set up the child Fragments you would like to display. I made 3 child Fragments, calling them ChildFragment1, ChildFragment2, and ChildFragment3. Remember to have them extend support.v4.app.fragment. Now make layouts for all three of the Fragments. I call them child_fragment_1_layout, child_fragment_2_layout, and child_fragment_3_layout.

public class ChildFragment1 extends Fragment {

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    super.onCreateView(inflater, container, savedInstanceState);
    View rootView = inflater.inflate(R.layout.child_fragment_1_layout, container, false);
    Button buttonInFragment1 = rootView.findViewById(R.id.button_1);
    buttonInFragment1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(getContext(), "button in fragment 1", Toast.LENGTH_SHORT).show();
        }
    });

    return rootView;
}
}

Make an adapter for the ViewPager. You will have to extend either theFragmentPagerAdapter or the FragmentStatePagerAdapter. For this tutorial we will use the FragmentPagerAdapter . The difference between the two can be found here. After extending the FragmentPagerAdapter, you will need to call super(FragmentManager) in your constructor and implement the methods getItem(position) and getCount(). The getItem(position)method is used to return the fragment at the corresponding position, ordered from left to right. So ChildFragment1 would be at position 0, ChildFragment2 would be at position 1, and ChildFragment3 would be at position 2. The getCount() method is to count how many Fragments there are to display, and in this case, there are 3.

public class ViewPagerAdapter extends FragmentPagerAdapter {

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

@Override
public Fragment getItem(int position) {
    switch (position)
    {
        case 0:
            return new ChildFragment1(); //ChildFragment1 at position 0
        case 1:
            return new ChildFragment2(); //ChildFragment2 at position 1
        case 2:
            return new ChildFragment3(); //ChildFragment3 at position 2
    }
    return null; //does not happen
}

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

Now find your ViewPager in MainActivity and call the setAdapter() method and pass in your custom adapter. If you are doing this in another Fragment (Nested Fragments), you will have to pass in getChildFragmentManager() in the argument of your adapter instead. Now your ViewPager is all set

public class MainActivity extends AppCompatActivity {

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

    ViewPager viewPager = findViewById(R.id.view_pager);
    viewPager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager()));
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *