如何使用反应本机VirtualizedList迭代数据对象?

时间:2017-05-10 21:07:01

标签: android listview react-native react-native-android react-native-listview

当数据是数组时,我能成功地从ListView迁移到FlatList,但是如何将Objects的新React Native List类型实现为数据?

我正在尝试使用VirtualizedList(链接:https://facebook.github.io/react-native/releases/0.44/docs/virtualizedlist.html#virtualizedlist)但无法让它迭代所有键。

示例数据:

{
   ruth: {
      id: 10901,
      fullName: 'Ruth Rachelin'
      gender: 'female'
    },
   rupert: {
      id: 20033,
      fullName: 'Rupert Luis'
      gender: 'male'
   }
}

代码,不太正确:

import React, { Component } from 'react';
import { VirtualizedList } from 'react-native';
import _isEqual from 'lodash/isEqual';
import _size from 'lodash/size';
import _forEach from 'lodash/forEach';
import GameListView from './GameListView';

export default class GameCategoriesRoll extends Component
{
    constructor(props)
    {
        super(props)
        // console.log('data:', this.props.data);
        this._getItem = this._getItem.bind(this)
        this._keyExtractor = this._keyExtractor.bind(this)
        this._renderRow = this._renderRow.bind(this)
    }

    shouldComponentUpdate(nextProps, nextState, nextContext)
    {
        return !_isEqual(this.props, nextProps) || !_isEqual(this.state, nextState)
    }

    _keyExtractor(data) {
       // returns all keys in object
        return _forEach(data, function(category, key){
            console.log('keyextractor:', key);
            return key
        });
    }

    _getItem(data) {
      // data is the whole collection returned so i try to return just one key
        return _forEach(data, function(category, key){
            console.log('getItem category: ', category, 'key', key);
            return category
        });
    }

    _renderRow(item)
    {
        console.log('renderRow: ', item); // <- returns whole collection / object
        if (item.appItems && item.appItems.length > 0)
        {
            console.log('item', item);
            /*return (
                <GameListView
                    category={item.name}
                    games={item}
                    recentlyPlayed={false}
                />)*/
        }
    }

    render()
    {
        return (
            <VirtualizedList
                keyExtractor={(item) => this._keyExtractor(item)}
                data={this.props.data}
                getItem={(data) => this._getItem(data)}
                getItemCount={(data) => data ? _size(data) : 0}
                renderItem={this._renderRow}
                debug
                contentContainerStyle={{ flexGrow: 1, overflow: 'hidden' }}
                showsVerticalScrollIndicator={false}
                automaticallyAdjustContentInsets={false}
                removeClippedSubviews={false}
                enableEmptySections={true}
            />
        )
    }
 }

1 个答案:

答案 0 :(得分:1)

我绕过它!我使用lodash的Private Sub looping() Dim rw_cnt As Long, lastRow As Long Application.ScreenUpdating = False Application.Calculation = xlCalculationManual With Sheets("Personal") lastRow = .Cells(.Rows.Count, 22).End(xlUp).Row For rw_cnt = 8 To lastRow If .Range("R" & rw_cnt).Value = "W" And .Range("R" & rw_cnt).Value <> "" Then 'I am having difficulties on this section. Dim myCel As Range Set myCel = Sheets("Personal").Range("V" & rw_cnt) With myCel .Select If .Offset(0, -8).Value > 0 Then .FormulaR1C1 = "=RC[-8]" ElseIf Abs(.Offset(0, -8).Value) < Abs(.Offset(-1, 2).Value) Then .FormulaR1C1 = "=RC[-8]" ElseIf (.Offset(0, -6).Value + .Offset(0, -8).Value) < 0 Then .FormulaR1C1 = "=RC[-8]+R[-1]C[6]" Else .FormulaR1C1 = "=-R[-1]C[7]" End If End With .Range("Z" & rw_cnt).FormulaR1C1 = "=0" End If Next rw_cnt End With 'Sheets("Personal") MsgBox ("Done!!!") Application.ScreenUpdating = True Application.Calculation = xlCalculationAutomatic End Sub 将对象转换为数组 https://lodash.com/docs/4.17.4#values

所以这就是诀窍:

_values()