Android App Development
iOS App Development
Flutter App Development
Cross Platform App Development
Hire on-demand project developers and turn your idea into working reality.
Big thanks to Webkul and his team for helping get Opencart 3.0.3.7 release ready!
Deniel Kerr
Founder. Opencart
Top Partners
In this blog, I will show you a small technique to make the Viewpager attractive.
By the implementation of a PagerAdapter, we make the views which allows the user to flip left and right through pages of data.
PagerAdapter,
ViewPager is most often used in conjunction with Fragment, which is a useful way to supply and manage the lifecycle of each page.
Fragment,
Let’s start,
<android.support.v4.view.ViewPager android:id="@+id/banner_pager" android:layout_width="match_parent" android:layout_height="wrap_content" /> 1234 <android.support.v4.view.ViewPager android:id="@+id/banner_pager" android:layout_width="match_parent" android:layout_height="wrap_content" />
I gave the name of the viewpager to banner_pager.
I have created an adapter named HomeImagePagerAdapter.
public class HomeImagePagerAdapter extends PagerAdapter { private List<Banner> images; Context mContext; public HomeImagePagerAdapter(Context context, List<Banner> images) { this.images = images; mContext = context; } @Override public void destroyItem(View container, int position, Object object) { ((ViewPager) container).removeView((View) object); } @Override public int getCount() { return images.size(); } @Override public Object instantiateItem(ViewGroup view, final int position) { LayoutInflater inflater = LayoutInflater.from(mContext); HomeViewPagerBannerBinding bannerBinding = DataBindingUtil.bind(inflater.inflate(R.layout.home_view_pager_banner, view, false));//HomeViewPagerBannerBinding.bind(view); bannerBinding.setData(images.get(position)); bannerBinding.executePendingBindings(); return bannerBinding.getRoot(); } @Override public boolean isViewFromObject(View view, Object object) { return view.equals(object); } } 123456789101112131415161718192021222324252627282930313233 public class HomeImagePagerAdapter extends PagerAdapter { private List<Banner> images; Context mContext; public HomeImagePagerAdapter(Context context, List<Banner> images) { this.images = images; mContext = context; } @Override public void destroyItem(View container, int position, Object object) { ((ViewPager) container).removeView((View) object); } @Override public int getCount() { return images.size(); } @Override public Object instantiateItem(ViewGroup view, final int position) { LayoutInflater inflater = LayoutInflater.from(mContext); HomeViewPagerBannerBinding bannerBinding = DataBindingUtil.bind(inflater.inflate(R.layout.home_view_pager_banner, view, false));//HomeViewPagerBannerBinding.bind(view); bannerBinding.setData(images.get(position)); bannerBinding.executePendingBindings(); return bannerBinding.getRoot(); } @Override public boolean isViewFromObject(View view, Object object) { return view.equals(object); }}
mainBinding.bannerPager.setAdapter(new HomeImagePagerAdapter(mContext, mainData.getBanner())); 1 mainBinding.bannerPager.setAdapter(new HomeImagePagerAdapter(mContext, mainData.getBanner()));
I have passed an array of banners to this adapter.
At this point, the Viewpager is done and working fine.
So make it more attractive.
mainBinding.bannerPager.setClipToPadding(false); mainBinding.bannerPager.setPadding(48, 0, 48, 0); mainBinding.bannerPager.setPageMargin(24); 123 mainBinding.bannerPager.setClipToPadding(false);mainBinding.bannerPager.setPadding(48, 0, 48, 0);mainBinding.bannerPager.setPageMargin(24);
These above lines make your view pager attractive. You can also add the Viewpager transition to make it more attractive.
You can follow this GitHub link to add a transition in the Viewpager. Or you can create it by your self.
This is the link: https://github.com/geftimov/android-viewpager-transformers.
If you have any issue or doubt please comment below. And keep update and stay super.
Your email address will not be published. Required fields are marked*
Name*
Email*
Save my name email and website in this browser for the next time I comment.
Be the first to comment.
We use cookies to personalize your experience. By continuing to visit this website you agree to our use of cookies. Learn more about privacy policy
It was a successful collaboration. The Mobikul team provided the client everything they needed in a timely and high-quality manner. They were very professional, reliable, and accessible during the entire process. The client also saw no room for any improvements in the vendor's performance.
Vincenzo Carrano
Administrator, Segshop
USA
India
Global
Name
Email
Enquiry or Requirement
If you have more details or questions, you can reply to the received confirmation email.