Osc*_*dez 13 android android-coordinatorlayout bottom-sheet android-viewpager2
我遇到了麻烦,我认为是我CoordinatorLayout的错,但不确定。我ViewPager2在 a 内部使用ConstraintLayout,我使用CoordinatorLayoutlike BottomSheet。但是当我拖动以隐藏它时效果不佳。我替换了ViewPager2byViewPager并且效果很好。我希望你能帮助我。
这是我的 XML 文件:
<androidx.coordinatorlayout.widget.CoordinatorLayout 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">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="@string/bottom_sheet_behavior"
app:behavior_hideable="false"
app:behavior_peekHeight="60dp" >
<androidx.viewpager2.widget.ViewPager2
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintDimensionRatio="4:3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:background="#0000FF"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)
这可以通过禁用 ; 的嵌套滚动来解决ViewPager2 RecyclerView。但由于RecyclerView无法通过图书馆直接访问ViewPager2。那么在布局上就无法实现。
因此,我们可以RecyclerView使用 java 反射来获取它,如下所示:
爪哇:
public static RecyclerView getRecyclerView(ViewPager2 viewPager) {
try {
Field field = ViewPager2.class.getDeclaredField("mRecyclerView");
field.setAccessible(true);
return (RecyclerView) field.get(viewPager);
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return null;
}
Run Code Online (Sandbox Code Playgroud)
科特林:
fun ViewPager2.getRecyclerView(): RecyclerView? {
try {
val field = ViewPager2::class.java.getDeclaredField("mRecyclerView")
field.isAccessible = true
return field.get(this) as RecyclerView
} catch (e: NoSuchFieldException) {
e.printStackTrace()
} catch (e: IllegalAccessException) {
e.printStackTrace()
}
return null
}
Run Code Online (Sandbox Code Playgroud)
然后禁用嵌套滚动:
爪哇:
RecyclerView recyclerView = getRecyclerView(viewPager);
if (recyclerView != null) {
recyclerView.setNestedScrollingEnabled(false);
recyclerView.setOverScrollMode(View.OVER_SCROLL_NEVER); // Optional
}
Run Code Online (Sandbox Code Playgroud)
科特林:
val recyclerView = viewPager.getRecyclerView()
recyclerView?.isNestedScrollingEnabled = false
recyclerView?.overScrollMode = View.OVER_SCROLL_NEVER // Optional
Run Code Online (Sandbox Code Playgroud)
这适用于我,无需设置OverScrollMode,因此,如果它不起作用,您可以禁用OverScrollMode上面的功能。
| 归档时间: |
|
| 查看次数: |
541 次 |
| 最近记录: |