Adding/Removing views for PagerAdapter / ViewPager issue Android - android

I would like to keep a list of 3 views at all times. The app starts at position 1 (out of positions 0,1,2). When someone scrolls to position 0, I would like to remove view 2, and create a view before position 0. This way, it appears to the user, that there are unlimited views. In the same way, when someone scrolls to position 2, I would like to remove the view at position 0 and add one at the end.
However I'm having problems with both adding and removing views. When I get to position 0, nothing changes unless I try scrolling past position 0 (to position -1, i.e. the boundary is hit). At that point, I can see that it is the boundary of my views, but then setCurrentItem(1,false) is triggered and I'm brought back to the middle of the views. When I scroll to position 2 I see that position 2 has been updated. However position 0 and 1 remain the same.
When I scroll to position 2, nothing happens. However if I try and scroll to the boundary, for some reason, position 0 gets updated and setCurrentItem(1,false) is triggered.
I have no idea why its happening like this. Can anyone shed some light on this?
Here is my code:
public class MainActivity extends Activity {
ArrayList<Integer> showThree = new ArrayList<Integer>();
int focusedPage = 0;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
showThree.add(0,5); //adding integers 5,6,7 for positions 0,1,2
showThree.add(1,6);
showThree.add(2,7);
final MyPagerAdapter adapter = new MyPagerAdapter(getApplicationContext(),showThree);
final ViewPager myPager = (ViewPager) findViewById(R.id.mypanelpager);
myPager.setAdapter(adapter);
myPager.setCurrentItem(1);
myPager.setOnPageChangeListener(new OnPageChangeListener(){
#Override
public void onPageScrollStateChanged(int state) {
if (state == ViewPager.SCROLL_STATE_IDLE) {
//when position= 0, change the 3 views from 5,6,7 to 4,5,6.
if (focusedPage == 0) {
showThreeMonths.set(0,4);
showThreeMonths.set(1,5);
showThreeMonths.set(2,6);
adapter.notifyDataSetChanged();
adapter.startUpdate(myPager);
}
else if (focusedPage ==2){
//ignore, just testing focusPage=0 for now }
}
//set current page to the middle of the 3 new views, which would be
//the same view at position 0 of the old 3 views.
//Thus user doesn't experience the views changing despite being 3 new views.
myPager.setCurrentItem(1,false);
}
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
// TODO Auto-generated method stub
}
#Override
public void onPageSelected(int position) {
focusedPage = position;
}
});
}
PagerAdapter
public class MyPagerAdapter extends PagerAdapter {
private ArrayList<Integer> showThreeMonths;
private Context ctx;
public MyPagerAdapter (Context ctx, ArrayList<Integer> showThree){
this.ctx = ctx ;
this.showThree = showThree;
}
#Override
public int getCount() {
return showThree.size();
}
public Object instantiateItem(ViewGroup collection, int position ){
//NewCustomView is a class I made that takes parameters context and an integer and creates a view based on the integer
NewCustomView MyOwnView = new NewCustomView(ctx, showThree.get(position));
View customViewLayout = MyOwnView.newLayout; //part of the class object
collection.addView(customViewLayout);
return customViewLayout;
}
#Override
public void destroyItem(ViewGroup collection, int position, Object arg2) {
((ViewPager) collection).removeView((ViewGroup) arg2);}
#Override
public Parcelable saveState() {
return null;}
#Override
public boolean isViewFromObject(View view, Object arg1) {
return view==arg1;}
#Override
public void startUpdate(ViewGroup collection) {}
#Override
public void finishUpdate(ViewGroup collection) {}
}

The instantiateItem() method creates the 2 view pages in the memory by default. Therefore when you swipe to the second page then 0 page is recreated as it's outside the range of the 2 pages saved in the memory. Please try to use
myViewPager.setOffscreenPageLimit(numberOfPages)
method that receives an integer as a parameter and declares how many pages it should be keeping before recycling them.

Related

ViewPager + PagerAdapter w/ Pagination not working properly

