JNLK
JNLK

Reputation: 1808

kivy: horizontal ListView

Is there posibility to create in Kivy framework horizontal ListView?

All examples shows how to create a list of elements with vertical orientation, but i need to arrange them horizontally with horizontal scrollbar.

Defaults:


:                   :
|                   |
+-------------------+  ^
|                   |  ^
|     Element 5     |  ^
|                   |  |
+-------------------+  |
|                   |  |
|     Element 6     |  |
|                   |  |
+-------------------+  |
|                   |  |
|     Element 7     |  |
|                   |  v
+-------------------+  v
|                   |  v
:                   :

I would like to:

... --+-----------+-----------+-----------+---- ...
      |           |           |           |
      | Element 5 | Element 6 | Element 7 |
      |           |           |           |
... --+-----------+-----------+-----------+---- ...

   <<<---------------------------------------->>>

Upvotes: 2

Views: 913

Answers (1)

Nykakin
Nykakin

Reputation: 8747

Orientation is hard-coded. If you look at https://github.com/kivy/kivy/blob/master/kivy/uix/listview.py you will find:

Builder.load_string('''
<ListView>:
    container: container
    ScrollView:
        pos: root.pos
        on_scroll_y: root._scroll(args[1])
        do_scroll_x: False
        GridLayout:
            cols: 1
            id: container
            size_hint_y: None
''')

I copied this file into project directory and replaced this with:

Builder.load_string('''
<ListView>:
    container: container
    ScrollView:
        pos: root.pos
        on_scroll_y: root._scroll(args[1])
        do_scroll_x: False
        GridLayout:
            rows: 1
            id: container
            size_hint_y: None
''')

And with this test code:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from listview import ListView
from kivy.uix.gridlayout import GridLayout


class MainView(GridLayout):
    def __init__(self, **kwargs):
        kwargs['cols'] = 2
        super(MainView, self).__init__(**kwargs)

        list_view = ListView(item_strings=[str(index) for index in range(100)])

        self.add_widget(list_view)


if __name__ == '__main__':
    from kivy.base import runTouchApp
    runTouchApp(MainView(width=800))

I managed to get horizontal list. However, it broke the scrolling. Looks like you have to alter this configuration more and modify scrolling method as well to achieve desired result.

Upvotes: 1

Related Questions