Forum Archive

Dealing with custom subviews in a custom view (an idea)

Phuket2

I am not sure what you guys will think about this. But, now when I am adding a custom view to a custom view, rather than letting layout do its work, from the parent view's layout, I call my own method user_layout on the object with the h,w I want it to be. The example below is only one custom class added to the parent custom class, but you can image if you had 5 you wanted to add. A lot of dependencies about the order of calls.

As an example, I also support the layout method if the class has no parent and could be used for debugging the class

I think it's a good idea, but I am ready to be told it's not! The learning curve!

# coding: utf-8
import ui

_demo_headers = ['one', 'two', 'three', 'four', 'five']

_header_heigth = 32

class MyCustomClass(ui.View):
    def __init__(self, frame):
        self.frame = frame
        self.background_color = 'white'
        self.flex = 'WH'

        self.img = ui.ImageView()
        self.img.image = ui.Image.named('iob:alert_circled_256')
        self.add_subview(self.img)

        self.header = HeaderClass()
        self.add_subview(self.header)

    def layout(self):
        # do resizing in the main class first
        self.img.frame = (0, _header_heigth, self.width, self.height - _header_heigth)

        # then call a layout method on my inc. views
        self.header.user_layout(self.width, _header_heigth)


class HeaderClass(ui.View):
    def __init__(self):
        for col, header in enumerate(_demo_headers):
            lb = ui.Label(name = str(col))
            lb.text = header
            lb.alignment = ui.ALIGN_CENTER
            lb.border_width = .5
            self.add_subview(lb)

            # if you want layout caloed on this custom
            # class as i understand it.
            self.flex = 'WH'

    def layout(self):
        # if the class has no parent class calling
        # the shots...
        if not self.superview:
            self.user_layout(self.width, self.height)


    def user_layout(self, w, h):
        self.width = w
        self.height = h
        num_headers = len(self.subviews)
        cell_width = self.width / num_headers

        for i, sv in enumerate(self.subviews):
            sv.frame = (i * cell_width , 0, cell_width, h)

if __name__ == '__main__':
    f = (0,0,540, 576)
    mcc = MyCustomClass(f)
    mcc.present('')
Phuket2

A few minor changes to get HeaderClass working without a parent

# coding: utf-8
import ui

_demo_headers = ['one', 'two', 'three', 'four', 'five']

_header_heigth = 32

class MyCustomClass(ui.View):
    def __init__(self, frame):
        self.frame = frame
        self.background_color = 'white'
        self.flex = 'WH'

        self.img = ui.ImageView()
        self.img.image = ui.Image.named('iob:alert_circled_256')
        self.add_subview(self.img)

        self.header = HeaderClass()
        self.add_subview(self.header)

    def layout(self):
        # do resizing in the main class first
        self.img.frame = (0, _header_heigth, self.width, self.height - _header_heigth)

        # then call a layout method on my inc. views
        self.header.user_layout(self.width, _header_heigth)


class HeaderClass(ui.View):
    def __init__(self):
        self.background_color = 'white'
        for col, header in enumerate(_demo_headers):
            lb = ui.Label(name = str(col))
            lb.text = header
            lb.alignment = ui.ALIGN_CENTER
            lb.border_width = .5
            self.add_subview(lb)

            # if you want layout called on this custom
            # class as i understand it.
            self.flex = 'WH'

    def layout(self):
        # if the class has no parent class calling
        # the shots...
        if not self.superview:
            self.user_layout(self.width, _header_heigth)


    def user_layout(self, w, h):
        self.width = w
        self.height = h
        num_headers = len(self.subviews)
        cell_width = self.width / num_headers

        for i, sv in enumerate(self.subviews):
            sv.frame = (i * cell_width , 0, cell_width, h)

if __name__ == '__main__':
    #f = (0,0,540, 576)
    #mcc = MyCustomClass(f)
    #mcc.present('')

    h = HeaderClass().present()