如何停止循环视图清除文本条目?

问题描述 投票:0回答:1

我有一个只包含文本框的循环视图。当您在文本框中键入内容时,向下滚动并向上滚动文本将会消失。

我尝试更新循环视图的数据列表但是当我滚动它时再次重置它。我只用循环视图制作了一个单独的程序来尝试实验。

from kivy.app import App
from kivy.uix.recycleview import RecycleView
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder
from kivy.properties import ObjectProperty, ListProperty
from kivy.clock import Clock

Builder.load_string('''
<Row>:
    canvas.before:
        Color:
            rgba: 0.5, 0.5, 0.5, 1
        Rectangle:
            size: self.size
            pos: self.pos
    itemText: ''

    TextInput:
        id:CellText
        text:root.itemText

<Row2>
    canvas.before:
        Color:
            rgba: 0.5, 0.5, 0.5, 1
        Rectangle:
            size: self.size
            pos: self.pos
    itemText: ''
    TextInput:
        id:CellText
        text:root.itemText
    TextInput:
        id:CellText
        text:root.itemText2

<RV>:
    id: rv
    viewclass: 'Row'
    pos: 10,-50
    scroll_type: ['bars', 'content']
    scroll_wheel_distance: dp(114)
    bar_width: dp(10)
    RecycleGridLayout:
        cols:6
        default_size: None, dp(30) 
        default_size_hint: 1, None
        size_hint_y: None
        height: self.minimum_height
        orientation: 'vertical'
        spacing: dp(1)
''')


class RV(RecycleView):
    list_items = ListProperty([])
    numRows = 16
    def __init__(self, **kwargs):
        super(RV, self).__init__(**kwargs)
        self.data = [{'itemText': "1", 'paren': self, 'index':0},
                     {'itemText': "1", 'paren': self, 'index':0},
                     {'itemText': '0','paren': self, 'index':2},
                     {'itemText': "1", 'paren': self, 'index':0},
                     {'itemText': '0','paren': self, 'index':2},
                     {'itemText': '0','paren': self, 'index':2}]
        for x in range(2,32):
            self.data.extend([{'itemText': str(x), 'paren': self, 'index':6}])
            self.data.extend([{'itemText': "1", 'paren': self, 'index':6}])
            self.data.extend([{'itemText': "0", 'paren': self, 'index': 6}])
            self.data.extend([{'itemText': "1", 'paren': self, 'index':6}])
            self.data.extend([{'itemText': "0", 'paren': self, 'index': 6}])
            self.data.extend([{'itemText': "0", 'paren': self, 'index': 6}])

    def which_edit(self, *args):
        '''This will print the index of the box which is currently edited'''
        #print args[0].parent.index


class Row(BoxLayout):
    paren = ObjectProperty() #the instance of the rv

    def __init__(self, **kwargs):
        super(Row, self).__init__(**kwargs)
        Clock.schedule_once(self.update)

    def update(self, *args):
        self.paren.list_items.append(self)
        self.ids.CellText.bind(text=self.paren.which_edit)


class TestApp(App):

    def build(self):
        return RV()

if __name__ == '__main__':
    TestApp().run()
python-3.x kivy kivy-language
1个回答
0
投票

Problem 1

我尝试更新循环视图的数据列表但是当我滚动它时再次重置它。

根本原因

无论在TextInput中输入什么都不会反映在self.data中。因此,只要有滚动,TextInput就会从self.data刷新。

Problem 2

覆盖了index,例如{'itemText': "1", 'paren': self, 'index':0},

说明

index是RecycleView的保留属性。它用于标识列中的每个单元格。

Solution

方圆角

只要TextInput有任何更改,就更新self.data。

Snippets

TextInput:
    id:CellText
    text:root.itemText
    on_text:
        app.root.data[root.index]['itemText'] = self.text

Python脚本

index替换Index

Snippets

