2017-10-14 14 views
0

以前のバージョンのKivyで書かれたListViewを使用するプログラムがあります。 ListViewを使用すると、アダプターを介して選択したノードを取得するのが非常に簡単でした。しかし、これを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

私達にあなたのコードを表示してください。

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:あなたはパラメータを追加することができますrv.dataに=ノードが選択されていない0と1は、それが、これは一例ですされたときに「選択された」場合

<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) 

I

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

私のミス:あなたはselectedLabel値を印刷したいよく理解しています。私は間違ったサンプルファイルを列挙した。コードは編集されています。私の目標は選択したノードをrecycleView.layout_managerから取得することです。 –

+0

私は自分の答えを更新しました、希望はあなたが望んだものです@PistolPete –

+0

それは間違った場所で探していたことが判明しました。私の目標は、選択したウィジェットを取得することでした。私は '' rv.layout_manager''を見ていました。私は '' rv.view_adapter.views''を見ていたはずです。しかし、助けてくれてありがとう。 –

関連する問題