ViewPager In Android
Step 1 : create ScreenSlidePagerActivity like below.
Step 2 : create ScreenSlidePagerAdapter like below attached find it.
Step 3 : activity_screen_slide_pager layout
Step 4 : create fragment and layout.
Step 6 : Run and watch the output .
Step 7: Comment this tutorial below or write what you want.
Step 1:
public class ScreenSlidePagerActivity extends FragmentActivity {
private static final int NUM_PAGES = 5;
private ViewPager mPager;
private PagerAdapter mPagerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_screen_slide_pager);
mPager = (ViewPager) findViewById(R.id.pager);
mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter);
}
@Override
public void onBackPressed() {
if (mPager.getCurrentItem() == 0) {
super.onBackPressed();
} else {
mPager.setCurrentItem(mPager.getCurrentItem() - 1);
}
}
}
Step 2:
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
Bundle bundle = new Bundle();
ScreenSlidePageFragment fragment = new ScreenSlidePageFragment();
switch (position) {
case 0:
bundle.putString("name","Lokesh");
fragment.setArguments(bundle);
return fragment;
case 1:
bundle.putString("name","Krishna");
fragment.setArguments(bundle);
return fragment;
case 2:
bundle.putString("name","Ram");
fragment.setArguments(bundle);
return fragment;
case 3:
bundle.putString("name","Arjun");
fragment.setArguments(bundle);
return fragment;
case 4:
bundle.putString("name","Eashwar");
fragment.setArguments(bundle);
return fragment;
default:return fragment;
}
}
@Override
public int getCount() {
return NUM_PAGES;
}
}
Step 3:
activity_screen_slide_pager
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_screen_slide_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="mycompany.com.viewpagerdemo.ScreenSlidePagerActivity">
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
step 4:
public class ScreenSlidePageFragment extends Fragment {
String mName;
TextView mTextView;
ImageView mImageView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Bundle bundle = getArguments();
if(bundle.getString("name") != null) {
mName = bundle.getString("name");
}
ViewGroup rootView = (ViewGroup) inflater.inflate(
R.layout.fragment_screen_slide_page, container, false);
mTextView = (TextView) rootView.findViewById(R.id.name);
mTextView.setText(mName);
mImageView = (ImageView) rootView.findViewById(R.id.image);
switch (mName) {
case "Lokesh":
mImageView.setImageResource(R.drawable.androidwithglasses);
break;
case "Krishna":
mImageView.setImageResource(R.drawable.krishna);
break;
case "Ram":
mImageView.setImageResource(R.drawable.ram);
break;
case "Arjun":
mImageView.setImageResource(R.drawable.arjun);
break;
case "Eashwar":
mImageView.setImageResource(R.drawable.shiva);
break;
}
return rootView;
}
}
fragment_screen_slide_page:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView style="?android:textAppearanceMedium"
android:padding="16dp"
android:id="@+id/name"
android:layout_centerHorizontal="true"
android:lineSpacingMultiplier="1.2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/lorem_ipsum" />
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/name"/>
</RelativeLayout>
Output:
after swipped you will get..