ViewPager片段在系统栏下方滑动

时间:2016-05-14 15:31:21

标签: android-fragments android-toolbar

我的表盘有一个配套配置应用程序,它只是将各种设置显示为片段(每个设置一个,以便您可以快速浏览它们)

我希望片段视图在保持操作栏的同时可以滚动,但是发生的事情是,如果你向上滑动操作栏,它就会消失在系统栏下面并且很难向后移动。

很抱歉,如果不清楚,但这里有几个屏幕截图。 enter image description here enter image description here

以下是我的各种布局和代码组件;让我知道我是否还需要更多:

AndroidManifest/xml

 <?xml version="1.0" encoding="utf-8"?>

<uses-feature android:name="android.hardware.type.watch" android:required="false"/>

<!-- Required to act as a custom watch face. -->
<uses-permission android:name="com.google.android.permission.PROVIDE_BACKGROUND" />
<!-- So we can keep the screen on and start vibrations -->
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE"/>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ref_watch_icon"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".RefWatch"
        android:launchMode="singleTop"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

        <intent-filter>
            <action android:name=
                        "com.pipperpublishing.RefWatch.wearable.watchface.CONFIG_DIGITAL" />
            <category android:name=
                          "com.google.android.wearable.watchface.category.COMPANION_CONFIGURATION" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

</application>

我的活动的布局XML:

 <?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="@dimen/appbar_padding_top"
    android:theme="@style/AppTheme.AppBarOverlay">

    <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:popupTheme="@style/AppTheme.PopupOverlay">

    </android.support.v7.widget.Toolbar>

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

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

 <android.support.design.widget.FloatingActionButton
    android:id="@+id/fab_previous"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="start|bottom"
    android:layout_margin="@dimen/fab_margin"
    android:src="@android:drawable/ic_media_rew" />
<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab_next"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="end|bottom"
    android:layout_margin="@dimen/fab_margin"
    android:src="@android:drawable/ic_media_ff" />

和我的大部分OnCreate:

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

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
...
    /*-------------------------------------------------------------*/
    /* Set up Settings Pager, Adapter, and floating action buttons */
    /*-------------------------------------------------------------*/
...
    // Create the adapter that will return a fragment for each of the     menu setting sections
    mSettingsPagerAdapter = new  SettingsPagerAdapter(getSupportFragmentManager());
...
    mSettingsViewPager = (ViewPager) findViewById(R.id.container);
...
    // Set up the ViewPager with the Settings adapter.
    mSettingsViewPager.setAdapter(mSettingsPagerAdapter);
...

1 个答案:

答案 0 :(得分:0)

这回答了我的问题:https://guides.codepath.com/android/Using-the-App-ToolBar#using-toolbar-as-actionbar以及其他一些stackoverflow问题。我添加了这段代码:

         Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    AppBarLayout.LayoutParams params = (AppBarLayout.LayoutParams) toolbar.getLayoutParams();
    params.setScrollFlags(0);  // clear all scroll flags

并且瞧,Action Bar不再滚动到系统栏下。