0

I have a CardView inside a RecyclerView designed as such:

enter image description here

Here is the code for the same:

<?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>

Here is my RecyclerView Adapter:

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();
    }
}

Problem is, that both the tables (Table 1 in green and Table 2 in red) inside the CardView will hav dynamic amount of rows. How can I achieve this without creating multiple Visibility.GONE rows? My initial idea was to make several invisible rows and make the visible with the button click, but I realised this maybe a really ugly way to do it.

Thanks.

Jishan
  • 1,654
  • 4
  • 28
  • 62

1 Answers1

1

First, it's not necessarily wrong to have multiple Visibility.GONE rows, and may be simpler than the alternative. An alternative is to have multiple view types. Recycler has that built into it's design.

Here is a link to an answered question concerning Multiple ViewTypes which should be a good place to start.

Les
  • 10,335
  • 4
  • 40
  • 60
  • My concern is performance degradation(?) with `Visibility.GONE`. It indeed seems simpler than the other approach. – Jishan Mar 08 '18 at 16:53
  • 1
    Yes, using multiple viewTypes will be more performant than one which has to hide fields. But I don't think the simple approach will be that much worse. The views are already inflated and there shouldn't be very many views to have to adjust (per the nature of a recycler). – Les Mar 11 '18 at 18:21