CardView
内有一个RecyclerView
,设计如下:
以下是相同的代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:id="@+id/cv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/cv_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="10dp"
android:fontFamily="@font/roboto"
android:text="@string/customer_feedback"
android:textColor="@color/grey_shadow"
android:textSize="12sp" />
<TableLayout
android:id="@+id/tbl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:stretchColumns="3">
<TableRow>
<ImageView
android:id="@+id/cv_icon"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_column="1"
android:layout_marginEnd="16dp"
android:contentDescription="@string/row_title_icon"
android:src="@drawable/ic_settings" />
<TextView
android:id="@+id/cv_title"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:layout_column="2"
android:fontFamily="@font/roboto"
android:gravity="start"
android:text="Android"
android:textSize="16sp" />
<TextView
android:id="@+id/cv_title_data"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:layout_column="3"
android:fontFamily="@font/open_sans"
android:gravity="center|end"
android:text="Nougat 7.2.1"
android:textSize="12sp" />
</TableRow>
<TableRow
android:id="@+id/tv_subtitle_row"
android:animateLayoutChanges="true"
android:visibility="visible">
<TextView
android:id="@+id/cv_subtitle"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:layout_column="2"
android:layout_marginTop="12dp"
android:fontFamily="@font/roboto"
android:gravity="start"
android:text="Operating System"
android:textSize="14sp" />
<TextView
android:id="@+id/cv_subtitle_data"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:layout_column="3"
android:layout_marginTop="12dp"
android:fontFamily="@font/open_sans"
android:gravity="end"
android:text="Build 23.4.10 7.2.1"
android:textSize="12sp" />
</TableRow>
</TableLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="2dp"
android:background="@color/lightGrey" />
<android.support.v7.widget.AppCompatButton
style="?android:attr/borderlessButtonStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="@font/open_sans"
android:text="Expand"
android:textAllCaps="false"
android:textColor="@color/grey_shadow" />
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
这是我的RecyclerView
适配器:
public class SystemInfoAdapter extends RecyclerView.Adapter<SystemInfoAdapter.SystemInfoViewHolder> {
private Context mContext;
private List<CardData> systemInformation;
public class SystemInfoViewHolder extends RecyclerView.ViewHolder {
public TextView tvGroup;
public TextView tvTitle;
public TextView tvTitleValue;
public TextView tvSubtitle;
public TextView tvSubtitleValue;
public ImageView ivIcon;
public SystemInfoViewHolder(View itemView) {
super(itemView);
tvGroup = itemView.findViewById(R.id.cv_group);
tvTitle = itemView.findViewById(R.id.cv_title);
tvTitleValue = itemView.findViewById(R.id.cv_title_data);
tvSubtitle = itemView.findViewById(R.id.cv_subtitle);
tvSubtitleValue = itemView.findViewById(R.id.cv_subtitle_data);
ivIcon = itemView.findViewById(R.id.cv_icon);
}
}
public SystemInfoAdapter(Context mContext, List<CardData> systemInformation) {
this.mContext = mContext;
this.systemInformation = systemInformation;
}
@Override
public SystemInfoViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.cv_system_info, parent, false);
return new SystemInfoViewHolder(itemView);
}
@Override
public void onBindViewHolder(SystemInfoViewHolder holder, int position) {
CardData sysInfo = systemInformation.get(position);
holder.tvGroup.setText(sysInfo.getCvGroup());
holder.tvSubtitle.setText(sysInfo.getCvSubtitle());
holder.tvSubtitleValue.setText(sysInfo.getCvSubtitleData());
holder.tvTitle.setText(sysInfo.getCvTitle());
holder.tvTitleValue.setText(sysInfo.getCvTitleData());
holder.ivIcon.setImageDrawable(sysInfo.getCvTitleIcon());
}
@Override
public int getItemCount() {
return systemInformation.size();
}
}
问题是,CardView
内的表格(绿色表1和红色表2)将具有动态的行数。如何在不创建多个Visibility.GONE
行的情况下实现此目的?我最初的想法是制作几个不可见的行,并通过按钮点击使其可见,但我意识到这可能是一种非常难看的方式。
感谢。
答案 0 :(得分:1)
首先,拥有多个Visibility.GONE
行并不一定是错的,并且可能比替代方案更简单。另一种方法是拥有多种视图类型。 Recycler已经内置了它的设计。
以下是有关Multiple ViewTypes的回答问题的链接,该问题应该是一个很好的起点。