工具栏在AppBarLayout中是可滚动的,即使RecyclerView没有足够的内容可以滚动。

74
在AppBarLayout中的工具栏是否可滚动是有意为之的吗?即使带有“appbar_scrolling_view_behavior”的主容器没有足够的内容进行真正的滚动?
到目前为止,我所测试过的:
当我使用一个NestedScrollView作为主容器,并将TextView作为子容器时,AppBarLayout可以正常工作且不会滚动。
然而,当我使用一个仅有几项条目并具有“wrap_content”属性(因此没有滚动需求)的RecyclerView时,即使RecyclerView从未接收到滚动事件(使用OnScrollChangeListener测试),AppBarLayout中的工具栏也是可滚动的。
这是我的布局代码:
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/coordinatorLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appBarLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways"
            app:theme="@style/ToolbarStyle" />
    </android.support.design.widget.AppBarLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>

以下是翻译结�,仅供�考:

尽管无需滚动,但工具��滚动的效�如下:

我也找到了一�处�方法,�检查所有RecyclerView项目是���,并使用RecyclerView的setNestedScrollingEnabled()方法。 �过,对我�说似�更�是一个错误。 有什么���? 😄

编辑#1:

对��能对我当�解决方案感兴趣的人,请注�,由�LayoutManager始终在调用用�查找第一个和最�一个项是���的方法时返�-1,我必须将setNestedScrollingEnabled()逻辑放在5ms延迟的Handler的postDelayed()方法中。
我在RecyclerView�始化�的onStart()方法中使用此代�,并且�当RecyclerView的内容更改�都会使用该代�。

final LinearLayoutManager layoutManager = (LinearLayoutManager) mRecyclerView.getLayoutManager();
new Handler().postDelayed(new Runnable() {
    @Override
    public void run() {
        //no items in the RecyclerView
        if (mRecyclerView.getAdapter().getItemCount() == 0)
            mRecyclerView.setNestedScrollingEnabled(false);
        //if the first and the last item is visible
        else if (layoutManager.findFirstCompletelyVisibleItemPosition() == 0
                && layoutManager.findLastCompletelyVisibleItemPosition() == mRecyclerView.getAdapter().getItemCount() - 1)
            mRecyclerView.setNestedScrollingEnabled(false);
        else
            mRecyclerView.setNestedScrollingEnabled(true);
    }
}, 5);

编辑 #2:

我刚刚试用了一个新应用程序,发现这种(意外的)行为已经在支持库版本23.3.0(甚至更早)中得到修复。因此,不再需要使用解决方法了!


我的观点是这是有意为之的。这个问题在这里已经被问了多次,如果这是一个 bug,他们早就修复了 - 设计库已经不再年轻了。 - natario
好的,谢谢你的回答。由于我自己没有找到提到的答案/讨论,你能否至少发布一个你的来源? - eickeee
这不是 bug,所有 viewGroup 中的事件都是以这种方式处理的。因为你的 RecyclerView 是 CoordinatorLayout 的子项,所以每当事件生成时,首先会检查其父级,如果父级不感兴趣,则将其传递给子项。 - Sulabh Deep Puri
我在 Material Design 规范中没有找到任何相关参考,但根据 Inbox by Gmail 和 _Google Play_(我的愿望清单)目前的工作方式,似乎正确的行为是仅在有足够内容可滚动时才滚动应用程序栏。 - joelpet
请查看 https://dev59.com/UFwY5IYBdhLWcg3wF0le#61941446 - Vipul Kumar
9个回答

