gpt4 book ai didi

android - Recyclerview 滚动在嵌套 ScrollView 中的 fragment 中不起作用

转载 作者:行者123 更新时间:2023-11-29 02:24:48 24 4
gpt4 key购买 nike

首先:我之前使用过回收器 View 和滚动监听器,一切都很好,但这是第一次使用协调器布局并在嵌套 ScrollView 中使用 fragment ,所以我认为问题与设计有关

这是我对滚动代码的总结

  categoryRecyclerView?.addOnScrollListener(object : RecyclerView.OnScrollListener() {

override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)
Log.e("scrolling","scroll")
if (dy > 0) {
Log.e("scrolling","vertical")

所以当我滚动时,我没有在我的 logcat 中看到 ("scrolling","vertical"),但无论我是否滚动,我只看到一次 ("scrolling","scroll")

这是我的 Activity xml

<?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="@color/layoutBackground">

<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
</android.support.design.widget.AppBarLayout>

<android.support.v4.widget.NestedScrollView
android:id="@+id/nestedScroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
app:layout_behavior="@string/appbar_scrolling_view_behavior">

<FrameLayout
android:padding="8dp"
android:id="@+id/fragmentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</android.support.v4.widget.NestedScrollView>

<android.support.design.widget.BottomNavigationView
android:id="@+id/bottomNavigationView"
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_gravity="bottom"
android:background="@color/colorPrimary"
android:padding="8dp"
app:layout_behavior="@string/hide_bottom_view_on_scroll_behavior"
app:itemIconTint="@drawable/navigation_item_selector"
app:itemTextColor="@android:color/white"
app:menu="@menu/navigation_menu" />

</android.support.design.widget.CoordinatorLayout>

这是我的 fragment xml

<?xml version="1.0" encoding="utf-8"?>

<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/layoutBackground"
tools:context=".fragments.AllItemsFragment">

<android.support.v7.widget.RecyclerView
android:id="@+id/categoryRecyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:listitem="@layout/entertainment_recycler_view_item" />

<ProgressBar
android:id="@+id/loadingCategoryList"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

在我设置的fragment的onActivtyCreated

scrollView.isNestedScrollingEnabled = false

在初始化 recyclerview 代码中,我将 recyclerview 设置为

categoryRecyclerView?.isNestedScrollingEnabled = false
categoryRecyclerView?.setHasFixedSize(false);

最佳答案

设置

categoryRecyclerView?.isNestedScrollingEnabled = true

并且不使用 recyclerview.addOnScrollListener(),而是使用 nestedScrollView.setOnScrollChangeListener()。

    nestedScrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {

@Override
public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX,
int oldScrollY) {
LinearLayoutManager layoutManager = (LinearLayoutManager) questionRecyclerView.getLayoutManager();

if (layoutManager != null) {
int visibleItemCount = layoutManager.getChildCount();
int totalItemCount = layoutManager.getItemCount();
int firstVisibleItemPosition = layoutManager.findFirstVisibleItemPosition();

if (!questionAdapter.isLoading()) {
if (visibleItemCount +firstVisibleItemPosition >= totalItemCount
&& firstVisibleItemPosition >= 0
&& totalItemCount >= PAGE_SIZE) {

// loadMore();
}
}
}
}
}});

关于android - Recyclerview 滚动在嵌套 ScrollView 中的 fragment 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52875242/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com