我很高兴谷歌发布了基于 RecyclerView 构建的ViewPager2,解决了旧 ViewPager 存在的很多问题。
我很快将旧的 ViewPager 代码替换为 ViewPager2:
用 xml 中的 ViewPager2 替换 ViewPager
替换FragmentPagerAdapter(FragmentManager)为FragmentStateAdapter(Fragment)
ViewPager 设置如下:
viewPager.adapter = fragmentAdapter
val mediator = TabLayoutMediator(tabLayout, viewPager, true) { tab, position ->
tab.text = fragmentAdapter.tabNames[position]
}
mediator.attach()
Run Code Online (Sandbox Code Playgroud)没有进行其他更改。
执行上述更改后,我意识到一个问题——
现在我的 ViewPager 是一个普通的水平寻呼机,我的每个片段fragmentAdapter都有一个垂直的RecyclerView.
我观察到,当 RecyclerView 的滚动位置为 0 时,该 RecyclerView 中的项目无法接收任何单击或长按事件,但可以滚动。一旦滚动,它可以再次收到点击。*
知道这ViewPager2也是一个RecyclerView,是否与嵌套的 RecyclerView 有关系?
android android-viewpager android-recyclerview android-touch-event android-viewpager2
我正在使用 Horizontal ViewPager2,里面有 4 个片段。每个片段在 RecyclerView 上都有 SwipeRefreshLayout。我面临的问题是,当 RecyclerView 处于顶部位置时,任何项目上的 onClick 列表都不起作用。如果我向下滚动 RecyclerView,则 onClick 工作正常。当 RecyclerView 处于顶部位置时,如果 SwipeRefreshLayout 处于刷新状态,则 onClick 工作正常。似乎 SwipeRefreshLayout 在触摸事件中与 ViewPager2 发生冲突。它适用于简单的 ViewPager。实际问题可能是什么,我们如何处理?任何想法都是可观的。
谢谢
xml:
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/fragment_home"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<include
android:id="@+id/headerPanel"
layout="@layout/home_header"
app:viewModel="@{viewModel}" />
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/swipe_refresh_layout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:paddingStart="@dimen/home_post_side_spacing"
android:paddingEnd="@dimen/home_post_side_spacing"
app:colorScheme="@{@color/fayvo_color}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@+id/headerPanel"
app:onRefreshListener="@{() -> viewModel.manualRefresh()}"
app:refreshing="@{viewModel.isRefreshing}"
android:background="#f0f0f0">
<RelativeLayout
android:id="@+id/rl_rv"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_homePosts"
adapter="@{viewModel.postObservableArrayList}"
addToTop="@{viewModel.addToTop}"
clearList="@{viewModel.clearList}"
showLoader="@{viewModel.isPagingEnabled && viewModel.isLoading}"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />
<FrameLayout
android:id="@+id/emptyFrameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="@{viewModel.showEmptyLayout?View.VISIBLE:View.GONE}" />
<include
android:id="@+id/postUploadProgressLayout"
layout="@layout/item_post_upload_progress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:viewModel="@{viewModel}" …Run Code Online (Sandbox Code Playgroud) android android-viewpager swiperefreshlayout android-recyclerview android-viewpager2
我使用 ViewPager2 和 FragmentStateAdapter 来绑定片段。我的每个片段都有 3 个垂直排列的回收器视图。问题是我无法在片段内滚动回收器视图,而且当我尝试垂直滚动内容时,视图分页器会更改选项卡。如何解决这个问题?