7
所以,恰当的信用,这个答案几乎为我解决了问题https://dev59.com/OlwY5IYBdhLWcg3wno8r#32923226。但是由于当您实际拥有可滚动的recyclerview并且其最后一个项目可见时它不显示工具栏(它不会在第一次向上滚动时显示工具栏),因此我决定修改它并将其调整为更容易实现和适应动态适配器。
首先,您必须为应用程序栏创建自定义布局行为:
public class ToolbarBehavior extends AppBarLayout.Behavior{

private boolean scrollableRecyclerView = false;
private int count;

public ToolbarBehavior() {
}

public ToolbarBehavior(Context context, AttributeSet attrs) {
    super(context, attrs);
}

@Override
public boolean onInterceptTouchEvent(CoordinatorLayout parent, AppBarLayout child, MotionEvent ev) {
    return scrollableRecyclerView && super.onInterceptTouchEvent(parent, child, ev);
}

@Override
public boolean onStartNestedScroll(CoordinatorLayout parent, AppBarLayout child, View directTargetChild, View target, int nestedScrollAxes, int type) {
    updatedScrollable(directTargetChild);
    return scrollableRecyclerView && super.onStartNestedScroll(parent, child, directTargetChild, target, nestedScrollAxes, type);
}

@Override
public boolean onNestedFling(CoordinatorLayout coordinatorLayout, AppBarLayout child, View target, float velocityX, float velocityY, boolean consumed) {
    return scrollableRecyclerView && super.onNestedFling(coordinatorLayout, child, target, velocityX, velocityY, consumed);
}

private void updatedScrollable(View directTargetChild) {
    if (directTargetChild instanceof RecyclerView) {
        RecyclerView recyclerView = (RecyclerView) directTargetChild;
        RecyclerView.Adapter adapter = recyclerView.getAdapter();
        if (adapter != null) {
            if (adapter.getItemCount()!= count) {
                scrollableRecyclerView = false;
                count = adapter.getItemCount();
                RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
                if (layoutManager != null) {
                    int lastVisibleItem = 0;
                    if (layoutManager instanceof LinearLayoutManager) {
                        LinearLayoutManager linearLayoutManager = (LinearLayoutManager) layoutManager;
                        lastVisibleItem = Math.abs(linearLayoutManager.findLastCompletelyVisibleItemPosition());
                    } else if (layoutManager instanceof StaggeredGridLayoutManager) {
                        StaggeredGridLayoutManager staggeredGridLayoutManager = (StaggeredGridLayoutManager) layoutManager;
                        int[] lastItems = staggeredGridLayoutManager.findLastCompletelyVisibleItemPositions(new int[staggeredGridLayoutManager.getSpanCount()]);
                        lastVisibleItem = Math.abs(lastItems[lastItems.length - 1]);
                    }
                    scrollableRecyclerView = lastVisibleItem < count - 1;
                }
            }
        }
    } else scrollableRecyclerView = true;
  }
}

然后,您只需要在布局文件中为应用栏定义此行为:
<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fitsSystemWindows="true"
    app:layout_behavior="com.yourappname.whateverdir.ToolbarBehavior"
    >

我还没有测试它的屏幕旋转功能,如果可以的话,请告诉我。我想应该可以,因为我认为当旋转发生时,计数变量不会被保存,但如果不行,请告诉我。

对于我来说,这是最简单和最清晰的实现方式,希望你喜欢。


我认为根据问题中的布局,你应该将方法命名为 updatedScrollable(target),因为 RecyclerView 不是 AppBarLayout 的直接子级。 - OneEyeQuestion
这实际上是对CoordinatorLayout的引用,它是父级视图;)。 - emirua

7

编辑 2:

事实证明,确保当RecyclerView不可滚动时Toolbar不可滚动的唯一方法是通过编程方式设置setScrollFlags,这需要检查RecyclerView是否可滚动。每次修改适配器时都必须进行此检查。

与Activity通信的接口:

public interface LayoutController {
    void enableScroll();
    void disableScroll();
}

主活动:

public class MainActivity extends AppCompatActivity implements 
    LayoutController {

    private CollapsingToolbarLayout collapsingToolbarLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        collapsingToolbarLayout = 
              (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);

        final FragmentManager manager = getSupportFragmentManager();
        final Fragment fragment = new CheeseListFragment();
        manager.beginTransaction()
                .replace(R.id.root_content, fragment)
                .commit();
    }

    @Override
    public void enableScroll() {
        final AppBarLayout.LayoutParams params = (AppBarLayout.LayoutParams)
                                  collapsingToolbarLayout.getLayoutParams();
        params.setScrollFlags(
                AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL 
                | AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS
        );
        collapsingToolbarLayout.setLayoutParams(params);
    }

    @Override
    public void disableScroll() {
        final AppBarLayout.LayoutParams params = (AppBarLayout.LayoutParams)
                                  collapsingToolbarLayout.getLayoutParams();
        params.setScrollFlags(0);
        collapsingToolbarLayout.setLayoutParams(params);
    }
}

