Recyclerview onscrolllistener no funciona cuando setNestedScrollingEnabled a false
Quiero implementar la paginación con recyclerView
, para esto agrego addOnScrollListener
a la recyclerView
pero estoy teniendo problemas con RecyclerView.OnScrollListener
no funciona, cuando establezco rvGridExplore.setNestedScrollingEnabled(false);
Pero cuando quito rvGridExplore.setNestedScrollingEnabled(false);
Entonces su trabajo bien, no sé cómo manejar esto
Aquí está el código:
rvGridExplore = (RecyclerView) view.findViewById(R.id.rvGridExplore); final GridLayoutManager glm = new GridLayoutManager(context,2); // rvGridExplore.setNestedScrollingEnabled(false); rvGridExplore.setLayoutManager(glm); // final int visibleItemCount,totalCount,pastVisibleItems; rvGridExplore.addOnScrollListener(new RecyclerView.OnScrollListener() { @Override public void onScrollStateChanged(RecyclerView recyclerView, int newState) { super.onScrollStateChanged(recyclerView, newState); Log.v("scrollll","state changed"); } @Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); if (dy > 0) { int totalCount = glm.getItemCount(); int visibleItemCount = glm.getChildCount(); int pastVisibleItems = glm.findFirstVisibleItemPosition(); if (loading) { if ((visibleItemCount + pastVisibleItems) >= totalCount) { Log.v("scroll","scrolled"+pastVisibleItems); } } } } });
Esta pregunta puede ser vieja, pero para ayudar a otros que tropezaron con este problema, me gustaría compartir lo que hice. Tuve que implementar onScroll Listener a recyclerview
para cargar datos desde el servidor y hacer algunos cambios en la interfaz de usuario. Y también necesitaba swipeRefresh Layout para actualizar datos.
Esta era mi estructura de archivos xml,
-RelativeLayout -SwipeRefreshLayout -NestedScrollView -LinearLayout(Vertical) -Multiple views required
Después de esto, para detectar el desplazamiento hacia arriba y hacia abajo i implementó setOnScrollListener a la NestedScrollView.
El uso normal de SwipeRefreshLayout para actualizar los datos.
Y para cargar más datos implementé la lógica dentro de onScrollListener de NestedScrollingView.
if (scrollY == (v.getChildAt(0).getMeasuredHeight() - v.getMeasuredHeight())) { // Load More Data }
Paso 1: Crear EndlessRecyclerOnScrollListener
public abstract class EndlessRecyclerOnScrollListener extends RecyclerView.OnScrollListener { public static String TAG = EndlessRecyclerOnScrollListener.class.getSimpleName(); // use your LayoutManager instead private LinearLayoutManager llm; public EndlessRecyclerOnScrollListener(LinearLayoutManager sglm) { this.llm = llm; } @Override public void onScrolled(RecyclerView recyclerView, int dx, int dy) { super.onScrolled(recyclerView, dx, dy); if (!recyclerView.canScrollVertically(1)) { onScrolledToEnd(); } } public abstract void onScrolledToEnd(); }
Paso 2: Aplique el detector de desplazamiento a la vista del reciclador.
recyclerview.addOnScrollListener(new EndlessRecyclerOnScrollListener(mLayoutManager) { @Override public void onScrolledToEnd() { Log.e("Position", "Last item reached"); if (loadMore == true) { // put your Load more code // add 10 by 10 to tempList then notify changing in data } } });