安卓一单独TextView无法滚,需置ScrollView中。ScrollView一系列函数中fullScroll实现滚动。
TextView执行append后立调fullScroll无法滚至真正底部是因Android大多函数基于消息,通消息队列同步,故大多函数异步。TextView调append后不等显示并添至消息队列后立返,调fullScroll时没显致无法滚至正确位。
ScrollView.FOCUS_UP; // 顶
ScrollView.FOCUS_DOWN; // 底
ScrollView.FOCUS_LEFT; // 左
ScrollView.FOCUS_RIGHT; // 右
ScrollViewScroll
package util;
import android.os.Handler;
import android.view.View;
import android.widget.ScrollView;
/**
* Created on 2018/7/4.
*
* @desc ScrollView滚动
*/
public class ScrollViewScroll {
/**
* 滚至底部
* 无动效
*
* @param scrollView scrollView
* @param scrollInnerView 内嵌布局
*/
public static void scrollToBottom(final View scrollView, final View scrollInnerView) {
Handler mHandler = new Handler();
mHandler.post(new Runnable() {
@Override
public void run() {
if (scrollView == null || scrollInnerView == null) {
return;
}
int offset = scrollInnerView.getMeasuredHeight() - scrollView.getHeight();
if (offset < 0) {
offset = 0;
}
scrollView.scrollTo(0, offset);
}
});
}
/**
* 滚动
* 需延时待布局动设生效(否无效)
* 有动效
*
* @param scrollView scrollView
* @param direction 方向
* @param delay 延时
*/
public static void scrollTo(final ScrollView scrollView, final int direction, int delay) {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
scrollView.fullScroll(direction);
}
}, delay);
}
}
主代码
ScrollViewScroll.scrollToBottom(svDataEntryFinishingAgent, llDataEntryFinishingAgentContainer);
或
ScrollViewScroll.scrollTo(svDataEntryFinishingAgent, ScrollView.FOCUS_DOWN, 100);