2017-08-23 6 views
1

Ich möchte eine recycleview machen, die mehrere Etiketten in jeder recycleview Reihe hat. In meinem Beispiel hätte ich gerne 3 Labels in jeder Zeile: 1 Label mit dem Item-Index, ein Label mit einem Item aus einem Dataset und ein Label aus einem DatasetKivy - Python - mehrere Widgets in recycleview Reihe

In diesem Beispiel (aus den kivy-Beispielen)) wir eine recycleview haben, wobei jede Reihe in der recycleview ein einzelnes Etikett enthält:

from kivy.app import App 
from kivy.lang import Builder 
from kivy.uix.recycleview import RecycleView 
from kivy.uix.recycleview.views import RecycleDataViewBehavior 
from kivy.uix.label import Label 
from kivy.properties import BooleanProperty 
from kivy.uix.recycleboxlayout import RecycleBoxLayout 
from kivy.uix.behaviors import FocusBehavior 
from kivy.uix.recycleview.layout import LayoutSelectionBehavior 

Builder.load_string(''' 
<SelectableLabel>: 
    # Draw a background to indicate selection 
    canvas.before: 
     Color: 
      rgba: (.0, 0.9, .1, .3) if self.selected else (0, 0, 0, 1) 
     Rectangle: 
      pos: self.pos 
      size: self.size 
<RV>: 
    viewclass: 'SelectableLabel' 
    SelectableRecycleBoxLayout: 
     default_size: None, dp(56) 
     default_size_hint: 1, None 
     size_hint_y: None 
     height: self.minimum_height 
     orientation: 'vertical' 
     multiselect: True 
     touch_multiselect: True 
''') 


items_1= {'apple', 'banana', 'pear', 'pineapple'} 
items_2= {'dog', 'cat', 'rat', 'bat'} 

class SelectableRecycleBoxLayout(FocusBehavior, LayoutSelectionBehavior, 
           RecycleBoxLayout): 
    ''' Adds selection and focus behaviour to the view. ''' 


class SelectableLabel(RecycleDataViewBehavior, Label): 
    ''' Add selection support to the Label ''' 
    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(SelectableLabel, self).refresh_view_attrs(
      rv, index, data) 

    def on_touch_down(self, touch): 
     ''' Add selection on touch down ''' 
     if super(SelectableLabel, 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])) 


class RV(RecycleView): 
    def __init__(self, **kwargs): 
     super(RV, self).__init__(**kwargs) 
     self.data = [{'text': str(x)} for x in items_1] 


class TestApp(App): 
    def build(self): 
     return RV() 

if __name__ == '__main__': 
    TestApp().run() 

würde ich jede recycleview Zeile wie 3 Labels habe: erstes Etikett der Index ist, ist zweites Etikett items_1 und dritte Markierung items_2. Wie folgt aus:

0 Apfel Hund

1 Banane Katze

2 Birne Ratte

3 Ananas Fledermaus

Thank you!

Antwort

0

enter image description here

der einfachste Weg ist items = [0, "apple", "dog", 1, "banana", "cat", 2, "pear", "rat", 3, "pineapple", "bat"] Offensichtlich von einem RecycleBoxLayout-RecycleGridLayout mit 3 Spalten und unter Verwendung der folgenden Liste zu ändern, könnte man Ihnen ursprüngliche Struktur Listendaten bleiben und kombiniere sie zusammen, um die Liste zu bilden, oben aber ich werde das für dich lassen;).

Eine weitere Option, die möglich sein sollte, ist die RecycleBoxLayout eine RecycleBoxLayout mit einer horizontal Ausrichtung pro Zeile hinzufügen.


Dies ist alles der Python-Code.

from kivy.app import App 
from kivy.lang import Builder 
from kivy.uix.recycleview import RecycleView 
from kivy.uix.recycleview.views import RecycleDataViewBehavior 
from kivy.uix.label import Label 
from kivy.properties import BooleanProperty 
from kivy.uix.recycleboxlayout import RecycleBoxLayout 
from kivy.uix.recyclegridlayout import RecycleGridLayout 
from kivy.uix.behaviors import FocusBehavior 
from kivy.uix.recycleview.layout import LayoutSelectionBehavior 

Builder.load_string(''' 
<SelectableLabel>: 
    # Draw a background to indicate selection 
    canvas.before: 
     Color: 
      rgba: (.0, 0.9, .1, .3) if self.selected else (0, 0, 0, 1) 
     Rectangle: 
      pos: self.pos 
      size: self.size 
<RV>: 
    viewclass: 'SelectableLabel' 
    SelectableRecycleGridLayout: 
     default_size: None, dp(56) 
     default_size_hint: 1, None 
     size_hint_y: None 
     height: self.minimum_height 
     orientation: 'vertical' 
     multiselect: True 
     touch_multiselect: True 
     cols: 3 
''') 


items = [0, "apple", "dog", 1, "banana", "cat", 2, "pear", "rat", 3, "pineapple", "bat"] 

class SelectableRecycleGridLayout(FocusBehavior, LayoutSelectionBehavior, 
           RecycleGridLayout): 
    ''' Adds selection and focus behaviour to the view. ''' 


class SelectableLabel(RecycleDataViewBehavior, Label): 
    ''' Add selection support to the Label ''' 
    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(SelectableLabel, self).refresh_view_attrs(
      rv, index, data) 

    def on_touch_down(self, touch): 
     ''' Add selection on touch down ''' 
     if super(SelectableLabel, 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])) 


class RV(RecycleView): 
    def __init__(self, **kwargs): 
     super(RV, self).__init__(**kwargs) 
     self.data = [{'text': str(x)} for x in items] 


class TestApp(App): 
    def build(self): 
     return RV() 

if __name__ == '__main__': 
    TestApp().run() 
+0

Wurde Ihre Frage beantwortet? Wenn dies in Betracht gezogen wird, oder wenn es Ihnen nur geholfen hat, können Sie darüber nachdenken, es zu erhöhen. – PalimPalim

Verwandte Themen