Android - smoothScrollToPosition inside a child Fragment in a ViewPager -
i'm having difficulty in setting smooth scroll fragments
inside viewpager
, nested in fragment
acts parent.
i set method :
public void scrolltotop() { this.recyclerview.smoothscrolltoposition(0); }
for fragments
not in viewpager
, don't know how reference of fragments
inside viewpager
.
i did
bottomlobifragment activelobi = (bottomlobifragment)getsupportfragmentmanager().findfragmentbytag(utilities.lobbytag);
to parent fragment
.
code viewpager
(the rest identical, difference @ number of item, , constructor)
public class lobbypageradapter extends fragmentstatepageradapter { public lobbypageradapter (fragmentmanager fm) { super(fm); } @override public fragment getitem(int pos) { switch(pos) { case 0: return friends.newinstance("friends, instance 1"); // case 1: return photo.newinstance("photo, instance 1"); // case 2: return activities.newinstance("activities, instance 1"); default: return friends.newinstance("friends, default"); } } @override public charsequence getpagetitle(int position) { return content[position % content.length]; } @override public int getcount() { //return 3; return content.length; } private class setadaptertask extends asynctask<void,void,void> { protected void doinbackground(void... params) { return null; } @override protected void onpostexecute(void result) { } } }
note : every adapter located inside parent fragment
first modify adapter this
public class lobbypageradapter extends fragmentstatepageradapter { private arraylist<fragment> mfragments = new arraylist<>(); public lobbypageradapter (fragmentmanager fm) { super(fm); mfragments.add(friends.newinstance("friends, instance 1")); mfragments.add(photo.newinstance("photo, instance 1")); mfragments.add(activities.newinstance("activities, instance 1")); } @override public fragment getitem(int pos) { return mfragments.get(pos); } @override public charsequence getpagetitle(int position) { return content[position % content.length]; } @override public int getcount() { //return 3; return content.length; } private class setadaptertask extends asynctask<void,void,void> { protected void doinbackground(void... params) { return null; } @override protected void onpostexecute(void result) { } } }
now in fragment or activity holding viewpager & adapter instance, can adapter.getitem(1); example :
//suppose lobbypageradapter instance looks in code lobbypageradapter adapter = new lobbypageradapter(getfragmentmanager()); // , want fragment @ second position in tab photo photofragmentobject = (photo)adapter.getitem(1); // , want fragment @ first position in tab friends friendsfragmentobject = (friends)adapter.getitem(0);
Comments
Post a Comment