Android how to move scrollview child to top programatically -
i have scrollview
contains numerous views on linearlayout
.i have implemented expand
, collapse
layouts. onclick
of view,it either expand or collapse.so when view expands,i want programmatically move scrollview
such view comes on top of screen.see image below
i want move scrollview
length shown on red line. child
of scrollview
contains textview
(4:status bar notification) , imageview
.further scrollview
may or maynot contains views above or below textview imageview
combination. have used customscrollview findout scroll y
public int newy=0; @override protected void onscrollchanged(int x, int y, int oldx, int oldy) { super.onscrollchanged(x, y, oldx, oldy); this.newy=y; }
for have used following logic
public void translateview(view view){ int[] xy =new int[2]; //view = imageview view.getlocationonscreen(xy); int scrolly = xy[1]+view.getbottom(); //screenheight =screenheight of phone if(scrolly>screenheight){ //heading textview textview.getlocationonscreen(xy); int diff = screenheight-xy[1]; diff =math.abs(diff); int moveheight = (int) (scrollview.newy+diff); scrollview.smoothscrollto(0, moveheight); } }
so trying finding difference between y
position of textview
, subtracting screen height , scrolling scrollview
difference,so view shown on top.but not working.any idea on how achieve this.
i found doing wrong. each screen comprises of statusbar
, in case actionbar
.so when calculating view y
position,i calculating top of screen , whenever use increment value scrollview scroll
,i moving view top of screen.so actual solution be
public void translateview(final view view){ int[] xy =new int[2]; view.getlocationonscreen(xy); int scrolly = xy[1]+view.getbottom(); if(scrolly>dataholder.screenheight){ handle.getlocationonscreen(xy); int actualy=xy[1]-dataholder.actionbarheight-dataholder.statusbarheight; int moveheight = (int) (scrollview.newy+actualy); scrollview.smoothscrollto(0, moveheight); } }
so doing view automatically reaches top of screen
Comments
Post a Comment