Подтвердить что ты не робот

Как разместить RecyclerView под панелью инструментов и выше TabLayout и ViewPager также обрабатывают ответы на свитки индивидуальным образом?

Я хочу создать макет, как показано ниже:

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

A КоординаторLayout, который содержит:

  • CollapsingToolbarLayout (содержит ImageView и панель инструментов)
  • RecyclerView
  • TabLayout
  • ViewPager (каждый фрагмент содержит RecyclerView)

Я хочу отвечать на события прокрутки таким образом:

  • CollapsingToolbarLayout расширяется и сворачивается при прокрутке
  • Панель инструментов располагается сверху, пока TabLayout не достигнет вершины
  • После этого панель инструментов прокрутится вверх и TabLayout вернется в начало

У меня возникают проблемы с RecyclerView между CollapsingToolbarLayout и TabLayout. Я могу реализовать этот макет без этого RecyclerView (я ставлю CollapsingToolbarLayout и TabLayout внутри AppBarLayout и ViewPager за его пределами внутри CoordinatorLayout).

Мой вопрос:

  • Где я должен поместить этот RecyclerView?
  • Какой и где layout_scrollFlags и layout_behavior следует установить для каждого макета?

Кажется, что AppBarLayout имеет ограниченную высоту. Когда я кладу RecyclerView внутри AppBarLayout, только часть части RecyclerView видна, а также TabLayout исчезает.

Я прочитал много учебников, таких как этот и множество вопросов, таких как этот и этот, но они не помогают мне.

4b9b3361

Ответ 1

используйте это как основной макет

activity_main

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/swipe_refresh_layout_profile"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:ignore="RtlHardcoded">
        <android.support.design.widget.CoordinatorLayout
            android:id="@+id/co_profile_activity_root_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/white"
            android:visibility="visible">
            <android.support.design.widget.AppBarLayout
                android:id="@+id/appbar_profile"
                android:layout_width="match_parent"
                android:layout_height="@dimen/profile_img_placeholder_height"
                android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
                <android.support.design.widget.CollapsingToolbarLayout
                    android:id="@+id/collapse_toolbar_profile"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    app:layout_scrollFlags="scroll|exitUntilCollapsed|snap">
                    <RelativeLayout
                        android:id="@+id/rel_top"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:scaleType="centerCrop">
                        <ImageView
                            android:id="@+id/img_bg_placeholder_profile"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:scaleType="centerCrop"
                            android:tint="#11000000"
                            app:layout_collapseMode="parallax"
                            app:layout_collapseParallaxMultiplier="0.9" />

                        <LinearLayout
                            android:id="@+id/lin_top_inner"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:background="#BF473e6b"
                            android:orientation="vertical"
                            android:scaleType="centerCrop">
                        </LinearLayout>

                    </RelativeLayout>

                    <FrameLayout
                        android:id="@+id/frame_detail_profile"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center|center_horizontal"
                        android:orientation="vertical"
                        app:layout_collapseMode="parallax"
                        app:layout_collapseParallaxMultiplier="0.3">
                        <android.support.v7.widget.RecyclerView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content">
                        </android.support.v7.widget.RecyclerView>
                    </FrameLayout>

                    <android.support.v7.widget.Toolbar
                        android:id="@+id/toolbar_profile"
                        android:layout_width="match_parent"
                        android:layout_height="@dimen/profile_toolbar_height"
                        android:gravity="top|center"
                        app:layout_anchor="@id/frame_detail_profile"
                        app:layout_collapseMode="pin"
                        app:theme="@style/ThemeOverlay.AppCompat.Dark"
                        app:title="">

                        <LinearLayout
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:orientation="horizontal">

                            <TextView
                                android:id="@+id/tv_toolbar_title"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_marginLeft="@dimen/profile_toolbar_title_left_margin"
                                android:gravity="center_vertical|center"
                                android:ellipsize="end"
                                android:singleLine="true"
                                android:layout_gravity="center"
                                android:textColor="@android:color/white"
                                android:textSize="20sp" />
                        </LinearLayout>
                    </android.support.v7.widget.Toolbar>

                    <android.support.design.widget.TabLayout
                        android:id="@+id/tab_layout_profile"
                        android:layout_width="match_parent"
                        android:layout_height="?attr/actionBarSize"
                        android:layout_gravity="bottom"
                        android:layout_marginTop="@dimen/profile_tab_layout_top_margin"
                        android:background="@color/white"
                        app:tabIndicatorColor="@color/colorPrimary"
                        app:tabSelectedTextColor="@color/colorPrimary"
                        app:tabTextColor="@color/charcoal_grey" />
                </android.support.design.widget.CollapsingToolbarLayout>
            </android.support.design.widget.AppBarLayout>

            <android.support.v4.view.ViewPager
                android:id="@+id/view_pager_profile"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:layout_behavior="@string/appbar_scrolling_view_behavior" />
        </android.support.design.widget.CoordinatorLayout>
    </RelativeLayout>
</android.support.v4.widget.SwipeRefreshLayout>

и для макета таблицы вкладок используются классы адаптеров.

Ответ 2

Начните с AppBarLayout, в котором вы добавляете CollapsingToolbarLayout с помощью scrollFlags = "scroll | exitUntilCollapsed",

добавьте LinearLayout с вертикальной ориентацией в CollapsingToolbarLayout и добавьте

  • FrameLayout с ImageView + Toolbar

  • RecyclerView

  • TabLayout

Ответ 3

Две вещи, которые нужно сделать: -

  • При нажатии кнопки поиска установите видимость просмотра ресайклера в VISIBLE
  • При нажатии кнопки "Назад" установите видимость просмотра ресайклера в GONE

Ниже приведены реализации:

1. Настройка видимости вида recycler для VISIBLE:

    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.searchView) {
            rView.setVisibility(VISIBLE);
        }
        return true;
    }

2. Установка видимости адаптера для рециркуляции в GONE

    MenuItem searchMenuItem = menu.findItem(R.id.searchView);
    MenuItemCompat.setOnActionExpandListener(searchMenuItem, new MenuItemCompat.OnActionExpandListener() {
        @Override
        public boolean onMenuItemActionExpand(MenuItem item) {
            return true;
        }

        @Override
        public boolean onMenuItemActionCollapse(MenuItem item) {
            recyclerView.setVisibility(GONE);
            return true;
        }
    });

ПРИМЕЧАНИЕ. Не забудьте сохранить видимость как GONE изначально при запуске активности.