Android 在RecyclerView中包含的CardView中动态添加表(和行)

Android 在RecyclerView中包含的CardView中动态添加表(和行),android,android-cardview,Android,Android Cardview,我在RecyclerView中有一个CardView,其设计如下: 以下是相同的代码: <?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">

我在RecyclerView中有一个CardView,其设计如下:

以下是相同的代码:

<?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>
问题是,CardView中的表1(绿色)和表2(红色)都具有动态行数。如何在不创建多个Visibility.GONE行的情况下实现这一点?我最初的想法是制作几个不可见的行,并通过单击按钮使其可见,但我意识到这可能是一种非常丑陋的方法


谢谢。

首先,具有多个Visibility.GONE行并不一定是错误的,而且可能比其他行更简单。另一种方法是使用多个视图类型。回收商在它的设计中加入了这一点


这里有一个链接,指向一个已回答的问题,关于哪个应该是一个好的起点。

我关心的是可见性的性能下降。消失了。的确,这似乎比另一种方法更简单。是的,使用多个视图类型比必须隐藏字段的视图类型更有效。但我不认为简单的方法会更糟糕。视图已经膨胀,根据回收商的性质,不应该有太多视图需要调整。
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();
    }
}