RecyclerView可扩展的cardView

问题描述 投票:9回答:3

我使用带有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>

android android-recyclerview recyclerview-layout
3个回答
2
投票

你用什么技术来做到这一点?

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


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步:在下面添加View to your card layout,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);

随意问:-)


0
投票

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

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

如您所见,在滚动到某个位置之前,我通知适配器此位置上的项目已更改。如果视图变得可见,我只会这样做

© www.soinside.com 2019 - 2024. All rights reserved.