How could i insert some padding between the pages inside view-pager like in the market app - the black line
ViewPager2 supports paging through a modifiable collection of fragments, calling notifyDatasetChanged() to update the UI when the underlying collection changes. This means that your app can dynamically modify the fragment collection at runtime, and ViewPager2 will correctly display the modified collection.
Android ViewPager is an interface component introduced in Android support library. It allows the user to swipe left or right to view a completely new page (screen). A ViewPager/ViewPager2 where each page fills the screen, which the user will swipe left to move to the next page, or swipe right to go back one page.
You can create swipe views using AndroidX's ViewPager widget. To use ViewPager and tabs, you need to add a dependency on ViewPager and on Material Components to your project. To insert child views that represent each page, you need to hook this layout to a PagerAdapter .
This worked for me. I just set the margin and filled it with a color.
viewPager.setPageMargin(20); // TODO Convert 'px' to 'dp'
viewPager.setPageMarginDrawable(R.color.black);
EDIT: To convert dp to pixel conversion I used:
int px = Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 20, getResources().getDisplayMetrics()));
mViewPager.setPageMargin(px);
mViewPager.setPageMarginDrawable(R.color.black);
That is a new api for the ViewPager
called the margin, a link to it can be found setPageMargin()
. Make sure you are using the latest support library.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With