问题描述
我用RecyclerView制作了一个小项目,里面有CardView项目.我创建了可扩展卡(通过按下卡内的小按钮进行扩展).每张卡始终包含可见部分(id:top_layout)和可扩展部分(id:expandable_part_layout).
I make small project with RecyclerView with CardView items inside. I created expandable card (expanded by pressing on small button inside the card).Each card always contain visible part (id:top_layout) and expandable part (id:expandable_part_layout).
问题是,当我展开最后一个项目或展开后其底部边缘低于RecyclerView底部边缘的项目(用户看不见该项目或已扩展项目的某些部分)时,RecyclerView应该向上滚动所有项目以显示整个项目扩展.我尝试使用scrollToPosition
和类似方法滚动到扩展卡的末尾,但没有成功.
Problem is that when I expand last item or item which after expand has bottom edge below bottom edge of RecyclerView (some part of item or expanded item is invisible to user) the RecyclerView should scroll up all items to show whole item that I expanded. I tried use scrollToPosition
and similar methods to scroll to end of expanded card but without success.
所以,我的问题是,在扩充卡后,向上滚动RecyclerView来了解有关扩充卡高度的问题,怎么做? -那是我的主意,也许有人有更好的解决方案.
So, my question is how to make that after I expand card scroll up RecyclerView about height of expanded part of card? - That is my idea, maybe someone has better solution.
<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>
推荐答案
您可以以自己开发的方式使用此lib实例,请尝试以下操作:
You can use this lib instance your self developed way, try this:
步骤1:将以下依赖项添加到您的build.gradle
文件中:
STEP 1: Add below dependency to your build.gradle
file:
**compile 'com.github.traex.expandablelayout:library:1.2.2'** use instead of
compile 'com.github.traex.expandablelayout:library:1.3'
步骤2:在视图"下面添加到您的卡布局中,card_layout
必须看起来像这样:
STEP 2: Add below View to Your card layout ,card_layout
Must be look like this:
<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时,请记住:
STEP 3: In your define of RecyclerView remember to:
recList.setItemViewCacheSize(ITEMS_COUNT);
随时提问:-)
这篇关于RecyclerView展开式CardView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!