Raghav Satyadev
Raghav Satyadev

Reputation: 748

Dynamically change count of PagerIndicator in ViewPager or PagerAdapter

enter image description here

What effect I want to have is :

  1. number of Bottom Circle indicator must be half of page count when page width is half.

  2. number of Bottom Circle indicator must be as much as page count when page width is full.

also, there is an another request:

  1. can i scroll two pages in single scroll when page width is half?
  2. and scroll only one page in single scroll when page width is full?

page width is acquired by PagerAdapter#getPageWidth()

can anyone give the perfect solution for this? without making two layout files or two adapters?

Here's the whole source code that I have developed to achieve this GIF based activity.

Question Improvement will be accepted.

https://github.com/raghavsatyadev/DemoPort

Upvotes: 6

Views: 1402

Answers (1)

Kirill Kulakov
Kirill Kulakov

Reputation: 10255

In my opinion the best practice in this case is to bind the view[s] to a view group in the adapter. In your adapter you should create a linear layout and add as much 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));
   }
}

Upvotes: 1

Related Questions