android ScrollView scrollTo不起作用

bd1hkmkf  于 2023-09-28  发布在  Android
关注(0)|答案(5)|浏览(122)

ScrollView .scrollTo not working? Saving ScrollView position on rotation相同
我在onCreate中动态地向scrollView添加项目。我在添加所有项目后尝试以下操作:

// no effect
    ScrollView mainScroll = (ScrollView) findViewById(R.id.average_scroll_mainScroll);

    // no effect
    ScrollView mainScroll = (ScrollView) findViewById(R.id.average_scroll_mainScroll);
    mainScroll.post(new Runnable() {
        public void run(){
            ScrollView mainScroll = (ScrollView) findViewById(R.id.average_scroll_mainScroll);
            mainScroll.scrollTo(0, 0);
        } 
    });

    // works like a charm
    ScrollView mainScroll = (ScrollView) findViewById(R.id.average_scroll_mainScroll);
    mainScroll.postDelayed(new Runnable() {
        public void run(){
            ScrollView mainScroll = (ScrollView) findViewById(R.id.average_scroll_mainScroll);
            mainScroll.scrollTo(0, 0);
        } 
    }, 30);

我的结论是,有一些事件,如'DOM准备'?有没有回调?

jmp7cifd

jmp7cifd1#

您不需要扩展ScrollView并根据David Daudelinthis question提供的答案创建自己的ScrollView。
获取ScrollViewViewTreeObserver,并将OnGlobalLayoutListener添加到ViewTreeObserver。然后从OnGlobalLayoutListeneronGlobalLayout()方法调用ScrollView.scrollTo(x,y)方法。代码:

ScrollView mainScroll = (ScrollView) findViewById(R.id.average_scroll_mainScroll);

 ViewTreeObserver vto = scrollView.getViewTreeObserver();
 vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
      public void onGlobalLayout() {
           mainScroll.scrollTo(0, 0);
      }
 });
ca1c2owp

ca1c2owp2#

这对我很有效,将scrollView.scrollTo()替换为

scrollView.post(new Runnable() {
        @Override
        public void run() {
            scrollView.scrollTo(0, ScrollViewYPosition);//0 is x position
        }
    });

瓦西里·卡布诺夫的回答帮助了我。
ScrollView .scrollTo not working? Saving ScrollView position on rotation

ubbxdtey

ubbxdtey3#

当您对ScrollView进行某些更改时,它需要一段时间才能将布局更改复制到显示列表,并通知ScrollView它确实可以滚动到某个位置。
我已经设法让它工作,用我自己的ScrollView扩展ScrollView,并添加一个方法,根据需要添加OnGlobalLayoutListener(如MH所建议的)并在稍后滚动。这比将其应用于您需要的每个案例更加自动化(但是您需要使用新的ScrollView)。这是一个简单的例子。

public class ZScrollView extends ScrollView {

    // Properties
    private int desiredScrollX = -1;
    private int desiredScrollY = -1;
    private OnGlobalLayoutListener gol;

    // ================================================================================================================
    // CONSTRUCTOR ----------------------------------------------------------------------------------------------------

    public ZScrollView(Context __context) {
        super(__context);
    }

    public ZScrollView(Context __context, AttributeSet __attrs) {
        super(__context, __attrs);
    }

    public ZScrollView(Context __context, AttributeSet __attrs, int __defStyle) {
        super(__context, __attrs, __defStyle);
    }

    // ================================================================================================================
    // PUBLIC INTERFACE -----------------------------------------------------------------------------------------------

    public void scrollToWithGuarantees(int __x, int __y) {
        // REALLY Scrolls to a position
        // When adding items to a scrollView, you can't immediately scroll to it - it takes a while
        // for the new addition to cycle back and update the scrollView's max scroll... so we have
        // to wait and re-set as necessary

        scrollTo(__x, __y);

        desiredScrollX = -1;
        desiredScrollY = -1;

        if (getScrollX() != __x || getScrollY() != __y) {
            // Didn't scroll properly: will create an event to try scrolling again later

            if (getScrollX() != __x) desiredScrollX = __x;
            if (getScrollY() != __y) desiredScrollY = __y;

            if (gol == null) {
                gol = new OnGlobalLayoutListener() {
                    @Override
                    public void onGlobalLayout() {
                        int nx = desiredScrollX == -1 ? getScrollX() : desiredScrollX;
                        int ny = desiredScrollY == -1 ? getScrollY() : desiredScrollY;
                        desiredScrollX = -1;
                        desiredScrollY = -1;
                        scrollTo(nx, ny);
                    }
                };

                getViewTreeObserver().addOnGlobalLayoutListener(gol);
            }
        }
    }
}

对我来说非常有用,因为我想在添加后立即滚动到ScrollView中的给定View。

ljsrvy3e

ljsrvy3e4#

试试这个它的工作原理:(Kotlin)

myScrollView.post(Runnable {
                 myScrollView.scrollTo(0, scrollValue)
             })
lyr7nygr

lyr7nygr5#

下面是滚动recycleView的示例:

private int scrollPosition = 0;
@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.home_activity);
    ...
    // set scrollPosition value
    recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            scrollPosition += dy;
        }
    });
    ...
}      

@Override
protected void onResume() {
    super.onResume();
    ...
    // scrollToPosition recycleView
    recyclerView.post(() -> {
        recyclerView.scrollToPosition(scrollPosition);//0 is x position
    });
}

// Save the scroll position when the activity is paused (e.g., when navigating away)
@Override
protected void onPause() {
    super.onPause();
    scrollPosition = getCurrentScrollPosition();
}

private int getCurrentScrollPosition() {
    LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
    if (layoutManager != null) {
        return layoutManager.findLastVisibleItemPosition();
    }
    return 0;
}

相关问题