web-dev-qa-db-ja.com

androidでコンテンツを上にスクロールするときにツールバーを非表示にする方法

私はコンテンツでテキストと画像をスクロールするときにツールバーを非表示にしようとしていますが、ここではスクロールビューを使用してスクロールコンテンツを取得します。

ここに私のXMlコード

content_main.XML

<Android.support.v4.widget.NestedScrollView
xmlns:app="http://schemas.Android.com/apk/res-auto"
Android:layout_width="match_parent"
Android:layout_height="match_parent"
xmlns:Android="http://schemas.Android.com/apk/res/Android"
app:layout_behavior="@string/appbar_scrolling_view_behavior">


<LinearLayout
    Android:orientation="vertical"
    Android:layout_width="match_parent"
    Android:layout_height="match_parent">

<LinearLayout
    Android:paddingTop="?android:attr/actionBarSize"
    Android:orientation="vertical"
    Android:layout_width="match_parent"
    Android:layout_height="match_parent">

    <TextView
        Android:layout_marginLeft="10dp"
        Android:layout_marginRight="10dp"
        Android:id="@+id/textone"
        Android:layout_width="match_parent"
        Android:layout_height="match_parent"
        Android:textSize="23dp"
        Android:textStyle="bold"
        Android:text="hello world jheds sdjhs jds sjbs skjs ksjs kksjs ksj sdd dskd js sk "/>

    <ImageView
        Android:id="@+id/imge"
        Android:layout_width="match_parent"
        Android:layout_height="250dp"
        Android:src="@drawable/imag_bg"/>


    <TextView
        Android:id="@+id/texttwo"
        Android:layout_width="match_parent"
        Android:layout_height="match_parent"
        Android:layout_marginLeft="10dp"
        Android:layout_marginRight="10dp"
        Android:text="Pretty good, the Toolbar is moving along with the list and getting back just as we expect it to. This is thanks to the restrictions that we put on the mToolbarOffset variable.
        If we would omit checking if it’s bigger than 0 and lower than mToolbarHeight then when
        we would scroll
         up our list, the Toolbar would move along far away off the screen, so to show it back you
         would have to scroll the list down to 0. Right now it just scrolls up to mToolbarHeight
         position and not more so it’s “sitting” right above the list all of the time and if we
         start scrolling down, we can see it immediately showing.



          up our list, the Toolbar would move along far away off the screen, so to show it back you
         would have to scroll the list down to 0. Right now it just scrolls up to mToolbarHeight
         position and not more so it’s “sitting” right above the list all of the time and if we
         start scrolling down, we can see it immediately showing

          up our list, the Toolbar would move along far away off the screen, so to show it back you
         would have to scroll the list down to 0. Right now it just scrolls up to mToolbarHeight
         position and not more so it’s “sitting” right above the list all of the time and if we
         start scrolling down, we can see it immediately showing

          up our list, the Toolbar would move along far away off the screen, so to show it back you
         would have to scroll the list down to 0. Right now it just scrolls up to mToolbarHeight
         position and not more so it’s “sitting” right above the list all of the time and if we
         start scrolling down, we can see it immediately showing

          up our list, the Toolbar would move along far away off the screen, so to show it back you
         would have to scroll the list down to 0. Right now it just scrolls up to mToolbarHeight
         position and not more so it’s “sitting” right above the list all of the time and if we
         start scrolling down, we can see it immediately showing

          up our list, the Toolbar would move along far away off the screen, so to show it back you
         would have to scroll the list down to 0. Right now it just scrolls up to mToolbarHeight
         position and not more so it’s “sitting” right above the list all of the time and if we
         start scrolling down, we can see it immediately showing
        It works pretty well, but this is not what we want. It feels weird that you can
        stop it in the middle of
        the

         scroll and the Toolbar will stay half visible. Actually this is how it’s done in Google Play
         Games app
        which I consider as a bug

        It works pretty well, but this is not what we want. It feels weird that you can
        stop it in the middle of
        the
         scroll and the Toolbar will stay half visible. Actually this is how it’s done in Google Play
         Games app
        which I consider as a bug
        It works pretty well, but this is not what we want. It feels weird that you can
        stop it in the middle of
        the
         scroll and the Toolbar will stay half visible. Actually this is how it’s done in Google Play
         Games app
        which I consider as a bug."/>




</LinearLayout>

<View
    Android:layout_width="wrap_content"
    Android:layout_height="30dp" />
<LinearLayout

    Android:layout_width="match_parent"
    Android:layout_height="match_parent">
    <Button
         Android:text="hai"
         Android:layout_width="160dp"
         Android:layout_height="match_parent" />
    <Button
         Android:text="hello"
         Android:layout_width="160dp"
         Android:layout_height="match_parent" />


</LinearLayout>

activity_main.XML

<Android.support.design.widget.AppBarLayout
    Android:layout_height="wrap_content"
    Android:layout_width="match_parent" 
    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:popupTheme="@style/AppTheme.PopupOverlay" />

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

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

here my image

14
chanti

両方のレイアウトで多くの変更を行う必要があります。最初にCoordinatorLayoutを以下のようにactivity_main.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"
    Android:id="@+id/main_content"
    Android:layout_width="match_parent"
    Android:layout_height="match_parent"
    Android:fitsSystemWindows="true">

    <Android.support.design.widget.AppBarLayout
        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"
            Android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

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

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

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

content_main.XMLでは、ScrollViewの代わりにAndroid.support.v4.widget.NestedScrollViewを使用します。

以下のようにapp:layout_behavior="@string/appbar_scrolling_view_behavior"内でAndroid.support.v4.widget.NestedScrollViewも使用します。

<Android.support.v4.widget.NestedScrollView
        xmlns:app="http://schemas.Android.com/apk/res-auto"
        Android:layout_width="match_parent"
        Android:layout_height="match_parent"
        xmlns:Android="http://schemas.Android.com/apk/res/Android" 
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <LinearLayout
            Android:layout_width="match_parent"
            Android:layout_height="match_parent"
            Android:orientation="vertical">

            <TextView
                Android:id="@+id/textone"
                Android:layout_width="match_parent"
                Android:layout_height="match_parent"
                Android:text="hello world jheds sdjhs jds sjbs skjs ksjs kksjs ksj sdd dskd js sk "
                Android:textSize="25dp"
                Android:textStyle="bold" />

            /// Add your other code here

            </LinearLayout>

    </Android.support.v4.widget.NestedScrollView>
29

これは、アプリで CoordinatorLayout を使用するのに最適なシナリオです。 CoordinatorLayoutは、非常に強力なFrameLayoutで、袖にたくさんの気の利いたアニメーショントリックがあります。

Designライブラリーは、CoordinatorLayoutを導入します。これは、Designライブラリーの多くのコンポーネントが利用する、子ビュー間のタッチイベントに対する追加レベルの制御を提供するレイアウトです。

this および this チュートリアルで開始できます。

1
Aritra Roy

Activity_main.xmlをコーディネーターレイアウトでラップして、親レイアウトになります。

<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:id="@+id/main_content"
Android:layout_width="match_parent"
Android:layout_height="match_parent"
Android:fitsSystemWindows="true">

<Android.support.design.widget.AppBarLayout
Android:layout_height="wrap_content"
Android:layout_width="match_parent" 
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:popupTheme="@style/AppTheme.PopupOverlay" />
</Android.support.design.widget.AppBarLayout>

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



</Android.support.design.widget.CoordinatorLayout>
0
Lukas Anda