Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

insert separator between view-pager [duplicate]

How could i insert some padding between the pages inside view-pager like in the market app - the black line enter image description here

like image 946
Tomer Mor Avatar asked Aug 13 '12 13:08

Tomer Mor


People also ask

What does ViewPager2 do?

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.

What is ViewPager2 in android?

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.

How do I use ViewPager?

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 .


2 Answers

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);
like image 93
SammyT Avatar answered Oct 16 '22 18:10

SammyT


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.

like image 35
HandlerExploit Avatar answered Oct 16 '22 18:10

HandlerExploit