Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 776
Raghav Satyadev Avatar asked Sep 01 '16 08:09

Raghav Satyadev


People also ask

What is difference between ViewPager and ViewPager2?

ViewPager2 is an improved version of the ViewPager library that offers enhanced functionality and addresses common difficulties with using ViewPager . If your app already uses ViewPager , read this page to learn more about migrating to ViewPager2 .

When should I use ViewPager?

ViewPager in Android is a class that allows the user to flip left and right through pages of data. This class provides the functionality to flip pages in app. It is a widget found in the support library. To use it you'll have to put the element inside your XML layout file that'll contain multiple child views.

Can we add activity in ViewPager?

You can't use ViewPager to swipe between Activities . You need to convert each of you five Activities into Fragments , then combine everything in one FragmentActivity with the Adapter you use with ViewPager .

What is PagerAdapter?

PagerAdapter supports data set changes. Data set changes must occur on the main thread and must end with a call to notifyDataSetChanged similar to AdapterView adapters derived from android. widget. BaseAdapter . A data set change may involve pages being added, removed, or changing position.


1 Answers

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));
   }
}
like image 188
Kirill Kulakov Avatar answered Oct 13 '22 23:10

Kirill Kulakov