I have a gallery/slideshow activity that allows user to swipe between Photos. Each "Page" is just a TouchImageView. There is a pagination logic in there, and I can see that it is calling the API accordingly. However, I am not able to swipe further even after notifyDataSetChanged has been called. Here's the code:
Activity {
ViewPager vp;
CustomPagerAdapter pagerAdapter;
onCreate() {
//api callback
pagerAdapter = new CustomPagerAdapter();
vp.setAdapter(pagerAdapter);
}
}
CustomPagerAdapter() {
TouchImageView imageView;
List<Photos> photos;
int getCount() {
return photos == null ? 0 : photos.size();
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
View itemView = mLayoutInflater.inflate(R.layout.pager_image, container, false);
imageView = (TouchImageView) itemView.findViewById(R.id.photo);
Glide.with(context)
.load(photos.get(position).getUrl())
.into(imageView);
container.addView(itemView);
if (position == photos.size()-1) {
loadMorePhotos();
}
return itemView;
}
#Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((RelativeLayout) object);
}
void loadMorePhotos() {
//call api and stuff
void onResponse(Call call, Response<List<Photo>> response) {
photos.addAll(response.body);
notifyDataSetChanged();
}
}
}
And this is the result:
http://imgur.com/nA1TzfI
I have no idea what is going on for 2 days. Please help!
This may not be ideal, but where you call notifyDatasetChanged() you could instead try calling vp.setAdapter(pagerAdapter);
(i.e. actually re-set the adapter). A downside of this is that the pager will probably go back to having the first page selected. To avoid this, you'd need to save the latest page somewhere and restore it afterwards.
As I say, not ideal, but it may help.
You dont have to load more photos from adapter class, in your activity class you can use onPageScrolled and call the api when the last item is scrolled and set adapter again also save state of view pager before calling api to retain it when adapter is set again.
private boolean isLastPageSwiped;
private int counterPageScroll;
private OnPageChangeListener mListener = new OnPageChangeListener() {
#Override
public void onPageSelected(int arg0) {
// TODO Auto-generated method stub
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
if (position == 6 && positionOffset == 0 && !isLastPageSwiped){
if(counterPageScroll != 0){
isLastPageSwiped=true;
//call api
}
counterPageScroll++;
}else{
counterPageScroll=0;
}
}
#Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
};
and use this listener
vp.setOnPageChangeListener(mListener);
We can use this approach for doing pagination on ViewPager (with fragment)
load new page data but not append to the adapter
make sure the page is not scrolling, then append new page data into the adapter (ViewPager.OnPageChangeListener#onPageScrolled's positionOffsetPixels == 0).
update current item for view pager with offset = new page data's size (let's call it newPosition)
rebind data to 3 fragments: newPosition - 1, newPosition, newPosition + 1
For getting current fragment, we can use fragment manager
private fun getFragment(position: Int): PageFragment? =
supportFragmentManager.fragments.firstOrNull {
(it as? PageFragment)?.position == position // always pass possition value into the fragment
} as? PageFragment
We need to append data when not scrolling (step 2) to have a better UX to users. If we append data immediately when it comes and if user is scrolling, view pager's setCurrentItem will make a strange move.
I create a simple version here https://github.com/tuanchauict/DemoPaginationViewPager. Please take a look

Viewpager not getting last item

