Android - Animate RecyclerView Item of Fragment Inside ViewPager -
i using viewpager
fragments
. each fragment have recyclerview
gridlayoutmanager
. inside recyclerview
adapter have set animation on onbindviewholder
.
problem :
as viewpager loads 2 fragments @ once, animation second fragments gets completed if view not visible
expected output :
when swipe on viewpager recyclerview item should animate.
recyclerview adapter
public void onbindviewholder(viewholder viewholder, final int position) { viewholder.getserviceimageview().setimageresource(mservices.get(position).getimageresource()); viewholder.getservicenameview().settext(mservices.get(position).getservicename()); viewholder.getpriceperhourview().settext(string.valueof(mservices.get(position).getpriceperhour())); setanimation(viewholder.serviceitemcontainer, position); } private void setanimation(view viewtoanimate, int position) { // if bound view wasn't displayed on screen, it's animated if (position > lastposition) { animation animation = animationutils.loadanimation(mcontext, r.anim.fade_in); viewtoanimate.startanimation(animation); lastposition = position; } }
fragment code
public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.fragment_cleaning, container, false); mrecyclerview = (recyclerview) rootview.findviewbyid(r.id.recyclerview); mrecyclerview.setlayoutmanager(new gridlayoutmanager(getcontext(), span_count)); mservicesadapter = new recyclerviewadapter(getcontext(), data); mrecyclerview.setadapter(mservicesadapter); return rootview; }
viewpager adapter
private class sectionspageradapter extends fragmentstatepageradapter { public sectionspageradapter(fragmentmanager fm) { super(fm); } @override public fragment getitem(int position) { switch (position) { case 0: fragment = new fragment1(); case 1: return new fragment2(); case 2: return new fragment3(); default: return new fragment4(); } } @override public int getcount() { return 4; } @override public charsequence getpagetitle(int position) { locale l = locale.getdefault(); switch (position) { case 0: return getstring(r.string.title_section1).touppercase(l); case 1: return getstring(r.string.title_section2).touppercase(l); case 2: return getstring(r.string.title_section3).touppercase(l); case 3: return getstring(r.string.title_section4).touppercase(l); } return null; }
can suggest me how this?
try this
- implement own onresumefragment() @ activity of fragment want animate
- at onresumefragment() call adapter.notifydatasetchanged(); start animations
Comments
Post a Comment