Android 工具栏不响应滚动标志

Android 工具栏不响应滚动标志,android,listview,tabs,android-coordinatorlayout,android-appbarlayout,Android,Listview,Tabs,Android Coordinatorlayout,Android Appbarlayout,当listview滚动时,我试图隐藏工具栏,以便只有选项卡保持可见,但由于某些原因,我不能 代码: private void setupPages() { toolbar = (Toolbar) findViewById(R.id.toolBar); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { if (toolbar != null) {

当listview滚动时,我试图隐藏工具栏,以便只有选项卡保持可见,但由于某些原因,我不能

代码:

private void setupPages() {
        toolbar = (Toolbar) findViewById(R.id.toolBar);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            if (toolbar != null) {
                toolbar.setElevation(0);
            }
        }
        viewPager = (ViewPager) findViewById(R.id.pager);
        intent = getIntent();
        servitoros_id = intent.getStringExtra(AppConstant.WAITER_INTENT_ID);
        magazi_id = intent.getStringExtra(AppConstant.COMPANY_INTENT_ID);
        title = intent.getStringExtra(AppConstant.TABLE_INTENT_ID);
        if (toolbar != null) {
            toolbar.setTitle(getString(R.string.table_id) + title);
        }
        mAdapter = new ProductsTabPagerAdapter(getSupportFragmentManager(), ProductsViewOrder.this, ProductsViewOrder.this);
        viewPager.setAdapter(mAdapter);
        tabs = (TabLayout) findViewById(R.id.tabs);
        tabs.setupWithViewPager(viewPager);
        tabs.setSelectedTabIndicatorHeight(6);
        tabs.setSelectedTabIndicatorColor(getResources().getColor(R.color.white));
        if (toolbar !=null){
            setSupportActionBar(toolbar);
        }

        coffeesList = new ArrayList<>();
        snacksList = new ArrayList<>();
        sweetsList = new ArrayList<>();
        if (toolbar != null) {
            toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem item) {
                    int id = item.getItemId();
                    switch (id) {
                        case R.id.search: {
                            showSearchResults();
                            break;
                        }
                        case R.id.cart: {
                            showCart();
                            break;
                        }
                    }

                    return true;
                }
            });
        }
    }
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@color/white">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:fitsSystemWindows="true"
    android:layout_height="wrap_content">

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

    <com.quinny898.library.persistentsearch.SearchBox
        android:id="@+id/searchbox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="-6dp"
        android:layout_marginRight="-6dp"
        android:layout_marginTop="-66dp"
        android:background="@color/btn_login"
        android:visibility="gone" />

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

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

</android.support.design.widget.CoordinatorLayout>
dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile files('libs/circularimageview.jar')
    compile 'com.android.support:appcompat-v7:23.1.0'
    compile 'com.android.support:support-v4:23.1.0'
    compile 'com.quinny898.library.persistentsearch:library:1.0.0-SNAPSHOT'
    compile 'com.android.support:design:23.1.0'
}

我们将非常感谢您的帮助。为了做到这一点,我遵循了本教程。

使用ObserviceScrollView实现您的目的,这是最干净、最好的方法

好吧,如果其他人有这个问题,我找到了解决办法。我的问题是每个片段使用的是
ListView
,而不是
RecyclerView
。如果您已经设置了一个
列表视图
,并且不希望移动到
回收视图
,那么您就可以开始了

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
   listView.setNestedScrollingEnabled(true);
}

它只对棒棒糖及以上有效

是,当工具栏和listview位于同一活动上时,可能会发生这种情况。但我的工具栏位于viewpager片段中的activity和listview上