Question: How to make CardView which will contain the n-th number of textView? If I understand correctly this is a custom element, then is it necessary to do this through code? Now it is strictly stated that only 4 textView for minutes.

ViewHolder code (a):

class ScheduleViewHolder( inflater: LayoutInflater, container: ViewGroup) : RecyclerView.ViewHolder(inflater.inflate(R.layout.item_schedule, container, false)) { @BindView(R.id.textView_hour) lateinit var hour: TextView @BindView(R.id.textView_minute_first) lateinit var minuteFirst: TextView @BindView(R.id.textView_minute_second) lateinit var minuteSecond: TextView @BindView(R.id.textView_minute_third) lateinit var minuteThird: TextView @BindView(R.id.textView_minute_fourth) lateinit var minuteFourth: TextView init { ButterKnife.bind(this, itemView) } fun bind(data: TimeViewData) { hour.text = data.hour minuteFirst.text = data.minuteFirst minuteSecond.text = data.minuteSecond minuteThird.text = data.minuteThird minuteFourth.text = data.minuteFourth } } 

The markup of my CardView is:

 <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:card_view="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginEnd="1dp" android:layout_marginLeft="1dp" android:layout_marginRight="1dp" android:layout_marginStart="1dp" card_view:cardBackgroundColor="@android:color/transparent" card_view:cardElevation="0dp" card_view:cardUseCompatPadding="true"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/textView_hour" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:background="@color/colorLightGray" android:gravity="center" android:maxLines="1" android:paddingBottom="24dp" android:paddingLeft="24dp" android:paddingRight="24dp" android:paddingTop="24dp" android:textAppearance="@style/ScheduleScreen.Text.Hours" android:textStyle="bold" tools:text="14 ч." /> <TextView android:id="@+id/textView_minute_first" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="8dp" android:layout_weight="1" android:background="@color/colorLightGray" android:gravity="center" android:paddingBottom="8dp" android:paddingLeft="16dp" android:paddingRight="16dp" android:paddingTop="8dp" android:textAppearance="@style/ScheduleScreen.Text.Minute" tools:text="10 мин." /> <TextView android:id="@+id/textView_minute_second" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="8dp" android:layout_weight="1" android:background="@color/colorLightGray" android:gravity="center" android:paddingBottom="8dp" android:paddingTop="8dp" android:textAppearance="@style/ScheduleScreen.Text.Minute" tools:text="25 мин." /> <TextView android:id="@+id/textView_minute_third" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="8dp" android:layout_weight="1" android:background="@color/colorLightGray" android:gravity="center" android:paddingBottom="8dp" android:paddingTop="8dp" android:textAppearance="@style/ScheduleScreen.Text.Minute" tools:text="45 мин." /> <TextView android:id="@+id/textView_minute_fourth" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="8dp" android:layout_weight="1" android:background="@color/colorLightGray" android:gravity="center" android:paddingBottom="8dp" android:paddingTop="8dp" android:textAppearance="@style/ScheduleScreen.Text.Minute" tools:text="55 мин." /> </LinearLayout> </android.support.v7.widget.CardView> 

Task setting: Make a bus schedule. The bus can go as many times per hour. The user selects a stop and then every hour in a separate textView indicates the minute in which he comes to a stop.

If it matters: CardView is displayed in RecyclerView. All the data I have in the database, if this is important, then in Realm.

Link to the repository code: github.com/elsemTim/android-kotlin-bus_schedule

Layout: What should look like.

  • The approach is not correct, there should be 1 textView inside the scroll - elik
  • and add as many texts to it as you want to set the space with \ n - elik
  • one
    @elik updated the question. Added layout. I'm not sure that your proposal allows you to solve this problem. Can you comment again after viewing the layout? - Timur Mukhortov pm
  • Yes, tried to create a factory of texts? - elik
  • one
    @elik what kind of text factory? - Timur Mukhortov

2 answers 2

You can add a RecyclerView in the ScheduleViewHolder , in which minutes will be displayed.

Then the R.layout.item_schedule markup will look like this:

 <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:card_view="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginEnd="1dp" android:layout_marginLeft="1dp" android:layout_marginRight="1dp" android:layout_marginStart="1dp" card_view:cardBackgroundColor="@android:color/transparent" card_view:cardElevation="0dp" card_view:cardUseCompatPadding="true"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/textView_hour" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:background="@color/colorLightGray" android:gravity="center" android:maxLines="1" android:paddingBottom="24dp" android:paddingLeft="24dp" android:paddingRight="24dp" android:paddingTop="24dp" android:textAppearance="@style/ScheduleScreen.Text.Hours" android:textStyle="bold" tools:text="14 ч." /> <android.support.v7.widget.RecyclerView android:id="@+id/rv_stop_minutes" android:layout_width="match_parent" android:layout_height="300dp"/> </LinearLayout> </android.support.v7.widget.CardView> 

RecyclerView made a fixed height so that all of the ViewHolder looked the same.

After that, inside the ScheduleViewHolder you will need to create an adapter for this RecyclerView and populate it. Thus, within the added RecyclerView can be any number of cells with minutes.

    CardView is a descendant of FrameLayout . Accordingly, add to it what is necessary in an appropriate way and make the layout you want.

    It is not clear from the question if the map should be of a fixed structure (heading + table 3x5), or dynamic (the bus runs every 5 minutes, 12 lines without scrolling will not fit).
    Based on this, and make a decision about using ScrollView or something.

    • I do not get you. The structure is dynamic. Here I do not know how best to clarify, I think this is another task. Look at us at bus stop A, there are 5 buses, each of them passes a different number of times during this hour. I assume that my task is to draw only minutes, without gaps, which the designer did. Just a bus can go once 5 times, to another 1. Or consider another option, where we find the maximum number of bus passes per hour and in all other elements, we make the same number of emails. with minutes, but with dashes. How is the idea more correct? - Timur Mukhortov
    • Insert ScrollView inside CardView, then the task is trivial. - Eugene Krivenja
    • The question is how to add n elements to my CardView? Now add the markup for the item. It turns out uses always 4 textView. - Timur Mukhortov
    • Just like in any other FrameLayout . - Eugene Krivenja
    • @TimurMukhortov I think what you need is to put one TextView in CardView , and then through the RecyclerViewAdapter we TextVew say this TextVew in the right quantities. You can use one TextView and it will have what quantity you need in accordance with the data from the database. - McDaggen