2017-10-14 112 views
0

我有一些使用ListViews的基於以前版本的Kivy編寫的程序。使用ListViews,通過適配器獲取選定的節點非常容易。但是,如何使用RecycleView執行此操作並不那麼明確。現在,可以使用rv.layout_manager.selected_nodes來獲取選定的值,但也有時候我對實際節點感興趣。此外,以下片段可用於生成節點,但顯然它們不是RecycleView中的實際節點。從Kivy Recycleview獲取選定節點

opts = rv.layout_manager.view_opts 
for i in range(len(rv.data)): 
    s = rv.view_adapter.get_view(i, rv.data[i], opts[i]['viewclass']) 
    print s.text, s.selected 

我有興趣找到從RecycleView中獲取選定節點的方法。

全碼:

import random 
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 

#Aside: the code in the string would need to be indented back one tab, but it's like this for SO formatting 
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: 
     key_selection: "True" 
     default_size: None, dp(56) 
     default_size_hint: 1, None 
     size_hint_y: None 
     height: self.minimum_height 
     orientation: 'vertical' 
     multiselect: False 
     touch_multiselect: True 
     touch_deselect_last: True 
''') 


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 


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


class TestApp(App): 
    def build(self): 
     self.rv = RV() 
     self.rv.layout_manager.bind(selected_nodes=self.selectionChange) 
     return self.rv 

    def selectionChange(self, inst, val): 
     print inst, val 

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

回答

1

,請告訴我們你的代碼。在可以添加一個參數rv.data「選擇」 = 0當未選擇節點和1當這是一個例子:

class TestRecycleView(RecycleView): 

    def __init__(self, **kwargs): 
     super(TestRecycleView, self).__init__(**kwargs) 
     self.data = [{'name': 'test', 'ind':0,'selected': 0}] 

class TestBox(RecycleDataBehaviour, BoxLayout): 
    def action(self, boss): 
     if boss.data[self.ind]['selected'] == 0: 
     boss.data[self.ind]['selected'] = 1 
     else: 
     boss.data[self.ind]['selected'] = 0 

的KV:

<TestRecycleView>: 
    viewclass: 'TestBox' 
    RecycleBoxLayout: 
     default_size: None, 440 
     default_size_hint: 1, None 
     size_hint_y: None 
     height: self.minimum_height 
     orientation: 'vertical' 
<TestBox>: 
    Button: 
     on_press: root.action(root.parent.parent) 

,如果我非常瞭解您想要打印的selectedLabel值:

class TestApp(App): 
    def build(self): 
     self.rv = RV() 
     self.rv.layout_manager.bind(selected_nodes=self.selectionChange) 
     return self.rv 

    def selectionChange(self, inst, val): 
     print self.rv.data[val[0]]['text'] 

proof(image link)

+0

我的錯。我列出了錯誤的示例文件。代碼被編輯進來。我的目標是從recycleView.layout_manager中獲取選定的節點。 –

+0

我更新了我的答案,希望是你想要的@PistolPete –

+0

事實證明,我正在尋找錯誤的地方。我的目標是獲得選定的小部件,並且正在尋找''rv.layout_manager''。事實證明,我應該一直在尋找''rv.view_adapter.views''。但是,感謝您的幫助。 –