一、ScrollView滚动到顶部或者底部:

ScrollView scrollView = (ScrollView) findViewById(R.id.scroll_view);scrollView.post(new Runnable(){    @Override    public void run(){        //滚动到底部        scrollview.fullScroll(ScrollView.FOCUS_DOWN);         //滚动到顶部        //scrollview.fullScroll(ScrollView.FOCUS_UP);     }});

二、ScrollView滚动到指定位置:

LinearLayout llNeedToSkip = (LinearLayout) findViewById(R.id.ll_need_to_skip);ScrollView scrollView = (ScrollView) findViewById(R.id.scroll_view);scrollView.post(new Runnable(){    @Override    public void run(){        //滚动到指定位置(滚动要跳过的控件的高度的距离)        scrollView.scrollTo(0, llNeedToSkip.getMeasuredHeight());        //如果要平滑滚动,可以这样写        //scrollView.smoothScrollTo(0, llNeedToSkip.getMeasuredHeight());    }});

    因为ScrollView是一个“重量级”控件,类似于ListView、RecyclerView等,scrollXX()等方法是基于消息队列来同步的,所以在onCreate()方法中调用未必会马上就执行,可能在队列中等待处理,因此需要用post()方法。

    我遇到的现实情况是ScrollView中嵌套了多个RecyclerView,页面比较复杂,即使采用post()方法也不能达到效果,这时候我能想到的办法就是延时:

LinearLayout llNeedToSkip = (LinearLayout) findViewById(R.id.ll_need_to_skip);ScrollView scrollView = (ScrollView) findViewById(R.id.scroll_view);scrollView.postDelay(new Runnable(){    @Override    public void run(){        scrollView.smoothScrollTo(0, llNeedToSkip.getMeasuredHeight());    }}, 1000);

    如果还是不行的话,就增加延时时间看看。

更多相关文章

  1. Android中AutoCompleteTextView控件的使用
  2. android按行读取文件内容的几个方法
  3. android获取bitmap的方法
  4. android webview goback 跳过页面302自动跳转方法
  5. Android NestedScrollView 嵌套ViewPager不显示和出现空白部分
  6. Android数据库中查找一条数据 query方法详解
  7. android 彻底退出程序方法
  8. Android实现滑动加载数据的方法
  9. 使用命令行ls命令的Android文件浏览控件,适合于root过的设备,可以

随机推荐

  1. go语言删除文件的方法
  2. go语言开源么
  3. mac下怎么安装go语言
  4. go语言 类型转换表示什么
  5. yum怎么安装go语言
  6. go语言是动态语言么
  7. 详解golang string和[]byte的对比
  8. go属于动态语言么
  9. 关于go使用mysql测试
  10. go是强类型语言么