activity_main.xml:

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:fitsSystemWindows="true">

    <android.support.design.widget.CoordinatorLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/main_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.design.widget.AppBarLayout
            android:id="@+id/appbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

            <android.support.design.widget.CollapsingToolbarLayout
                android:id="@+id/collapsing_toolbar"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                app:contentScrim="?attr/colorPrimary">

                <android.support.v7.widget.Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    android:background="?attr/colorPrimary"
                    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

            </android.support.design.widget.CollapsingToolbarLayout>

        </android.support.design.widget.AppBarLayout>

        <FrameLayout
            android:id="@+id/root_content"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="fill_vertical"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

    </android.support.design.widget.CoordinatorLayout>

</android.support.v4.widget.DrawerLayout>

测试片段:

public class CheeseListFragment extends Fragment {

    private static final int DOWN = 1;
    private static final int UP = 0;

    private LayoutController controller;
    private RecyclerView rv;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);

        try {
            controller = (MainActivity) getActivity();
        } catch (ClassCastException e) {
            throw new RuntimeException(getActivity().getLocalClassName()
                    + "must implement controller.", e);
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rv = (RecyclerView) inflater.inflate(
                R.layout.fragment_cheese_list, container, false);
        setupRecyclerView(rv);

        // Find out if RecyclerView are scrollable, delay required
        final Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (rv.canScrollVertically(DOWN) || rv.canScrollVertically(UP)) {
                    controller.enableScroll();
                } else {
                    controller.disableScroll();
                }
            }
        }, 100);

        return rv;
    }

    private void setupRecyclerView(RecyclerView recyclerView) {
        final LinearLayoutManager layoutManager = new LinearLayoutManager(recyclerView.getContext());

        recyclerView.setLayoutManager(layoutManager);

        final SimpleStringRecyclerViewAdapter adapter =
                new SimpleStringRecyclerViewAdapter(
                        getActivity(),
                        // Test ToolBar scroll
                        getRandomList(/* with enough items to scroll */)
                        // Test ToolBar pin
                        getRandomList(/* with only 3 items*/)
                );

        recyclerView.setAdapter(adapter);
    }
}

资料来源:

编辑:

您应该使用CollapsingToolbarLayout来控制行为。

直接将工具栏添加到AppBarLayout使您可以访问enterAlwaysCollapsed和exitUntilCollapsed滚动标志,但无法详细控制不同元素对崩溃的反应。 [...] setup使用CollapsingToolbarLayout的app:layout_collapseMode =“pin”确保工具栏本身固定在屏幕顶部,而视图会崩溃。http://android-developers.blogspot.com.tr/2015/05/android-design-support-library.html

<android.support.design.widget.CollapsingToolbarLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">

    <android.support.v7.widget.Toolbar
        android:id="@+id/drawer_toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        app:layout_collapseMode="pin"/>

</android.support.design.widget.CollapsingToolbarLayout>

添加

app:layout_collapseMode="pin"

将其添加到您的XML工具栏中。
    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways"
        app:layout_collapseMode="pin"
        app:theme="@style/ToolbarStyle" />

它对我不起作用。你测试过吗?我想是没有。 - eickeee
是的,我在我的应用程序中使用它。但我刚刚注意到了我认为的区别。请查看我的编辑。您应该使用CollapsingToolbarLayout包装Toolbar,并在该视图上设置scrollFlags,并确保将Toolbar设置为固定。 - user3623735
嘿,首先,很抱歉我完全误解了你想做的事情,并通过给出错误答案浪费了你的时间。其次,请检查我的编辑2,它正好可以实现你想要做的事情。如果你有任何问题,请告诉我。 - user3623735
整体想法是有道理的,但存在一个问题。如果RecyclerView在占用整个CoordinatorLayout垂直空间时不滚动,但在占用AppBarLayout + CollapsingToolbarLayout剩余所有空间时会滚动,这就出现了问题:RecyclerView可以在剩余空间内正常滚动,但AppBarLayout无法折叠。为了解决这个问题,我们应该以某种方式检查“当CollapsingToolbarLayout完全展开时,是否会在剩余的垂直空间内垂直滚动”。 - droid256

