android - 如何检测 NestedScrollView 何时到达底部?
问题描述
描述:
我在我的一个项目中使用这个监听器,以检查用户何时到达 NestedScroll 的底部并且它工作正常。
问题:
我正在尝试在另一个项目中使用相同的侦听器,但它无法正常工作
调试:
调试过程说明:
一旦我打开我的活动,第一个断点就会被激活,但方法的主体没有响应。一旦我按下 f9(恢复程序),调试过程就会停止并且应用程序开始运行并且回收器被我的 API 填充
我想要什么?
我需要这个监听器来检查用户何时滑到底部(当他们到达最后一个帖子时)以显示更多项目(回收站分页),但我不能,因为监听器没有响应(检查绿色矩形上图)
xml代码:
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/coordinatorLayout"
xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.core.widget.NestedScrollView
android:id="@+id/nested"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="@string/app_name">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="@string/app_name">
<com.google.android.gms.ads.AdView
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="@+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/twentydp"
ads:layout_constraintLeft_toLeftOf="parent"
ads:layout_constraintTop_toTopOf="parent"
ads:adSize="BANNER"
ads:adUnitId="ca-app-pub-3940256099942544/6300978111">
</com.google.android.gms.ads.AdView>
<TextView
android:id="@+id/txt_resultados_lore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/tendp"
android:layout_marginTop="@dimen/twentydp"
android:contentDescription="@string/app_name"
android:fontFamily="@font/nunito"
android:text="@string/resultados_lore"
android:textColor="@color/dark_gray"
android:textSize="@dimen/thirteensp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@+id/adView"/>
<View
android:id="@+id/view_barra1"
android:layout_width="match_parent"
android:layout_height="@dimen/twodp"
android:layout_marginTop="@dimen/tendp"
android:layout_marginLeft="@dimen/tendp"
android:layout_marginRight="@dimen/tendp"
android:background="@color/light_gray2"
app:layout_constraintTop_toBottomOf="@+id/txt_resultados_lore"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:contentDescription="@string/app_name"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_animales_filtrados"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="@dimen/fivedp"
tools:listitem="@layout/item_row_recycler_filtered_animals"
android:contentDescription="@string/app_name"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@+id/view_barra1" />
<ProgressBar
android:id="@+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:contentDescription="@string/app_name"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
监听器java代码:
binding.nested.setOnScrollChangeListener((NestedScrollView.OnScrollChangeListener) (v, scrollX, scrollY, oldScrollX, oldScrollY) -> {
p.showToast(getActivity(),"test-text");
if(scrollY == v.getChildAt(0).getMeasuredHeight() - v.getMeasuredHeight()){
page++;
binding.progressBar.setVisibility(View.VISIBLE);
}
});
正如我上面提到的......当我滑动时从未显示吐司,因为听众没有对这个项目做出响应,但它对另一个项目的响应是 100% 好的。
非常感谢您的阅读!
编辑: 我也尝试过使用回收器侦听器并且它可以工作(侦听器),但是检查用户是否到达底部的 if 不起作用(也许我有 xml 设计问题?idk :( )
binding.recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if (!recyclerView.canScrollVertically(1)) {
//Activated everytime i slide (and not when i reach the bottom)
}
//Another way to check if i reach the bottom
if (!recyclerView.canScrollVertically(1) && newState==RecyclerView.SCROLL_STATE_IDLE) {
//Activated everytime i slide (and not when i reach the bottom)
}
//Another way to check if i reach the bottom
int visibleItemCount = mLayoutManager.getChildCount();
int totalItemCount = mLayoutManager.getItemCount();
int pastVisibleItems = mLayoutManager.findFirstVisibleItemPosition();
if (pastVisibleItems + visibleItemCount >= totalItemCount) {
//Activated everytime i slide (and not when i reach the bottom)
}
}
});
解决方案
推荐阅读
- django - 如何基于 FK 进行查询?
- javascript - 当模式关闭时,如何停止播放嵌入的 iframe youtube 视频?
- arduino - 我的两个 nRF24 发生了一些奇怪的事情
- sql - 仅显示循环中的最高 alpha
- puppet - 木偶嵌套资源create_resources,无法将字符串转换为哈希
- arrays - AngularJS foreach 无法在同一服务中找到功能
- javascript - React Native - 按 ID 更新和删除
- python - 如何将数据转换为正确的 numpy 维度格式?
- java - 在我的 JSP 页面中找不到图像源。在浏览器控制台中找不到 404 页面
- html - 如何使用 ui-sref 在 laravel 中的行内获取 html 表格行?