In last post I posted initial implementation for my Audiobook Reader app for Ubuntu-Touch, I was able to add some more features to it then after. I added support for Adding and removing custom bookmark and play the custom bookmark.
I also added support for searching the books by title in book list view. Similar to email application in Nokia N9. In this post I will show how similar feature can be implemented in QML application.
In my implementation if you pull down the book list, then it will show the search box. You can type in that search box and it will try to show books that matches that typed text. If you don't type for some time, search box will gets disappear.
Here is demo,
Following is my code. First I created TextField where user can type search text. If user type some text then I am applying the filter to my list's data model using typed text and also resetting the timer, which is responsible in hiding the search field.
I also added support for searching the books by title in book list view. Similar to email application in Nokia N9. In this post I will show how similar feature can be implemented in QML application.
In my implementation if you pull down the book list, then it will show the search box. You can type in that search box and it will try to show books that matches that typed text. If you don't type for some time, search box will gets disappear.
Here is demo,
Following is my code. First I created TextField where user can type search text. If user type some text then I am applying the filter to my list's data model using typed text and also resetting the timer, which is responsible in hiding the search field.
.... TextField{ id: searchField width: parent.width visible: false onTextChanged: { timer.restart(); if(text.length > 0 ) { model.applyFilter(text); } else { model.reload(); } } onVisibleChanged: { if( visible) focus = true } Behavior on visible { NumberAnimation{ duration: 200 } } }Following is my list view, Here I am setting its y property based on visibility of search field. Data model implements method for showing filtered data or all the data. Other important function is onContentYChanged, this function makes search field visible if user pull down the book list view.
ListView { id:listView clip: true width: parent.width height: parent.height y: searchField.visible ? searchField.height : 0 Behavior on y { NumberAnimation{ duration: 200 } } model: ListModel { id: model Component.onCompleted: { reload(); } function reload() { var bookList = DB.getAllBooks(); model.clear(); for( var i=0; i < bookList.length ; ++i ) { model.append(bookList[i]); } } function applyFilter(bookName) { var bookList = DB.getBooksByName(bookName); model.clear(); for( var i=0; i < bookList.length ; ++i ) { model.append(bookList[i]); } } } delegate: listDelegate onContentYChanged: { if( contentY < -100 ) { searchField.visible = true; timer.running = true; } } }Lastly the timer, which is responsible for hiding the search field if user don't type anything for certain duration.
Timer{ id: timer; running: false; interval: 7000; repeat: false onTriggered: { searchField.visible = false; } }That's all needed to add search support to QML ListView.