1

我使用自己的Behavior类来实现它,该类可以附加到AppBarLayout上:

public class CustomAppBarLayoutBehavior extends AppBarLayout.Behavior {

private RecyclerView recyclerView;
private int additionalHeight;

public CustomAppBarLayoutBehavior(RecyclerView recyclerView, int additionalHeight) {
    this.recyclerView = recyclerView;
    this.additionalHeight = additionalHeight;
}

public boolean isRecyclerViewScrollable(RecyclerView recyclerView) {
    return recyclerView.computeHorizontalScrollRange() > recyclerView.getWidth() || recyclerView.computeVerticalScrollRange() > (recyclerView.getHeight() - additionalHeight);
}

@Override
public boolean onStartNestedScroll(CoordinatorLayout parent, AppBarLayout child, View directTargetChild, View target, int nestedScrollAxes) {
    if (isRecyclerViewScrollable(mRecyclerView)) {
        return super.onStartNestedScroll(parent, child, directTargetChild, target, nestedScrollAxes);
    }
    return false;
}

}

以下是设置此行为的代码:

And below is the code how to set this behavior:

final View appBarLayout = ((DrawerActivity) getActivity()).getAppBarLayoutView();
CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) appBarLayout.getLayoutParams();
layoutParams.setBehavior(new AppBarLayoutNoEmptyScrollBehavior(recyclerView, getResources().getDimensionPixelSize(R.dimen.control_bar_height)));

1

LayoutManager 的子类中编写类似以下代码似乎可以实现所需的行为:

@Override
public boolean canScrollVertically() {
    int firstCompletelyVisibleItemPosition = findFirstCompletelyVisibleItemPosition();
    if (firstCompletelyVisibleItemPosition == RecyclerView.NO_POSITION) return false;

    int lastCompletelyVisibleItemPosition = findLastCompletelyVisibleItemPosition();
    if (lastCompletelyVisibleItemPosition == RecyclerView.NO_POSITION) return false;

    if (firstCompletelyVisibleItemPosition == 0 &&
            lastCompletelyVisibleItemPosition == getItemCount() - 1)
        return false;

    return super.canScrollVertically();
}

canScrollVertically() 的文档说明如下:

/**
 * Query if vertical scrolling is currently supported. The default implementation
 * returns false.
 *
 * @return True if this LayoutManager can scroll the current contents vertically
 */

注意“可以垂直滚动当前内容”的措辞,我认为这意味着返回值应该反映当前状态。
然而,在提供的任何LayoutManager子类中都没有做到这一点,通过v7 recyclerview库(23.1.1),这让我有些犹豫是否这是正确的解决方案;在除了本问题讨论的情况以外,它可能会导致不良影响。

这里的一个问题是,如果在应用栏被滚动移开后删除了填充屏幕的项目,则应用栏将卡在屏幕外,因为AppBarLayout.Behavior将在onStartNestedScroll中返回false,因此不会接收到处理应用栏滚动的onNested[Pre]Scrolll方法的任何调用。原因是,只有当RecyclerViewLayoutManager表示它可以垂直滚动时,才会添加SCROLL_AXIS_VERTICAL标志。 - joelpet

1
这不是一个bug,所有的viewGroup都是这样处理事件的。因为你的recyclerview是coordinatorLayout的子视图,所以每当事件产生时,首先检查父视图,如果父视图不感兴趣,才会传递给子视图。 请参阅谷歌文档

0

我建议你尝试使用this示例来支持设计库元素。

这是一个类似于你的布局在示例中的布局。

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:layout_scrollFlags="scroll|enterAlways" />

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</android.support.design.widget.CoordinatorLayout>

