0

Создаю расписание, в нём есть день недели, время, предмет. Всё хранится в json получает по средством запроса на сайт.

Сам вопрос заключается в том, как сделать внутри RecyclerView еще RecyclerView. То есть, у нас есть день недели, до 6 штук , его мы записываем в самый вверх item, после этого записываем время и предмет и снова время и предмет, пока они не закончатся для дня недели. Затем переходим к новому дню недели и снова день недели сверху, потом время и предмет пока они не закончятся. и так до конца 6 дней недели.

Возможно есть какой-то способ проще, тк я знаю сколько дней недели задействовано и сколько максимум предметов будет.

2 ответа 2

0

Можно сделать к примеру при помощи TableLayout. То есть использовать изначально не RecyclerView а другой вью-элемент для этой задачи. Вот как добавляется tableLayout обычном активити:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tableLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:shrinkColumns="*"
    android:stretchColumns="*" >

    <TableRow
        android:id="@+id/tableRow4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal" >

        <TextView
            android:id="@+id/textView9"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_span="6"
            android:gravity="center"
            android:text="Weather Table"
            android:textSize="18dp"
            android:textStyle="bold"
            android:typeface="serif" >
        </TextView>
    </TableRow>

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/TextView04"
            android:text="" >
        </TextView>

        <TextView
            android:id="@+id/TextView04"
            android:text="Feb 7"
            android:textStyle="bold"
            android:typeface="serif" >
        </TextView>

        <TextView
            android:id="@+id/TextView03"
            android:text="Feb 8"
            android:textStyle="bold"
            android:typeface="serif" >
        </TextView>

        <TextView
            android:id="@+id/TextView02"
            android:text="Feb 9"
            android:textStyle="bold"
            android:typeface="serif" >
        </TextView>

        <TextView
            android:id="@+id/TextView01"
            android:text="Feb 10"
            android:textStyle="bold"
            android:typeface="serif" >
        </TextView>

        <TextView
            android:id="@+id/textView1"
            android:text="Feb 11"
            android:textStyle="bold"
            android:typeface="serif" >
        </TextView>
    </TableRow>

    <TableRow
        android:id="@+id/tableRow2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/textView2"
            android:text="Day High"
            android:textStyle="bold" >
        </TextView>

        <TextView
            android:id="@+id/textView3"
            android:gravity="center_horizontal"
            android:text="28°F" >
        </TextView>

        <TextView
            android:id="@+id/textView4"
            android:gravity="center_horizontal"
            android:text="26°F" >
        </TextView>

        <TextView
            android:id="@+id/textView5"
            android:gravity="center_horizontal"
            android:text="23°F" >
        </TextView>

        <TextView
            android:id="@+id/textView6"
            android:gravity="center_horizontal"
            android:text="17°F" >
        </TextView>

        <TextView
            android:id="@+id/textView7"
            android:gravity="center_horizontal"
            android:text="19°F" >
        </TextView>
    </TableRow>

    <TableRow
        android:id="@+id/tableRow2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/textView2"
            android:text="Day Low"
            android:textStyle="bold" >
        </TextView>

        <TextView
            android:id="@+id/textView3"
            android:gravity="center_horizontal"
            android:text="15°F" >
        </TextView>

        <TextView
            android:id="@+id/textView4"
            android:gravity="center_horizontal"
            android:text="14°F" >
        </TextView>

        <TextView
            android:id="@+id/textView5"
            android:gravity="center_horizontal"
            android:text="3°F" >
        </TextView>

        <TextView
            android:id="@+id/textView6"
            android:gravity="center_horizontal"
            android:text="5°F" >
        </TextView>

        <TextView
            android:id="@+id/textView7"
            android:gravity="center_horizontal"
            android:text="6°F" >
        </TextView>
    </TableRow>

    <TableRow
        android:id="@+id/tableRow3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center" >

        <TextView
            android:id="@+id/textView8"
            android:text="Conditions"
            android:textStyle="bold" >
        </TextView>

        <ImageView
            android:id="@+id/imageView1"
            android:src="@drawable/hot" >
        </ImageView>

        <ImageView
            android:id="@+id/imageView2"
            android:src="@drawable/pt_cloud" >
        </ImageView>

        <ImageView
            android:id="@+id/imageView3"
            android:src="@drawable/snow" >
        </ImageView>

        <ImageView
            android:id="@+id/imageView4"
            android:src="@drawable/lt_snow" >
        </ImageView>

        <ImageView
            android:id="@+id/imageView5"
            android:src="@drawable/pt_sun" >
        </ImageView>
    </TableRow>

</TableLayout>

и вот результат:

введите сюда описание изображения

это мне кажется будет проще чем возится с RecyclerView созданием адаптера для него и тд. Вот сайт первоисточник кода который я вам показал. Есть еще ссылка_1, ссылка_2 и ссылка_3 по вашему вопросу. Надеюсь что-то да пригодится.

0

Назовем 2-ой адаптер InnerAdapter, собственно сам класс адаптера не буду расписывать, главное onCreateViewHolder и onBindViewHolder сделать для него.

public class InnerAdapter extends RecyclerView.Adapter<InnerAdapter.ViewHolder>{...}

Назовем 1-ый адаптер OuterAdapter

public class OuterAdapter extends RecyclerView.Adapter<OuterAdapter.SomeViewHolder> {
    ...
    @Override
    public void onBindViewHolder(final OuterAdapter.SomeViewHolder holder, final int position) {
        final InnerAdapter innerAdapter = new InnerAdapter(init);
        holder.recyclerView.setLayoutManager(new LinearLayoutManager(ctx));
        holder.recyclerView.setHasFixedSize(true);
        holder.recyclerView.setAdapter(innerAdapter);
    }
}

То есть для каждого айтема вы сделаете по своему адаптеру и инициируете нужными вам параметрами. Я использую одну инициализацию, просто во второй адаптер передаю нужные значения из некого сипска, чтобы показать этот айтем в RV отдельно.

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.