Development Tip

프로그래밍 방식으로 NestedScrollView의 맨 위로 스크롤

yourdevel 2020. 11. 12. 20:26
반응형

프로그래밍 방식으로 NestedScrollView의 맨 위로 스크롤


NestedScrollView부모에 대한 스크롤 이벤트를 트리거하여 프로그래밍 방식으로 맨 위로 스크롤하는 방법이 있습니까? smoothScrollTo(x, y)스크롤 이벤트를 NestedScrollingParent.


NestedScrollView.scrollTo(0, 0);

나는 그것을 할 수 있었다 (애니메이션 스크롤) :

CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) appBarLayout.getLayoutParams();
AppBarLayout.Behavior behavior = (AppBarLayout.Behavior) params.getBehavior();
if (behavior != null) {
    behavior.onNestedFling(coordinatorLayout, appBarLayout, null, 0, 10000, true);
}

10000y 방향의 속도는 어디에 있습니까 ?


NestedScrollView위로 또는 아래로 완전히 부드럽게 스크롤 하는 또 다른 방법 fullScroll(direct)기능을 사용하는 것입니다.

nestedScrollView.fullScroll(View.FOCUS_DOWN);
nestedScrollView.fullScroll(View.FOCUS_UP);

나를 위해 일한 것이 없었고 이것이 왜 효과가 있었는지 정말로 모르겠지만 여기에 내 해결책과 문제가 있습니다.

중첩 된 스크롤보기에 리사이클 러보기를 추가 할 때 해당 활동에 도달 할 때 화면에 리사이클 러보기가 표시되었습니다. 맨 위로 스크롤하려면 다음을 사용해야합니다.

RecyclerView recyclerView = (RecyclerView) findViewById(R.id.details_recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setItemAnimator(new DefaultItemAnimator());
ProductDescriptionAdapter adapter = new ProductDescriptionAdapter(this);
adapter.setData(mProduct.getDetails());
recyclerView.setAdapter(adapter);
NestedScrollView scrollView = (NestedScrollView) findViewById(R.id.scroll);
scrollView.getParent().requestChildFocus(scrollView, scrollView);

저도 비슷한 시나리오에 직면했습니다. 내 경우 아래로 스크롤하면 FAB 버튼이 나타나고 사용자가 해당 FAB 버튼을 탭하면 페이지 상단으로 이동해야합니다. 이를 위해 @SilentKnight 답변 NestedScrollView.scrollTo(0, 0);For go to the top을 추가했지만 스크롤 업을위한 부드러운 애니메이션에는 충분하지 않습니다.
부드러운 애니메이션을 위해 @Sharj 대답을 사용했지만 NestedScrollView.fullScroll(View.FOCUS_UP);내 AppBar가 보이지 않으므로 다음과 같이 AppBar를 확장해야합니다 appBarLayout1.setExpanded(true). 따라서이 세 가지를 사용하면 페이지 상단으로 원활하게 이동할 수 있습니다.

nestedScrollView.fullScroll(View.FOCUS_UP);
nestedScrollView.scrollTo(0,0);
appBarLayout1.setExpanded(true);

NestedScrollView 수준에서 스크롤을 쉽게 가짜로 만들 수 있습니다. 기본적으로 coordinatorLayout에 툴바의 높이만큼 스크롤했다고 알려줍니다.

    NestedScrollView nestedScrollView = (NestedScrollView) getActivity().findViewById(R.id.your_nested_scroll_view);

    int toolbarHeight = getActivity().findViewById(R.id.toolbar).getHeight();

    nestedScrollView.startNestedScroll(ViewCompat.SCROLL_AXIS_VERTICAL);
    nestedScrollView.dispatchNestedPreScroll(0, toolbarHeight, null, null);
    nestedScrollView.dispatchNestedScroll(0, 0, 0, 0, new int[]{0, -toolbarHeight});
    nestedScrollView.scrollTo(0, nestedScrollView.getBottom());

부드러운 스크롤을 위해 이것을 사용할 수 있습니다.

nestedScrollView.fullScroll(View.FOCUS_UP);
nestedScrollView.smoothScrollTo(0,0);

또는 일반 스크롤의 경우

nestedScrollView.fullScroll(View.FOCUS_UP);
nestedScrollView.scrollTo(0,0);

위의 모든 응답을 시도했지만 아무것도 작동하지 않는 것 같았지만 postDelayed 만 필요했습니다.

    scrollview.postDelayed(new Runnable() {
        @Override
        public void run() {
            listener.setAppBarExpanded(false, true); //appbar.setExpanded(expanded, animated);
            scrollview.fullScroll(View.FOCUS_DOWN);
        }
    }, 400);

Add the line two times .worked for me

nestedScrollView.fullScroll(View.FOCUS_UP);
nestedScrollView.fullScroll(View.FOCUS_UP);

Use the View.scollTo(int x, int y) instead to scroll to the top.

findViewById({your NestedScrollView resource id}).scrollTo(0, 0);

I had problem with NestedScrollView when I used than with RecyclerView . This code worked for me: nestedScrollView!!.getParent().requestChildFocus(nestedScrollView, nestedScrollView);

    val recyclerViewSearch = activity?.findViewById<RecyclerView>(R.id.recycler)
    recyclerViewSearch.setAdapter(setAdapterSearch())

    val nestedScrollView = activity?.findViewById<NestedScrollView>(R.id.bottom_sheet_Search)
    nestedScrollView!!.getParent().requestChildFocus(nestedScrollView, nestedScrollView);

참고URL : https://stackoverflow.com/questions/31014409/programmatically-scroll-to-the-top-of-a-nestedscrollview

반응형