我的RecyclerView包含CardView的列表
用于MainActivity的xml:
<android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.AppBarLayout>
<android.support.v7.widget.Toolbar/>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView>
<android.support.v7.widget.RecyclerView
android:id="@+id/view_recycler"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
</android.support.v7.widget.RecyclerView>
</android.support.v4.widget.NestedScrollView>
<FloatingActionButton/>
</android.support.design.widget.CoordinatorLayout>
我使用上述RecyclerView的适配器来容纳这些卡。
用于在适配器内膨胀ViewHolder的xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/relativeLayout"
android:paddingTop="2dp"
android:paddingRight="2dp"
android:paddingLeft="2dp"
android:orientation="vertical"
android:descendantFocusability="blocksDescendants">
<android.support.v7.widget.CardView
android:id="@+id/cardview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:foreground="?android:attr/selectableItemBackground"
card_view:cardBackgroundColor="@android:color/holo_red_light"
card_view:cardPreventCornerOverlap="true"
card_view:cardCornerRadius="2dp"
card_view:cardElevation="3dp"
card_view:contentPadding="7dp"
card_view:cardUseCompatPadding="true">
<RelativeLayout
android:id="@+id/relat"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="true"
android:focusableInTouchMode="false"
android:padding="10dp">
<TextView/>
//...
</RelativeLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
为了让卡片可以点击,我在these - two流行的帖子中尝试了所有的解决方案,但我总是有这个奇怪的bug:
当我第一次启动应用程序时,卡片列表不会滚动,除非我点击一次RecyclerView。这就好像RecyclerView一开始并不在焦点上。
另外,如果我去掉了所有的单击侦听器或类似的方法来使CardView的代码可单击,而只保留xml格式的可聚焦代码:
android:focusable="true"
android:focusableInTouchMode="false"
,然后它确实会立即滚动,但只要我添加任何单击(侦听器)机制,或者甚至为ViewHolder添加"android:clickable="true"“,该错误就会重新出现。请给我建议。谢谢
发布于 2016-09-09 09:19:16
事实证明,滚动问题与RecyclerView无关。这是由于我使用的一个开源小工具,它锚定在RV上,并以某种方式干扰了聚焦/滚动/触摸拦截。经过几天的寻找,终于摆脱了这个bug ..
不管怎样还是要谢谢你
发布于 2016-09-07 10:30:03
您永远不应该在ScrollView
中嵌套RecyclerView
。只需移除NestedScrollView
,RecyclerView
就会处理好它的滚动行为。
<android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.AppBarLayout>
<android.support.v7.widget.Toolbar/>
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/view_recycler"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
</android.support.v7.widget.RecyclerView>
</android.support.design.widget.CoordinatorLayout>
https://stackoverflow.com/questions/39360207
复制相似问题