RecyclerView可扩展的cardView

时间:2015-07-28 20:52:54

标签: android android-recyclerview recyclerview-layout

我使用带有CardView项目的RecyclerView制作小项目。我创建了可扩展卡(通过按下卡内的小按钮进行扩展)。 每张卡片始终包含可见部分(id:top_layout)和可扩展部分(id:expandable_part_layout)。

问题是当我扩展最后一个项目或项目后,扩展后底部边缘下方的RecyclerView底部边缘(项目或扩展项目的某些部分对用户是不可见的),RecyclerView应向上滚动所有项目以显示我的整个项目扩大。我尝试使用scrollToPosition和类似的方法滚动到扩展卡的末尾,但没有成功。

所以,我的问题是如何在我扩展卡片向上滚动RecyclerView后关于卡片的扩展部分的高度? - 这是我的想法,也许有人有更好的解决方案。

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   app:cardCornerRadius="8px"
   app:cardPreventCornerOverlap="false">

<RelativeLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    >

    <RelativeLayout
        android:id="@+id/top_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="false"
        android:layout_alignParentTop="true"
        >
             <!-- some other controls -->

            <ImageButton
                android:id="@+id/card_header_inner_expand_btn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:background="?android:selectableItemBackground"
                android:clickable="true"
                android:padding="6dp"
                android:src="@drawable/expand_icon" />

    </RelativeLayout>

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/expandable_part_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/top_layout"
        android:animateLayoutChanges="true"
        android:clickable="false"
        android:visibility="gone">

        <RadioGroup
            android:id="@+id/extened_stage_radiogroup"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:orientation="vertical">

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>

        </RadioGroup>

      <!-- some other controls -->

    </RelativeLayout>
</RelativeLayout>

3 个答案:

答案 0 :(得分:2)

你可以用自己开发的方式使用这个lib实例,试试这个:

第1步:在您的build.gradle文件中添加以下相关性:

**compile 'com.github.traex.expandablelayout:library:1.2.2'** use instead of
 compile 'com.github.traex.expandablelayout:library:1.3'

第2步:在下面添加查看到您的卡片布局,card_layout必须如下所示:

<com.andexert.expandablelayout.library.ExpandableLayout
    android:id="@+id/row_0"
    xmlns:expandable="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    expandable:headerLayout="@layout/YOUR_CARD_VIEW_UI"
    expandable:contentLayout="@layout/YOUR_CARD_VIEW_EXPANDED_UI"/>

第3步:在您的RecyclerView定义中,请记住:

recList.setItemViewCacheSize(ITEMS_COUNT);

随意问: - )

答案 1 :(得分:2)

为了做到这一点,你使用了什么tecn​​ique?

我遇到了同样的情况,但对我来说问题是这个函数 ExpandAndCollapseViewUtil.expand() ExpandAndCollapseViewUtil.collapse()来自本教程Diseño Android: Tarjetas con CardView所以我没有使用它。而不是我只是显示/隐藏没有任何动画的cardview视图。

答案 2 :(得分:0)

我遇到了同样的问题,并通过在ViewHolder中放置以下代码(Kotlin)来解决了该问题

    if (expandableView.isVisible) {
        val recyclerView = containerView?.parent as RecyclerView
        recyclerView.adapter.notifyItemChanged(adapterPosition)
        recyclerView.smoothScrollToPosition(adapterPosition)
    }

如您所见,在滚动到某个位置之前,我通知适配器该位置上的项目已更改。而且,只有在视图变为可见时,我才这样做