scrollview - android scrollto does not work in onResume method -


i scroll screen particular position when screen displayed did code in onresume function of fragment

  scrollview.post(new runnable() {         @override public void run () {             scrollview.scrollto(0, -200);             log.d(tag, "x: " + scrollview.getscrollx() + " " + "y: " + scrollview.getscrolly());         }     }      ); 

but scrollview doesn 't scroll

i've encountered same problem scrolling fragment previous position when returning it. no scrolling possible in onresume(). suspect when post runnable (as mention in question) there no guarantee whether work.

i've found 2 solutions, hope helps:

1) more general approach, still doesn't work api level < 11:

public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) {     // inflate main view     mview = inflater.inflate(r.layout.your_fragment_id, container, false);      // find scroll view     mscrollcontainer = (scrollview) mview.findviewbyid(r.id.scroll_container);       // add onlayoutchangelistener     mview.addonlayoutchangelistener(new view.onlayoutchangelistener() {         @override         public void onlayoutchange(view v, int left, int top, int right, int bottom, int oldleft, int oldtop, int oldright, int oldbottom) {                  // x , y scroll                  ...                 mscrollcontainer.scrollto(x,y);             }         }     }); } 

you should x , y own source (say own bundle), since in cases - when activity doesn't save it's state - can't use fragment's savedinstancestate (see here)

2) more specific more useful method set onfocuschangelistener element gets focus after fragment shown:

public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) {         ...      mlistview.setonfocuschangelistener(new onfocuschangelistener() {         @override         public void onfocuschange(view v, boolean hasfocus) {         // scrolling here            }     }); } 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -