我要做什么::我正在尝试实现一种功能,该功能在用户向下滚动时隐藏Toolbar
,并在用户向上滚动时使其可见。就像在eBay应用中一样,当用户向下滚动时,搜索栏消失,而当用户向上滚动时,搜索栏出现在顶部。
我尝试使用app:layout_scrollFlags="scroll|enterAlways"
,但是当我向下滚动时它不会隐藏工具栏。
XML代码:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<include
android:id="@+id/toolBar"
layout="@layout/toolbar" />
<include
android:id="@+id/searchToolBar"
layout="@layout/search_toolbar" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.DrawerLayout
android:id="@+id/main_activity_drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".MainActivity">
<ScrollView
android:id="@+id/searchBarScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<LinearLayout
android:id="@+id/main_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<ImageView
android:layout_width="300dp"
android:layout_height="300dp"
android:src="@drawable/camera" />
<ImageView
android:layout_width="300dp"
android:layout_height="300dp"
android:src="@drawable/camera" />
<ImageView
android:layout_width="300dp"
android:layout_height="300dp"
android:src="@drawable/camera" />
</LinearLayout>
</ScrollView>
<android.support.design.widget.NavigationView
android:id="@+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:menu="@menu/navigation_menu">
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
工具栏布局
search_toolbar
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@color/ebayBlue"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/Base.ThemeOverlay.AppCompat.Light"
app:layout_scrollFlags="scroll|enterAlways"
android:id="@+id/searchtoolBar">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/round_corners"
android:backgroundTint="@color/white"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:padding="10dp">
<ImageView
android:id="@+id/searchImage"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_centerVertical="true"
android:layout_marginStart="16dp"
android:src="@drawable/search" />
<TextView
android:id="@+id/search_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginStart="12dp"
android:layout_toEndOf="@+id/searchImage"
android:onClick="searchWindow"
android:text="Search for anything"
android:textSize="18sp" />
<ImageView
android:id="@+id/mic_image"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_centerVertical="true"
android:layout_marginEnd="18dp"
android:layout_toLeftOf="@+id/camera_image"
android:src="@drawable/mic" />
<ImageView
android:id="@+id/camera_image"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:layout_marginEnd="16dp"
android:src="@drawable/camera" />
</RelativeLayout>
</android.support.v7.widget.Toolbar>
main toolbar
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@color/ebayBlue"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/Base.ThemeOverlay.AppCompat.Light"
android:id="@+id/toolBar">
<ImageView
android:layout_width="75dp"
android:layout_height="40dp"
android:src="@drawable/ebaylogo"
android:layout_gravity="left"/>
</android.support.v7.widget.Toolbar>
MainActivity.java
公共类MainActivity扩展了AppCompatActivity {
// ToolBar
Toolbar toolbar;
Toolbar searchToolBar;
// Drawer
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mMenuToggle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Sets toolbar
toolbar = findViewById(R.id.toolBar);
searchToolBar = findViewById(R.id.searchToolBar);
setSupportActionBar(searchToolBar);
setSupportActionBar(toolbar);
// Main Activity DrawerLayout with Toggle
mDrawerLayout = findViewById(R.id.main_activity_drawer_layout);
// Menu Toggle Button for Drawer
mMenuToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar,
R.string.navigation_drawer_open, R.string.navigation_drawer_close);
mDrawerLayout.addDrawerListener(mMenuToggle);
mMenuToggle.syncState();
}}
预期和实际结果:工具栏android:id="@+id/searchToolbar"
在用户向下滚动时应隐藏,而在用户向上滚动时应可见。现在什么也没发生。
答案 0 :(得分:1)
在您的情况下,我建议您使用BottomSheet
而不是您的LinearLayout
来实现您想要的行为要困难得多。 BottomSheet
的常规实现不能覆盖全屏,但是,您可以对其进行修改以具有全屏高度。
这是一个nice implementation,可能会有所帮助。