¿Hay alguna manera de desplazarse programáticamente a la parte superior de un NestedScrollViewactivando también los eventos de desplazamiento para el padre? smoothScrollTo(x, y)no delega los eventos de desplazamiento al NestedScrollingParent.
android
android-layout
android-nestedscrollview
lukas1994
fuente
fuente

Respuestas:
NestedScrollView.scrollTo(0, 0);fuente
NestedScrollView.scrollTo(0, 0);para desplazarse hasta la parte superior del archivoNestedScrollView. De nada.Logré hacerlo (desplazamiento animado):
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); }donde
10000es la velocidad en la dirección y.fuente
behavior.onNestedFling(coordinator, appbar, null, 0, -params.height, true);para revertirlobehavior.onNestedFling(coordinator, appbar, null, 0, params.height, true);Otra forma de suavizar el desplazamiento
NestedScrollViewhacia arriba o hacia abajo es usando lafullScroll(direct)funciónnestedScrollView.fullScroll(View.FOCUS_DOWN); nestedScrollView.fullScroll(View.FOCUS_UP);fuente
NestedScrollViewdebajo de la barra de aplicaciones?Nada funcionó para mí, y realmente no sé por qué funcionó, pero aquí está mi solución y problema.
Al agregar la vista de reciclador a una vista de desplazamiento anidada, mostraba la vista de reciclador en la pantalla al llegar a esa actividad. Para desplazarme hacia arriba, tuve que usar esto:
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);fuente
scrollView.getParent().requestChildFocus(scrollView, scrollView);También me enfrenté a un escenario similar. En mi caso, cuando me desplazo hacia abajo para terminar, debería aparecer el botón FAB y cuando el usuario toque ese botón FAB debería ir a la parte superior de la página. Para eso, agregué la respuesta de @SilentKnight
NestedScrollView.scrollTo(0, 0);Para ir a la parte superior, pero que no es suficiente para una animación suave para desplazarse hacia arriba.Para una animación fluida, he usado la respuesta de @Sharj, que es
NestedScrollView.fullScroll(View.FOCUS_UP);Pero luego mi AppBar no es visible, por lo tanto, tengo que expandir la AppBar de la siguiente maneraappBarLayout1.setExpanded(true). Entonces, usando estos tres puedo ir sin problemas a la parte superior de la página.nestedScrollView.fullScroll(View.FOCUS_UP); nestedScrollView.scrollTo(0,0); appBarLayout1.setExpanded(true);fuente
Fácilmente podría simular el desplazamiento en el nivel NestedScrollView. Básicamente, le dice al coordinatorLayout que acaba de desplazarse por la altura de la barra de herramientas.
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());fuente
Puede usar esto para un desplazamiento suave.
nestedScrollView.fullScroll(View.FOCUS_UP); nestedScrollView.smoothScrollTo(0,0);O para desplazamiento normal
nestedScrollView.fullScroll(View.FOCUS_UP); nestedScrollView.scrollTo(0,0);fuente
Probé todas las respuestas anteriores, nada parecía funcionar, pero solo necesitaba un postDelayed.
scrollview.postDelayed(new Runnable() { @Override public void run() { listener.setAppBarExpanded(false, true); //appbar.setExpanded(expanded, animated); scrollview.fullScroll(View.FOCUS_DOWN); } }, 400);fuente
Algún tiempo
NestedScrollView.scrollTo(0, 0);yscrollview.pageScroll(View.FOCUS_UP);no funcionaEn eso necesitas usar
fling()ysmoothScrollTo()juntosCÓDIGO DE MUESTRA
nestedScrollView.post { nestedScrollView.fling(0) nestedScrollView.smoothScrollTo(0, 0) }fuente
Agregue la línea dos veces. Funcionó para mí
nestedScrollView.fullScroll(View.FOCUS_UP); nestedScrollView.fullScroll(View.FOCUS_UP);fuente
Use View.scollTo (int x, int y) en su lugar para desplazarse hasta la parte superior.
findViewById({your NestedScrollView resource id}).scrollTo(0, 0);fuente
Tuve un problema con NestedScrollView cuando lo usé que con RecyclerView. Este código funcionó para mí: 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);fuente
Así es como me desplazo a un elemento de RecyclerView cuando RecyclerView está dentro de NestedScrollView
Primero, obtenga y
item heightluego desplácese hasta elpositionque desee.val itemHeight = binding.calendarRecyclerView.findViewHolderForAdapterPosition(0)!!.itemView.height binding.nestedScrollView2.smoothScrollTo(0, position * itemHeight)fuente