I am using two Viewpager in my app,
1) First Viewpager displays images only
2) I am displaying price
now the issue is i have 4 images displaying in my viewpager1, and in second pager i have price as per selected product. first time it does not show anything, but when i scroll image and goes to next, it shows price..
pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
Picasso.with(ProductLandingActivity.this) .load(categorylist.get(position).getProductLanding_packLink())
.error(R.drawable.nopreview )
.placeholder(R.drawable.progress_animation)
.into(selectedImage);
System.out.println("Selected is"+position);
selectedname.setText(categorylist.get(position).getProductLanding_packDesc());
for (int i = 0; i < categorylist.get(position).getItems().size(); i++) {
System.out.println("ProductPack_ID : " + categorylist.get(position).getItems().get(i).getPackSize_sellingPrice());
}
temp = categorylist.get(position).getItems();
packadapter = new MyPacksPagerAdapter(ProductLandingActivity.this,categorylist);
pagerpacks.setAdapter(packadapter);
}
#Override
public void onPageSelected(int position) {
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
adapter
private class MyPacksPagerAdapter extends PagerAdapter {
Context context;
ArrayList<PackListModel> packsizedata ;
public MyPacksPagerAdapter(Context context,ArrayList<PackListModel> packsizedata) {
this.context = context;
this.packsizedata = packsizedata;
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
final OneActor oneActor;
View view;
LayoutInflater infl = ((Activity)context).getLayoutInflater();
view = infl.inflate(R.layout.list_item_pagerpacktitles, container,false);
oneActor = new OneActor();
// oneActor.avatar = (ImageView) view.findViewById(R.id.image);
oneActor.name = (TextView) view.findViewById(R.id.product_landing_packsname);
oneActor.cmtCount = (TextView) view.findViewById(R.id.product_landing_packsprice);
view.setTag(oneActor);
oneActor.name.setText(temp.get(position).getPackSize_packSize());
oneActor.cmtCount.setText(temp.get(position).getPackSize_sellingPrice());
((ViewGroup) container).addView(view);
return view;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View)object);
}
#Override
public int getCount() {
return packsizedata.size();
}
#Override
public boolean isViewFromObject(View view, Object object) {
return (view == object);
}
class OneActor{
// ImageView avatar;
TextView name,cmtCount;
}
}
By defaulat when i run the app it shows like this,in second pager it is not showing product price,
But when i scroll image it shows price
My Expected output is
This is my json response
http://pastebin.com/fbJang2B
First, as mentioned by #NotobharatKumar, you're loading the second adapter on a wrong event of the parent adapter, ie in onPageScrolled method. Second, you're setting a new adapter each time on that specific event, it seems useless. Finally, you are setting datas in an event, (I'm not sure why you're doing this but) I'd prefer to set it in a separate adapter and let the events listener for specific behaviors.
For me, it seems that you have two separate adapters, but one datas list shared by both. Assuming that, you have to set them both at start with their datas respectly, and on the event onPageSelected of the top adapter, you just have to automatically scroll the second. And if they have the same position in the list, onPageSelected should do the work correctly.
So, these modifications should solve your issue:
Your code in onScrollChanged, when you set the image and the text, seems really weird to me. I'd use a first adapter where I'll set all the datas like these two for the first ViewPager:
#Override
public void onCreate(Bundle savedInstansteState) {
...
// set a simple adapter for the first ViewPager
FirstPagerAdapter imageadapter =
new FirstPagerAdapter(ProductLandingActivity.this, categorylist);
pagerimages.setAdapter(imageadapter);
...
}
Then, as usual, set your datas in the FirstPagerAdapter:
#Override
public View getView(int position, View view, ViewGroup container) {
...
// set the content
Picasso.with(ProductLandingActivity.this)
.load(categorylist.get(position).getProductLanding_packLink())
.error(R.drawable.nopreview )
.placeholder(R.drawable.progress_animation)
.into(selectedImage);
selectedname.setText(
categorylist.get(position).getProductLanding_packDesc());
...
}
Then no need to (re)load the image or the text when an event is triggered, since they will be holding by the adapter.
You only use getPackSize_packSize() and getPackSize_sellingPrice() in the second adapter, so you should create a separate list to only fill with these datas but outside the swiping event. Start by initializing the second list and the adapters:
// get the Items to fill the pack items list (like you did for `temp`)
ArrayList<Items> packItems = new ArrayList<>();
for (int i = 0; i < categorylist.size(); i++) {
packItems.add(categorylist.get(position).getItems());
}
// fill the first adapter with your list of products (ie categorylist)
...
pagerimages.setAdapter(imageadapter);
...
// fill the second adapter with the pack items list
packadapter = new MyPacksPagerAdapter(ProductLandingActivity.this, packItems);
pagerpacks.setAdapter(packadapter);
You have to do this when categorylist is created and populated. So place this above code for example in your callback, when you retrieve the datas from your server.
Since the second list packItems is filling in the same order than categorylist, there will be no weird behavior by changing the both positions.
Now, in the second adapter, it's preferable to use the local list packsizedata, as follows:
#Override
public Object instantiateItem(ViewGroup container, int position) {
...
oneActor.name.setText(
packsizedata.get(position).getPackSize_packSize());
oneActor.cmtCount.setText(
packsizedata.get(position).getPackSize_sellingPrice());
...
}
Finally, control the bottom ViewPager by using onPageSelected event of the first:
pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset,
int positionOffsetPixels) { }
#Override
public void onPageSelected(int position) {
// set the second current page regarding the position of the first
pagerpacks.setCurrentItem(position);
}
#Override
public void onPageScrollStateChanged(int state) { }
});
Hope this will be helpful.
You should implement code for action at onPageSelected method because after page changed of your ViewPager then onPageSelected will be called.

