我RecyclerView包含MainActivity CardViewRecyclerView一次
列表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以上包含卡片。使用
XML膨胀ViewHolder在适配器内部:
<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热门的帖子,但我一直有这个奇怪的错误:
的第一次启动应用程序时,卡片列表不会滚动,除非我单击RecyclerView一次。就好像RecyclerView最初没有被关注。
另外,如果我摆脱所有的点击监听器或类似的方式,使CardView的点击,只保留在XML中的可聚焦代码:
android:focusable="true"
android:focusableInTouchMode="false"
,那么它的滚动的时候了,但只要当我添加任何点击(侦听器)机制,或者甚至为ViewHolder添加“android:clickable =”true“”时,该错误再次出现。 请指教。谢谢
单独改变这个不解决问题对我来说。删除NestedScrollView后,RecyclerView区域不再可以滚动。 – DanSoren
@DanSoren您使用的支持库的版本是什么? –
也可以尝试使用'android:layout_height =“match_parent”'? –