Движение LinearLayout под другой LinearLayout при прокрутке RecyclerView

Сделал весь интерфейс приложения в основном RelativeLayout. В нем уже всё делал по блокам LinearLayout в одном из блоков находится RecyclerView.

Будем обозначать так:

  • LinearLayout1 - верхний блок.
  • LinearLayout2 - блок находящийся под LinearLayout1.

Не могу сделать так чтобы при скроллинге RecyclerView, LinearLayout2 двигался под LinearLayout1. Явный пример есть, при листании ленты в приложении ВКонтакте. Когда Истории и блок на историями уходять за экран.

Описание на скрине

Код того что я пробовал:

final int[] heightStart = new int[1];
        final int[] height = new int[1];
        final LinearLayout linearLayout1 = findViewById(R.id.vip_ads_container);
        linearLayout1.post(new Runnable() {
            @Override
            public void run() {
                heightStart[0] = linearLayout1.getHeight();
                height[0] = heightStart[0];
            }
        });

        commonAdsrecyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                if (dy > 0){
                    animateNavigation(true);
                    LinearLayout linearLayout1 = findViewById(R.id.vip_ads_container);
                    LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) linearLayout1.getLayoutParams();
                    layoutParams.height = height[0] - 1;
                } else {
                    animateNavigation(false);
                    LinearLayout linearLayout2 = findViewById(R.id.vip_ads_container);
                    LinearLayout.LayoutParams layoutParams1 = (LinearLayout.LayoutParams) linearLayout2.getLayoutParams();
                    layoutParams1.height = height[0] + 1;
                }
            }
        });

Мой xml, по id надо двигать vip_ads_container под background_top_conteiner так как top_main_conteiner лежит в нем:

<RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="kz.sferagroup.fromcaappication.MainActivity" android:background="#EDEDED">


    <com.google.android.material.bottomnavigation.BottomNavigationView
            android:layout_width="match_parent"
            android:layout_height="50dp" android:id="@+id/bottom_navigation_view"
            android:layout_alignParentBottom="true"
            app:itemIconTint="@color/select_btn_bottom_novigation_view"
            app:itemTextColor="@color/select_btn_bottom_novigation_view"
            app:menu="@menu/menu_bottom_navigation_view" app:itemHorizontalTranslationEnabled="false"
            android:background="@drawable/bc_btn_nav_view"/>
    <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" android:id="@+id/main_conteiner" android:background="#00FFFFFF">

        <LinearLayout
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" android:gravity="top" android:id="@+id/main_conteiner2">

            <LinearLayout
                    android:orientation="vertical"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" android:id="@+id/background_top_conteiner"
                    android:background="@color/vip_ads_container">
                
                <LinearLayout
                        android:orientation="vertical"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content" android:id="@+id/top_main_conteiner"
                        android:paddingTop="25dp"
                        android:background="@drawable/top_main_container" android:paddingBottom="25dp"
                        android:paddingStart="15dp" android:paddingEnd="15dp">

                    <LinearLayout
                            android:orientation="horizontal"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content" android:id="@+id/search_btn_conteiner"
                            android:background="@drawable/btn_search">
                        <ImageView
                                android:layout_width="21dp"
                                android:layout_height="21dp" app:srcCompat="@drawable/search"
                                android:id="@+id/img_search" android:layout_marginStart="15dp"
                                android:layout_marginTop="15dp" android:layout_marginBottom="15dp"
                                app:tint="@color/notactiv_text_button"/>
                        <TextView
                                android:text="Поиск..."
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content" android:id="@+id/text_search_btn"
                                android:fontFamily="@font/roboto" android:textSize="20sp"
                                android:textColor="@color/notactiv_text_button" android:layout_marginStart="15dp"
                                android:layout_marginTop="10dp" android:layout_marginBottom="14dp"/>
                    </LinearLayout>
                    
                    <LinearLayout
                            android:orientation="horizontal"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content" android:id="@+id/btn_top_conteiner"
                            android:layout_marginTop="25dp">
                        <LinearLayout
                                android:orientation="horizontal"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:background="@drawable/activ_botton_top_container"
                                android:id="@+id/all_ads_top_conteiner" android:gravity="center"
                        >
                            <ImageView
                                    android:layout_width="21dp"
                                    android:layout_height="21dp"
                                    android:id="@+id/all_img_btn_top"
                                    android:layout_marginStart="10dp" android:layout_marginTop="8dp"
                                    android:layout_marginBottom="8dp"
                                    app:srcCompat="@drawable/activ_all_img_button"/>
                            <TextView
                                    android:text="Все"
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content" android:id="@+id/text_all_top_btn"
                                    android:textSize="20dp" android:fontFamily="@font/roboto"
                                    android:layout_marginStart="10dp" android:layout_marginEnd="10dp"
                                    android:layout_marginTop="8dp" android:layout_marginBottom="8dp"
                                    android:textColor="@color/activ_text_button"/>
                        </LinearLayout>
                        <LinearLayout
                                android:orientation="horizontal"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:background="@drawable/not_activ_botton_top_container"
                                android:id="@+id/category_top_conteiner" android:gravity="center"
                                android:layout_marginLeft="5dp">
                            <ImageView
                                    android:layout_width="21dp"
                                    android:layout_height="21dp" app:srcCompat="@drawable/list_category"
                                    android:id="@+id/categoty_img_btn_top"
                                    android:layout_marginStart="10dp" android:layout_marginTop="8dp"
                                    android:layout_marginBottom="8dp" app:tint="@color/notactiv_text_button"/>
                            <TextView
                                    android:text="Категории"
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content" android:id="@+id/text_category_top_btn"
                                    android:textSize="20dp" android:fontFamily="@font/roboto"
                                    android:layout_marginStart="10dp" android:layout_marginEnd="10dp"
                                    android:layout_marginTop="8dp" android:layout_marginBottom="8dp"
                                    android:textColor="@color/notactiv_text_button"/>
                        </LinearLayout>
                    </LinearLayout>

                </LinearLayout>
            </LinearLayout>
            <LinearLayout
                    android:orientation="vertical"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" android:id="@+id/vip_ads_container"
                    android:background="@drawable/vip_ads_container"
                    android:paddingBottom="25dp" android:paddingTop="25dp"
                    android:addStatesFromChildren="false">
                
                <LinearLayout
                        android:orientation="horizontal"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content" android:id="@+id/text_vip_ads_conteiner"
                        android:paddingStart="15dp">
                    <TextView
                            android:text="VIP Объявления"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content" android:id="@+id/textView2"
                            android:fontFamily="@font/roboto" android:textStyle="bold" android:textSize="26sp"
                            android:textColor="@color/color__text_"/>
                </LinearLayout>

                <LinearLayout
                        android:orientation="horizontal"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content" android:id="@+id/vip_ads_view_scroll"
                        android:layout_marginTop="15dp">
                    <androidx.recyclerview.widget.RecyclerView
                            android:layout_width="match_parent"
                            android:layout_height="200dp"
                            android:id="@+id/vip_ads_recyclerview"
                            android:orientation="horizontal"
                            app:layout_constraintStart_toStartOf="parent"
                            app:layout_constraintEnd_toEndOf="parent"
                            app:layout_constraintBottom_toBottomOf="@+id/imageView6"
                            app:layout_constraintTop_toBottomOf="@+id/imageView3"
                            app:layout_constraintVertical_bias="0.6"
                            android:paddingLeft="15dp"
                            tools:listitem="@layout/maket_vip_ads"></androidx.recyclerview.widget.RecyclerView>
                </LinearLayout>

Ответы (0 шт):