ViewPager not showing the right page

I am trying to implement a ViewPager without using Fragments. Let's say, I have an unspecific amount of data stored inside some kind of database which is mapped to specific days. What I am trying to do is to display the data for a specific day on ViewPager pages.
This is what I am currently doing:
#Override
protected void onCreate(Bundle savedInstanceState) {
dataList = (ViewPager) findViewById(R.id.dataList);
dataList.setAdapter(new DataListSlidePagerAdapter());
dataList.setCurrentItem(Integer.MAX_VALUE / 2);
}
private class DataListSlidePagerAdapter extends PagerAdapter {
int lastPosition;
#Override
public Object instantiateItem(ViewGroup collection, int position) {
if (position > lastPosition) {
GregorianCalendar today = Settings.getCurrentDay();
today.add(GregorianCalendar.DATE, 1);
Settings.loadData();
}
else if (position < lastPosition) {
GregorianCalendar today = Settings.getCurrentDay();
today.add(GregorianCalendar.DATE, -1);
Settings.loadData();
}
lastPosition = position;
LinearLayout v = createDataList(Settings.getTodaysData());
collection.addView(v);
return v;
}
#Override
public void destroyItem(ViewGroup collection, int position, Object view) {
((ViewPager) collection).removeView((LinearLayout) view);
}
#Override
public int getCount() {
return Integer.MAX_VALUE;
}
#Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}
public int getItemPosition(Object object) {
return POSITION_NONE;
}
}
This actually almost works, but unfortunately, the data for the current day is never shown on the current page, but rather on the next or previous page. My guess would be, that this is because instantiateItem does not necessarily build the view of the current page, but rather of some page which is probably needed to be shown next. createListData(), however, always creates the view of the current day.
Any idea how to fix this and to display the right data on the right pages? You can assume, that there are also functions like Settings.getTomorrowsData() and Settings.getYesterdaysData().

ListView not visible in ViewPager in custom ViewGroup

