我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」'? –