def __init__(self, **kwargs):
    super(RV, self).__init__(**kwargs)
    self.data = [{'itemText': "1", 'paren': self, 'Index': 0},
                 {'itemText': "1", 'paren': self, 'Index': 0},
                 {'itemText': '0', 'paren': self, 'Index': 2},
                 {'itemText': "1", 'paren': self, 'Index': 0},
                 {'itemText': '0', 'paren': self, 'Index': 2},
                 {'itemText': '0', 'paren': self, 'Index': 2}]
    for x in range(2, 32):
        self.data.extend([{'itemText': str(x), 'paren': self, 'Index': 6}])
        self.data.extend([{'itemText': "1", 'paren': self, 'Index': 6}])
        self.data.extend([{'itemText': "0", 'paren': self, 'Index': 6}])
        self.data.extend([{'itemText': "1", 'paren': self, 'Index': 6}])
        self.data.extend([{'itemText': "0", 'paren': self, 'Index': 6}])
        self.data.extend([{'itemText': "0", 'paren': self, 'Index': 6}])

Example

卖弄.朋友

from kivy.app import App
from kivy.uix.recycleview import RecycleView
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder
from kivy.uix.recycleview.views import RecycleDataViewBehavior
from kivy.properties import BooleanProperty

Builder.load_string('''
<Row>:
    canvas.before:
        Color:
            rgba: 0.5, 0.5, 0.5, 1
        Rectangle:
            size: self.size
            pos: self.pos
    itemText: ''

    TextInput:
        id:CellText
        text:root.itemText 
        on_text:
            #root.which_edit(args)
            app.root.data[root.index]['itemText'] = self.text

<RV>:
    id: rv
    viewclass: 'Row'
    pos: 10,-50

    scroll_type: ['bars', 'content']
    scroll_wheel_distance: dp(114)
    bar_width: dp(10)

    RecycleGridLayout:
        cols:6
        default_size: None, dp(30) 
        default_size_hint: 1, None
        size_hint_y: None
        height: self.minimum_height
        orientation: 'vertical'
        spacing: dp(1)
''')


class Row(RecycleDataViewBehavior, BoxLayout):
    pass
    index = None
    selected = BooleanProperty(False)
    selectable = BooleanProperty(True)

    def refresh_view_attrs(self, rv, index, data):
        ''' Catch and handle the view changes '''
        self.index = index
        return super(Row, self).refresh_view_attrs(
            rv, index, data)

    def on_touch_down(self, touch):
        ''' Add selection on touch down '''
        if super(Row, self).on_touch_down(touch):
            return True
        if self.collide_point(*touch.pos) and self.selectable:
            return self.parent.select_with_touch(self.index, touch)

    def apply_selection(self, rv, index, is_selected):
        ''' Respond to the selection of items in the view. '''
        self.selected = is_selected
        if is_selected:
            print("selection changed to {0}".format(rv.data[index]))
        else:
            print("selection removed for {0}".format(rv.data[index]))

    def which_edit(self, *args):
        '''This will print the index of the box which is currently edited'''
        print(f'args={args}')


class RV(RecycleView):
    numRows = 16

    def __init__(self, **kwargs):
        super(RV, self).__init__(**kwargs)
        self.data = [{'itemText': "1", 'paren': self, 'Index': 0},
                     {'itemText': "1", 'paren': self, 'Index': 0},
                     {'itemText': '0', 'paren': self, 'Index': 2},
                     {'itemText': "1", 'paren': self, 'Index': 0},
                     {'itemText': '0', 'paren': self, 'Index': 2},
                     {'itemText': '0', 'paren': self, 'Index': 2}]
        for x in range(2, 32):
            self.data.extend([{'itemText': str(x), 'paren': self, 'Index': 6}])
            self.data.extend([{'itemText': "1", 'paren': self, 'Index': 6}])
            self.data.extend([{'itemText': "0", 'paren': self, 'Index': 6}])
            self.data.extend([{'itemText': "1", 'paren': self, 'Index': 6}])
            self.data.extend([{'itemText': "0", 'paren': self, 'Index': 6}])
            self.data.extend([{'itemText': "0", 'paren': self, 'Index': 6}])


class TestApp(App):

    def build(self):
        return RV()


if __name__ == '__main__':
    TestApp().run()
© www.soinside.com 2019 - 2024. All rights reserved.