2015-11-06 231 views
3

当我滚动ListViewFragment我的ToolBar不隐藏/显示。在FragmentAndroid工具栏隐藏不起作用

<?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" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

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

     <include 
      layout="@layout/toolbar_layout"/> 

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

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

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

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

这我代码: 我用样品从here 这是我XML

public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.fragment_home, container, false); 

    mViewPager = (ViewPager) view.findViewById(R.id.home_viewpager); 
    mAdapter = new HomeScreenPagerAdapter(getChildFragmentManager(), getActivity()); 
    mViewPager.setAdapter(mAdapter); 
    mTabLayout = (TabLayout) view.findViewById(R.id.home_tabs); 
    mTabLayout.setupWithViewPager(mViewPager); 


    return view; 
} 

添加工具栏布局

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.Toolbar 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/toolbar" 
    android:layout_width="match_parent" 
    android:layout_height="?attr/actionBarSize" 
    android:background="?attr/colorPrimary" 
    app:layout_scrollFlags="scroll|enterAlways|snap" 
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light" /> 

我可以运行此代码,但ToolBar不会隐藏。我怎样才能隐藏/显示它?

+1

也发布您的toolbar_layout。另外,您尝试滚动的列表是否超出了您的屏幕尺寸? – Sevle

+0

@Sevle补充说。是的 - 测试列表足够大(比屏幕大) – MyNameIs

+0

我没有看到你的工具栏实现有什么问题。我只能推测,包含你的片段的列表视图的视图不支持可隐藏的工具栏。 (也许你将ListView封装在LinearLayout或简单的ScrollView中?)。尝试把你的ListView放在NestedScrollView下,并检查它是否有效。无论如何,如果你包含你的片段的布局xml,我可能会有更多的想法。 – Sevle

回答

1

您应该告诉我们toolbar_layout的代码。反正你应该添加到toolbarlayout_scrollFlags这样的:

<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|snap" /> 

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

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

不适合我 - 我添加了toolBar布局的代码。并且还添加了“| snap”。 – MyNameIs

2

没有什么不对您的工具栏实现。

appbar_scrolling_view_behavior无法工作的原因是在该片段膨胀的布局中存在不受支持的小部件。尝试将您的布局封装在NestedScrollView中,例如

<android.support.v4.widget.NestedScrollView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/ScrollView01" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior"> 

    <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="wrap_content" 
     tools:context=".MainActivity"> 
     /* Your views */ 
    </RelativeLayout> 
</android.support.v4.widget.NestedScrollView> 
0

同样的问题我解决了通过NestedScrollView而不是ScrollView在可滚动布局。

+0

嗨列昂尼德,请参阅这篇文章如何正确的好答案:[链接](http://stackoverflow.com/help/how-to-answer) – Lag