Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/neo4j/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从Kivy Recycleview获取所选节点_Python_Listview_Android Recyclerview_Kivy - Fatal编程技术网

Python 从Kivy Recycleview获取所选节点

Python 从Kivy Recycleview获取所选节点,python,listview,android-recyclerview,kivy,Python,Listview,Android Recyclerview,Kivy,我有一些使用ListView的Kivy早期版本编写的程序。使用ListView,通过适配器获取所选节点非常容易。然而,如何使用RecycleView实现这一点还不太清楚。现在,可以使用rv.layout\u manager.selected\u节点获取所选值,但有时我对实际节点感兴趣。另外,下面的代码片段可以用来生成节点,但显然,它们不是RecycleView中的实际节点 opts = rv.layout_manager.view_opts for i in range(len(rv.data)

我有一些使用ListView的Kivy早期版本编写的程序。使用ListView,通过适配器获取所选节点非常容易。然而,如何使用RecycleView实现这一点还不太清楚。现在,可以使用
rv.layout\u manager.selected\u节点
获取所选值,但有时我对实际节点感兴趣。另外,下面的代码片段可以用来生成节点,但显然,它们不是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获取所选节点的方法

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
完整代码:

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()
随机导入
从kivy.app导入应用程序
从kivy.lang导入生成器
从kivy.uix.recycleview导入recycleview
从kivy.uix.recycleview.views导入RecycleDataViewBehavior
从kivy.uix.label导入标签
从kivy.properties导入布尔属性
从kivy.uix.recycleboxlayout导入recycleboxlayout
从kivy.uix.behaviors导入焦点行为
从kivy.uix.recycleview.layout导入LayoutSelectionBehavior
#旁白:字符串中的代码需要缩进一个制表符,但格式是这样的
Builder.load_字符串(“”)
:
#绘制背景以指示选择
在以下情况之前:
颜色:
rgba:(.0,0.9,1,3)如果自选,则为其他(0,0,0,1)
矩形:
pos:self.pos
大小:self.size
:
viewclass:“SelectableLabel”
可选择的可循环利用布局:
按键选择:“正确”
默认大小:无,dp(56)
默认大小提示:1,无
尺寸提示:无
高度:自身最小高度
方向:“垂直”
多重选择:错误
触摸多选:真
触按\u取消选择\u最后一次:True
''')
类SelectableRecycleBoxLayout(焦点行为、布局选择行为、,
可回收性布局):
“将选择和焦点行为添加到视图中。”
类SelectableLabel(RecycleDataViewBehavior,Label):
''将选择支持添加到标签''
索引=无
selected=布尔属性(False)
可选=布尔属性(真)
def刷新\视图\属性(自身、rv、索引、数据):
''捕获并处理视图更改''
self.index=索引
返回超级(SelectableLabel,self)。刷新\u视图\u属性(
rv、索引、数据)
def on_触控向下(自身,触控):
''在触地时添加选择''
如果是超级(SelectableLabel,self)。打开时触摸下(触摸):
返回真值
如果自碰撞点(*touch.pos)和自选择:
返回self.parent。使用触摸键选择(self.index,touch)
def应用选项(选择了自身、rv、索引):
''响应视图中的项目选择''
self.selected=是否选中
RV级(回收利用审查):
定义初始(自我,**kwargs):
超级(RV,自我)。\uuuuu初始值(**kwargs)
self.data=[{'text':str(random.random())}表示范围(20)内的x
类TestApp(应用程序):
def生成(自):
self.rv=rv()
self.rv.layout\u manager.bind(选定的\u节点=self.selectionChange)
返回自助旅行车
def选择更改(自身、安装、val):
打印仪器,val
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
b=测试程序()
b、 运行()

请出示您的代码。在rv.data中,当未选择节点时,您可以添加参数“selected”=0,当未选择节点时,可以添加参数“selected”=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
千伏:

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

我的错误。我列出了错误的示例文件。代码在中编辑。我的目标是将所选节点从recycleView.layout_管理器中移除。我更新了我的答案,“希望是你想要的”,但事实证明我找错了地方。我的目标是获得选定的小部件,我正在查看
rv.layout\u manager
。原来我应该在
rv.view\u adapter.views
中查找。但是,谢谢你的帮助。