Dynamically change PagerIndicator counter in ViewPager or PagerAdapter

enter image description here

What effect do I want to get:

  • the bottom circle indicator number should be half the number of pages when the page width is half.

  • the number of indicators of the lower circle should be equal to the number of pages when the page width is full.

there is also another request:

  • Is it possible to scroll two pages in a single scroll when the page width is half?
  • and scroll only one page in a single scroll when the page width is full?

page width obtained by PagerAdapter # getPageWidth ()

can anyone give a perfect solution for this? without creating two layout files or two adapters?

Here is all the source code that I developed to achieve this GIF-based activity.

The question will be accepted.

https://github.com/raghavsatyadev/DemoPort

+6
source share
1 answer

In my opinion, the best practice in this case is to bind the view [s] to the group of views in the adapter. In your adapter, you must create a linear layout and add as many children as you want.

public Object instantiateItem(ViewGroup container, int position) { LinearLayout ll = new LinearLayout(context); LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(0,MATCH_PART); param.weight = 1.0f; for (int i; i < getChildrenInPage() ; i++) { MyView myView = View.inflate(context, R.layout.my_layout, null) myView.bind(getDataForPosition(getChildrenInPage()*position + i)) ll.add(myView, params)); } } 
+1
source

All Articles