【问题标题】:RecyclerView expandable cardViewRecyclerView 可扩展cardView
【发布时间】:2018-07-31 17:45:03
【问题描述】:

我用 RecyclerView 做了一个小项目,里面有 CardView 项目。我创建了可扩展卡(通过按下卡内的小按钮进行扩展)。 每张卡片总是包含可见部分(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


【解决方案1】:

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

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

【讨论】:

    【解决方案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);
    

    欢迎提问 :-)

    【讨论】:

      【解决方案3】:

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

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

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

      【讨论】:

        猜你喜欢
        • 2018-10-01
        • 2019-08-29
        • 2020-10-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-11-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多