Guilherme Souza
Guilherme Souza

Reputation: 364

How to slide ToolBar up/down while scrolling?

I'm sure there is a simple answer for this, but i'm struggling to find it. What i'm trying to do is a simple slide behaviour for a header inside an ApplicationWindow based on a scroll (or swipe in mobile platforms) of a ListView element.

The aim is to slide the ToolBar up or down while vertically flicking the ListView in a smooth manner.

Here is an example:

import QtQuick 2.7
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.3
import QtQuick.Window 2.2
import QtQuick.Layouts 1.1

ApplicationWindow {
    id: mainWindow
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")
    header: 
        RowLayout {
            height: 30
            ToolBar {
                id: toolBar
                anchors.fill: parent
                ToolButton {
                    id: toolBtn
                    width: parent.height
                    height: width
                }
                Text {
                    id: toolBarText
                    text: qsTr("Toggle hide/show when scroll")
                    anchors.fill: parent
                    horizontalAlignment: Text.AlignHCenter
                    verticalAlignment: Text.AlignVCenter
                        font.pixelSize: 25
                }
            Behavior on y {
                NumberAnimation {
                    //Does not work
                }
            }
        }
    }
    ListModel {
        id: myModel
        Component.onCompleted: {
            for(var i = 0; i <= 100; ++i) {
                myModel.append({pos: i})
            }
        }
    }
    ListView {
        anchors.fill: parent
        spacing: 16
        anchors.top: mainWindow.header.bottom
        model: myModel
        delegate: Rectangle {
            width: parent.width
            height: 50
            border.color: 'black'
            Text {
                text: pos
                anchors.centerIn: parent
            }
        }
        onFlickingVerticallyChanged: {
            //Does not work
            toolBar.y = toolBar.height * (-1)
        }
    } 
}

As you can see i tried to change the vertical position onFlickingVerticallyChanged and set a NumberAnimation on this but i think this is fixed to the main window.

Any help will be appreciated.

Upvotes: 2

Views: 600

Answers (1)

jpnurmi
jpnurmi

Reputation: 5836

You can use ListView::headerPositioning:

import QtQuick 2.7
import QtQuick.Controls 2.0

ApplicationWindow {
    id: window
    width: 320
    height: 480
    visible: true

    ListView {
        model: 100
        anchors.fill: parent
        delegate: ItemDelegate {
            text: "Item #" + index
        }

        headerPositioning: ListView.PullBackHeader
        header: ToolBar {
            width: parent.width
            z: 2 // above the delegate items
        }
    }
}

Upvotes: 3

Related Questions