I have a custom ViewGroup that has a ViewPager as a child. This is because I want to add a static header to the ViewPager and mess with onDraw() to make the header scroll vertically with the ViewPager content. My ViewPager has a ListView as a child. I'm able to create the ListView and the adapter and even set the adapter to the ListView. When I read wallList.getAdapter().getItemAt(0).toString() it returns the data I would expect it to return. But for some reason, I can't see the ListView at all.
The ViewPager still works as intended and the other four pages have arbitrary text for the time being. But the center page which is supposed to contain the ListView shows a blank screen. I can't figure out what's going on, but it sounds similar to a problem I had before here and was able to hack my way to get something acceptable, but wasn't able to answer my original question.
It seems like you can only call setAdapter() in certain places, not only does it have to be on the UI thread (I think) but it seems to have trouble doing it within certain methods, custom or overrided. Here's my custom ViewGroup code.
public class CustomProfilePager extends ViewGroup{
Bitmap coverPhoto, profilePhoto;
Paint coverStyle, profileStyle;
String name;
int coverHeight;
ViewPager pager;
ProfilePagerAdapter pagerAdapter;
ListView wallList;
Context context;
public CustomProfilePager(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
}
#Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
Log.i("CustomPager", "calling onLayout()");
pager.layout(0, coverHeight+240, getWidth(), getHeight());
// for(int i=0; i<getChildCount(); i++){
// getChildAt(i).layout(l, t, r, b);
// }
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
#Override
protected void onDraw(Canvas canvas) {
Log.i("CustomPager", "calling onDraw()");
super.onDraw(canvas);
if(coverPhoto!=null){
canvas.drawBitmap(coverPhoto, 0, 0, coverStyle);
}
}
public void init(String name){
Log.i("CustomPager", "calling init()");
this.name = name;
coverStyle = new Paint();
coverHeight = (int) (getWidth()/2.7);
profileStyle = new Paint();
wallList = new ListView(context);
pagerAdapter = new ProfilePagerAdapter();
pager = new ViewPager(context);
pager.setAdapter(pagerAdapter);
addView(pager);
pager.setCurrentItem(2);
pager.setOffscreenPageLimit(4);
}
public void setCoverPhoto(Bitmap bitmap){
Log.i("CustomPager", "calling setCoverPhoto()");
int initialWidth = bitmap.getWidth();
int initialHeight = bitmap.getHeight();
int finalHeight = (int) (initialWidth/2.7);
int initialYoffset = (int) (initialHeight-finalHeight)/2;
this.coverPhoto = Bitmap.createBitmap(bitmap, 0, initialYoffset, bitmap.getWidth(), finalHeight);
invalidate();
}
public void setProfilePhoto(Bitmap bitmap){
this.profilePhoto = bitmap;
}
public ViewPager getViewPager(){
return pager;
}
public void setWallAdapter(Profile.WallAdapter adapter){
Log.i("CustomPager", "calling setWallAdapter()");
wallList.setAdapter(adapter);
}
public class ProfilePagerAdapter extends PagerAdapter {
#Override
public void destroyItem(ViewGroup collection, int position, Object view) {
((ViewPager) collection).removeView((View) view);
}
#Override
public Object instantiateItem(ViewGroup collection, int position) {
ViewPager parent = (ViewPager) collection;
switch (position) {
case 2: // Wall
wallList.setDividerHeight(0);
parent.addView(wallList);
return wallList;
default:
TextView testText = new TextView(context);
testText.setText(String.valueOf(position) + ": " + name);
testText.setTextSize(46);
testText.setGravity(Gravity.CENTER);
parent.addView(testText);
return testText;
}
}
#Override
public int getCount() {
return 5;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
}
}
I too have experience that, Some of the View in my ViewPager were getting lost. When debug came to know that it contain only 2 (some time even 1) views in ViewPager. But actually there has to be 3 views since i implemented circular viewpager.
After many trial and error method, i commented the removeView line from destoryItem and i was surprise and it was working perfectly.
I am not sure will this solved your problem since the documentation for destroyItem says
Remove a page for the given position. The adapter is responsible for
removing the view from its container, although it only must ensure
this is done by the time it returns from finishUpdate(ViewGroup).
For me, there should always be 3 views in my ViewPager. since it was a circular ViewPager.
public class ProfilePagerAdapter extends PagerAdapter {
#Override
public void destroyItem(ViewGroup collection, int position, Object view) {
}
#Override
public Object instantiateItem(ViewGroup collection, int position) {
ViewPager parent = (ViewPager) collection;
switch (position) {
case 2: // Wall
wallList.setDividerHeight(0);
parent.addView(wallList);
return wallList;
default:
TextView testText = new TextView(context);
testText.setText(String.valueOf(position) + ": " + name);
testText.setTextSize(46);
testText.setGravity(Gravity.CENTER);
parent.addView(testText);
return testText;
}
}
#Override
public int getCount() {
return 5;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
}
There is a chance that you may only really need to call
notifyDataSetChanged()
on the PagerAdapter and the ListView will then be displayed properly.
Have you called setWillNotDrawEnabled(false) on your customViewGroup?
By default ViewGroups dont call the onDraw method, also try implmenting the onDispatchDraw instead of the onDraw method
Android Custom Layout - onDraw() never gets called
The problem wasn't really obvious and I'm not sure exactly what caused it in the first place. All I had to do was sublass FrameLayout instead of ViewGroup and everything works.

Android PagerAdapter, get current position

I want to get the current position of the visible view of my PagerAdapter
I didn't see an obvious function like getPosition() and I want one.
I want to add an object into its arraylist at that position, but I need to know it first
You would use:
int position = mViewPager.getCurrentItem()
I had this problem and could not get the getCurrentItem() methode.
I ended up getting the position from the ViewPager and not from the PageAdapter. The onPageSelected(int currentPage) methode is getting the currently displayed page.
//custom PageAdapter implementation
mAdapter = new AwesomePagerAdapter();
//Our custom view pager that extends from ViewPager
mPager = (CustomViewPager) findViewById(R.id.preview_gallery);
mPager.setAdapter(mAdapter);
// get the item that we should be showing from the intent
mCurrentPage = extra.getInt("currentIndex");
// show the item the user picked
mPager.setCurrentItem(mCurrentPage);
// listen for page changes so we can track the current index
mPager.setOnPageChangeListener(new OnPageChangeListener() {
public void onPageScrollStateChanged(int arg0) {
}
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
public void onPageSelected(int currentPage) {
//currentPage is the position that is currently displayed.
}
});
Doing it in the PageAdaper didn't work for me as I want to preload images that are not visible. The position that is passed instantiateItem(View collection, int position) of the PageAdapter` is the position of the next item initialized. This has nothing to do with the item that is displayed.
https://github.com/Shereef/ViewPagerPlusExpandableList/blob/master/src/net/shereef/vewpagerplusexpandablelistexample/ViewPagerPlusExpandableListActivity.java#L204
if i write after that line
Log.i("pager",myPager.getCurrentItem()+"");
it will show in the logcat the current item page while the oncreate is being run which is always 0
noteice i have used the object for the viewpager it self not the adapter.
Here's an updated solution that I used myself. Since setOnPageChangeListener is now deprecated you must use addOnPageChangeListener.
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
int counterPosition;
if (position == 0 || position <= currentInventory.size()){
counterPosition = position + 1;
} else {
counterPosition = position;
}
viewPagerHeader.setText("Prize " + counterPosition + " of " + currentInventory.size());
}
#Override
public void onPageSelected(int position) {
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
The implementation above displays the correct index in my TextView so that positions 0 and the last item in the listcurrentInventory.size() display correctly.
Hope this helps someone looking for an updated solution.
Try this:
imageSlider.addOnPageChangeListener(object: ViewPager.OnPageChangeListener{
override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {}
override fun onPageSelected(position: Int) {Log.d(TAG, "Page No.: $position")}
override fun onPageScrollStateChanged(state: Int) {}
})
I just faced this problem yesterday. I needed to start playing an animation in each page when the page is visible to the user and not before.
My adapter inherits from PagerAdapter and I found out that there is a function setPrimaryItem() which is triggered every time a page is shown to the user as the current page.
Called to inform the adapter of which item is currently considered to be the "primary", that is the one show to the user as the current page. This method will not be invoked when the adapter contains no items.
Not only you have a callback for when the page is visible but this callback also provides the position of the current page.
class MyAdapter extends PagerAdapter {
#Override
public int getCount() {
...
}
#Override
public boolean isViewFromObject(#NonNull #NotNull View view, #NonNull #NotNull Object object) {
...
}
#Override
public void setPrimaryItem(#NonNull #NotNull ViewGroup container, int position, #NonNull #NotNull Object object) {
super.setPrimaryItem(container, position, object);
// `position` gives you the position of the current page
// And this is how I managed to play the animation (Lottie library) when the page is visible to the user
LottieAnimationView animation = ((ViewGroup) object).findViewById(R.id.my_animation_view);
animation.playAnimation();
}
}

Categories

Resources