ListView和ScrollView滑动到顶部

这是产品那边刚刚提出的需求,

实现起来也是挺简单的,分享下给大家。

ListView实现是:

  mList.requestFocusFromTouch();
    mList.smoothScrollToPositionFromTop(0,0);

  本来是直接

 mList.setSelection(0);

  但是这个由于ListView是放在viewpager+fragment中。而且listview又加了headview,导致会有一些细节不符合产品的要求。

然后就用上面的实现了。

ScrollView滑动到顶部

  rootView.post(new Runnable() {
                @Override
                public void run() {
                    //To change body of implemented methods use File | Settings | File Templates.
//                    mRootScrollView.fullScroll(ScrollView.FOCUS_DOWN);
                    int[] location = new int[2];
                    mAdvertise.getLocationOnScreen(location);
                    int offset = location[1] - rootView.getMeasuredHeight();
                    if (offset < 0) {
                        offset = 0;
                    }
                    rootView.smoothScrollTo(0, offset);
                }
            });

rootview就是scrollview.

原文地址:https://www.cnblogs.com/itpepe/p/4959039.html