java - Android ScrollView only scroll to bottom if you're already there -
i have basic android app set scroll view , text added dynamically. want scroll bottom when text added (which happens) want scroll bottom if @ bottom, if you're reading doesn't scroll. here's have far.
private void addtext(final string msg){ this.runonuithread(new runnable() { public void run() { textview log = (textview) findviewbyid(r.id.chatlog); if(log.gettext().equals("loading...")){ log.settext(msg); }else{ scrollview scroller = (scrollview) findviewbyid(r.id.scroll_container); //set current scroll position int scroll_pos = scroller.getscrolly(); //scroll bottom scroller.fullscroll(scrollview.focus_down); //set bottom position int scroll_bot = scroller.getscrolly(); //add text log.append("\r\n" + msg); //if weren't @ bottom //scroll were. //this isn't working, scroll bot same //as scroll pos. if(scroll_pos != scroll_bot){ scroller.scrollto(0, scroll_pos); } //system.out.println("pos: " + scroll_pos); //system.out.println("bot: " + scroll_bot); } } }); }
the best solution found far use scrollview.post() method runnable invoked after text change:
final scrollview scrollview = (scrollview) findviewbyid(r.id.consoletab); textview textview = (textview) findviewbyid(r.id.consoleview); boolean autoscroll = (textview.getbottom() - (scrollview.getheight() + scrollview.getscrolly())) <= 0; textview.settext(state.getconsole().gettext()); if (autoscroll) { scrollview.post(new runnable() { public void run() { scrollview.fullscroll(scrollview.focus_down); } }); }
Comments
Post a Comment