你启动示例了吗? - Mohammad Hossein Gerami
我进行了测试,如果你减少RecyclerView中的项目,你会发现这个例子也有同样的问题。所以问题是一个bug还是故意的仍然存在。 - eickeee
请查看我的回答,了解预期行为。我也遇到了建议项目的同样问题:https://www.dropbox.com/s/16fep4r7linjtnp/sameproblem.mov?dl=0 - Kenneth

0
我想对user3623735的回答进行补充。以下代码是绝对不正确的。
// Find out if RecyclerView are scrollable, delay required
    final Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            if (rv.canScrollVertically(DOWN) || rv.canScrollVertically(UP)) {
                controller.enableScroll();
            } else {
                controller.disableScroll();
            }
        }
    }, 100);

即使它能正常工作,也不能覆盖所有情况。没有任何保证数据将在100毫秒内显示,并且在处理数据时,数据可能会拉伸视图的高度,不仅限于onCreateView方法。因此,您应该使用下面的代码并跟踪视图高度的变化:
view.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
        @Override
        public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
            if(bottom != oldBottom)
            {
                mActivity.setScrollEnabled(view.canScrollVertically(0) || view.canScrollVertically(1));
            }
        }
    });

此外,你不需要创建两个单独的方法来控制滚动状态,应该使用一个 setScrollEnabled 方法:
public void setScrollEnabled(boolean enabled) {
    final AppBarLayout.LayoutParams params = (AppBarLayout.LayoutParams)
            mToolbar.getLayoutParams();

    params.setScrollFlags(enabled ?
            AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL | AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS : 0);

    mToolbar.setLayoutParams(params);
}

0

谢谢,我创建了一个自定义的RecyclerView类,但关键是仍在使用setNestedScrollingEnabled()。在我的端上它运行得很好。

public class RecyclerViewCustom extends RecyclerView implements ViewTreeObserver.OnGlobalLayoutListener
{
    public RecyclerViewCustom(Context context)
    {
        super(context);
    }

    public RecyclerViewCustom(Context context, @Nullable AttributeSet attrs)
    {
        super(context, attrs);
    }

    public RecyclerViewCustom(Context context, @Nullable AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
    }

    /**
     *  This supports scrolling when using RecyclerView with AppbarLayout
     *  Basically RecyclerView should not be scrollable when there's no data or the last item is visible
     *
     *  Call this method after Adapter#updateData() get called
     */
    public void addOnGlobalLayoutListener()
    {
        this.getViewTreeObserver().addOnGlobalLayoutListener(this);
    }

    @Override
    public void onGlobalLayout()
    {
        // If the last item is visible or there's no data, the RecyclerView should not be scrollable
        RecyclerView.LayoutManager layoutManager = getLayoutManager();
        final RecyclerView.Adapter adapter = getAdapter();
        if (adapter == null || adapter.getItemCount() <= 0 || layoutManager == null)
        {
            setNestedScrollingEnabled(false);
        }
        else
        {
            int lastVisibleItemPosition = ((LinearLayoutManager) layoutManager).findLastCompletelyVisibleItemPosition();
            boolean isLastItemVisible = lastVisibleItemPosition == adapter.getItemCount() - 1;
            setNestedScrollingEnabled(!isLastItemVisible);
        }

        unregisterGlobalLayoutListener();
    }

    private void unregisterGlobalLayoutListener()
    {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
        {
            getViewTreeObserver().removeOnGlobalLayoutListener(this);
        }
        else
        {
            getViewTreeObserver().removeGlobalOnLayoutListener(this);
        }
    }
}

-2
在您的Toolbar中移除scroll标志,只保留enterAlways标志,这样您应该可以得到您想要的效果。为了完整起见,您的布局应如下所示:
<android.support.design.widget.CoordinatorLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/coordinatorLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appBarLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="enterAlways"
            app:theme="@style/ToolbarStyle" />
    </android.support.design.widget.AppBarLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>

6
如果RecyclerView的内容足够多,我希望工具栏可以滚动并消失在屏幕外;如果没有足够的内容,工具栏就不应该这样做。如果我取消滚动标志,工具栏将根本不会滚动。 - eickeee

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接