【发布时间】:2018-05-08 18:14:42
【问题描述】:
我有一个带有卡片的 RecyclerView,如下所示:
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/cv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
card_view:cardPreventCornerOverlap="false"
card_view:cardCornerRadius="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="48dp">
<TextView
android:id="@+id/fragment_acts_list_item_header"
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="@android:color/holo_red_dark"
android:gravity="center_vertical"
android:maxLines="1"
android:padding="10dp"
android:text="Header text"
android:textColor="@android:color/white"
android:textSize="20sp" />
<!--My dropdown Button -->
<RelativeLayout
android:id="@+id/button"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_alignParentEnd="true"
android:layout_gravity="end"
android:gravity="center">
<View
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_alignParentEnd="false"
android:background="@drawable/ic_collapse_act" />
</RelativeLayout>
</RelativeLayout>
<!-- Contains items that are visible without expanding -->
<LinearLayout
android:id="@+id/fragment_acts_list_visible_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
/>
<!-- Contains items that are visible after expanding -->
<LinearLayout
android:id="@+id/fragment_acts_list_expandable_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:orientation="horizontal" />
</LinearLayout>
</android.support.v7.widget.CardView>
我从 API 得到了两张地图:第一张包含要一直显示在屏幕上的元素(必须在上面的代码中添加到 fragment_acts_list_visible_layout 中),其他包含必须同时显示的元素用户点击展开按钮(上面代码中的按钮,必须添加到上面代码中的fragment_acts_list_expandable_layout)。
我想用上面的布局表示两个地图的每个地图条目:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="10">
<TextView
android:id="@+id/act_row_field_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="7"
android:textAlignment="center" />
<TextView
android:id="@+id/act_row_field_value"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="3"
android:textAlignment="center" />
</LinearLayout>
如何以正确的方式做到这一点?
在onBindViewHolder 方法中使用诸如viewHolder.visibleLayout.addView(row); 之类的代码会给我重复的可见元素和错误的可展开布局高度,因为这就是RecyclerView onBindViewHolder 的工作方式(多次调用,每次调用时都添加我的行)。
UPD:这是我的 bindViewHolder 方法:
@Override
public void onBindViewHolder(@NonNull final ExpandableRecyclerAdapter.ViewHolder viewHolder,
int i) {
viewHolder.setIsRecyclable(false);
Act act = acts.get(i);
ActToDisplay visibleSettings = formatActFields(act, viewSettings);
for (Map.Entry<String, String> entry : visibleSettings.getVisibleFields().entrySet()) {
LinearLayout row = (LinearLayout) LayoutInflater.from(context)
.inflate(R.layout.act_row, viewHolder.visibleLayout, false);
TextView fieldName = row.findViewById(R.id.act_row_field_name);
TextView fieldValue = row.findViewById(R.id.act_row_field_value);
fieldName.setText(entry.getKey());
fieldValue.setText(entry.getValue());
viewHolder.visibleLayout.addView(row);
}
for (Map.Entry<String, String> entry : visibleSettings.getInvisibleFields()
.entrySet()) {
LinearLayout row = (LinearLayout) LayoutInflater.from(context)
.inflate(R.layout.act_row, viewHolder.expandableLayout, false);
TextView fieldName = row.findViewById(R.id.act_row_field_name);
TextView fieldValue = row.findViewById(R.id.act_row_field_value);
fieldName.setText(entry.getKey());
fieldValue.setText(entry.getValue());
viewHolder.expandableLayout.addView(row);
}
//check if view is expanded
final boolean isExpanded = expandState.get(i);
viewHolder.expandableLayout.setVisibility(isExpanded ? View.VISIBLE : View.GONE);
viewHolder.buttonLayout.setRotation(expandState.get(i) ? 180f : 0f);
viewHolder.buttonLayout.setOnClickListener(v -> onClickButton(viewHolder.expandableLayout, viewHolder.buttonLayout, i));
}
UPD2:
通过添加.removeAllViews() in both visible and invisible layouts inonViewRecycled`方法解决了,但第二个问题并没有消失——可扩展的布局高度太大了。
这是来自模拟器的屏幕,在单击展开后必须显示 1 个可见元素和另外一个。为什么这张卡那么大(好像把 wrap_content 算错了)?
UPD3: 可扩展布局高度的问题是我的错误 - 我已将可扩展布局方向设置为水平而不是垂直
感谢@AbuYousuf 和@pskink
【问题讨论】:
-
当用户点击 expend 时,fragment_acts_list_visible_layout 将被隐藏或显示。
-
@AndroidTeam fragment_acts_list_visible_layout 必须一直显示,点击展开按钮只能放大整个卡片视图以在可见项目下方显示 fragment_acts_list_expandable_layout
-
发布您的
onBindViewHolder方法 -
它是因为在运行两个循环之前,您必须从
visibleLayout和expandableLayout中删除所有子视图 -
是的,你也可以在那里做——也许那里更好
标签: android android-recyclerview