带BottomSheetBehavior的NestedScrollView可点击元素嵌套滚动异常
问题描述
使用带有BottomSheetBehavior的NestedScrollView,内部嵌套ViewPager2。ViewPager2加载的Fragment包含可点击和不可点击元素,不可点击元素的嵌套滚动表现正常,但点击可点击元素时,无法将BottomSheet推至屏幕顶部,BottomSheet停留在下方位置,同时ViewPager2出现无效滚动。
最小复现代码
Activity代码
class NSCClickActivity : AppCompatActivity() { private var _fragmentAdapter: TestFragmentAdapter? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.nscclick_layout) _fragmentAdapter = TestFragmentAdapter(this) findViewById<ViewPager2>(R.id.challenge_list_VP).adapter = _fragmentAdapter TabLayoutMediator(findViewById(R.id.challenge_action_tabLayout), findViewById(R.id.challenge_list_VP)) { tab, position -> val tabNames = listOf("ONE", "TWO") tab.text = tabNames[position] }.attach() } }
Fragment适配器代码
class TestFragmentAdapter(fragment: FragmentActivity) : FragmentStateAdapter(fragment) { override fun getItemCount(): Int = 2 override fun createFragment(position: Int): Fragment = VPFragment() }
ViewPager加载的Fragment代码
class VPFragment : Fragment(R.layout.vpfragment_layout) { }
主布局文件(nscclick_layout.xml)
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout android:id="@+id/challenge_main_fragment_layout" 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"> <ImageView android:id="@+id/title_background" android:layout_width="0dp" android:layout_height="0dp" android:background="?attr/colorPrimary" app:layout_constraintStart_toStartOf="parent" android:contentDescription="@null" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintTop_toTopOf="parent"/> <androidx.coordinatorlayout.widget.CoordinatorLayout android:id="@+id/challenge_main_CL" android:layout_width="0dp" android:layout_height="0dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"> <androidx.core.widget.NestedScrollView android:id="@+id/scroll_SV" android:layout_width="match_parent" android:layout_height="match_parent" android:clipToPadding="false" android:fillViewport="true" android:orientation="vertical" android:overScrollMode="never" android:scrollbars="none" app:behavior_hideable="false" app:layout_behavior="@string/bottom_sheet_behavior"> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/white" android:orientation="vertical"> <TextView android:id="@+id/title_TV" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginEnd="10.dp" android:layout_marginStart="10.dp" android:layout_marginTop="24.dp" android:text="TITRE" android:textAppearance="?attr/textAppearanceHeadline1" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <androidx.viewpager2.widget.ViewPager2 android:id="@+id/challenge_list_VP" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="top" android:descendantFocusability="blocksDescendants" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/challenge_action_tabLayout" android:nestedScrollingEnabled="true"/> <com.google.android.material.tabs.TabLayout android:id="@+id/challenge_action_tabLayout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="24.dp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/title_TV" app:tabGravity="start" app:tabIndicatorColor="?attr/colorPrimary" app:tabMode="fixed" app:tabSelectedTextColor="?attr/colorPrimary" app:tabTextAppearance="?attr/textAppearanceHeadline4"/> </androidx.constraintlayout.widget.ConstraintLayout> </androidx.core.widget.NestedScrollView> </androidx.coordinatorlayout.widget.CoordinatorLayout> </androidx.constraintlayout.widget.ConstraintLayout>
Fragment布局文件(vpfragment_layout.xml)
<?xml version="1.0" encoding="utf-8"?> <androidx.core.widget.NestedScrollView 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:clipToPadding="false" android:fillViewport="true" android:orientation="vertical" android:overScrollMode="never" android:scrollbars="none" app:behavior_hideable="false" app:layout_behavior="@string/bottom_sheet_behavior"> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="1500dp" xmlns:app="http://schemas.android.com/apk/res-auto" android:nestedScrollingEnabled="true" android:weightSum="2" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_constraintTop_toTopOf="parent" android:text="a\n\n\n\n\n\na" android:id="@+id/text"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_constraintTop_toBottomOf="@id/text" android:text="b\n\n\n\n\n\nb" android:id="@+id/text2" android:clickable="true"/> <SurfaceView android:layout_width="match_parent" android:layout_height="15000dp" app:layout_constraintTop_toBottomOf="@id/text2"/> </androidx.constraintlayout.widget.ConstraintLayout> </androidx.core.widget.NestedScrollView>
内容的提问来源于stack exchange,提问作者AdrienM
相关产品推荐
相关产品推荐

