In this Android Studio article iam going to show you creating SwipeView Pager Adapter. So for good explanation you can watch the video for this article.
Also check my Android Development Courses
So now lets get started!
1: First of all create a new project in your Android Studio, and choose Empty Activity also iam using API 20 for this project.
2: In the second step you need to create Fragment and call it MyFragment.
3: OK now you need to create a FragmentAdapter.java class so this is the code for FragmentAdapter.java class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
package com.forogh.parwiz.swipewithviewpager; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentStatePagerAdapter; public class FragmentAdapter extends FragmentStatePagerAdapter { public FragmentAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int i) { MyFragment fragment = new MyFragment(); Bundle bundle = new Bundle(); i = i + 1; bundle.putString("MESSAGE", "Message From Page : " + i ); fragment.setArguments(bundle); return fragment; } @Override public int getCount() { return 50; } } |
OK at the code you can see that we have extended our class from FragmentStatePagerAdapter, after extend you need to implement some methods, the first one is getItem(), this is the place that we are going to get the items and also we send a bundle data to our fragment. also the second method is getCount(), and we are going to create 50 fragment for this project.
So now in the layout folder open your activity_main.xml file, and add a ViewPager in that file. after adding the file will looks like this.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<?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:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/viewpager" ></android.support.v4.view.ViewPager> </android.support.constraint.ConstraintLayout> |
And in our fragment_my.xml we are going to just add a TextView.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MyFragment"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World" android:textSize="30sp" android:textColor="@color/colorPrimary" android:textStyle="bold" android:layout_margin="100dp" /> </FrameLayout> |
So now open MyFragment.java file and we need to extend the class from Fragment. and also in CreateView method we need to inflate our layout.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
package com.forogh.parwiz.swipewithviewpager; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; /** * A simple {@link Fragment} subclass. */ public class MyFragment extends Fragment { TextView textView; public MyFragment() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View view = inflater.inflate(R.layout.fragment_my, container, false); textView = view.findViewById(R.id.textView); String message = getArguments().getString("MESSAGE"); textView.setText(message); return view; } } |
At the end you need to open your main_activity.java and create the object of FragmentAdaper and also find the id of viewpager, this is the code for our this java file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
package com.forogh.parwiz.swipewithviewpager; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; public class MainActivity extends AppCompatActivity { ViewPager viewPager; FragmentAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); viewPager = findViewById(R.id.viewpager); adapter = new FragmentAdapter(getSupportFragmentManager()); viewPager.setAdapter(adapter); } } |
Run the complete project and swipe the view this will be the result
Subscribe and Get Free Video Courses & Articles in your Email