具有交替颜色的Listview反应原生

sek*_*ogs 3 react-native react-native-listview

我有一些对象,如下例所示;

[{
        "id" : 13100,
        "key" : "Emlak Vergisi",
        "y" : 135638.98
    }, {
        "id" : 13154,
        "key" : "Çevre Temizlik ",
        "y" : 956.17
    }, {
        "id" : 19998,
        "key" : "Genel Tahakkuk",
        "y" : 89030.62
    }, {
        "id" : 24998,
        "key" : "Gecekondu ve So",
        "y" : 42721.07
    }, {
        "id" : 60000,
        "key" : "Ortak Gelirler",
        "y" : 827.42
    }
]
Run Code Online (Sandbox Code Playgroud)

是否可以为每个项目设置一个带有交替颜色的列表视图?

Che*_*niv 8

我会说这种方法更清洁:

 renderRow(rowData, sectionID, rowID) {

   let style = [
         styles.row, 
         {'backgroundColor': colors[rowID % colors.length]}
       ];

   return (<View style={style}/>);
 }

 let colors = ['#123456', '#654321', '#fdecba', '#abcdef'];

 let styles = StyleSheet.create({
       row: {
            // .. rows style
       }
 });
Run Code Online (Sandbox Code Playgroud)

这样,您可以轻松地为列表中的每一行添加一个specail颜色(不仅是偶数/奇数类型)


age*_*unt 5

是的,在renderRow中,基于rowID或rowData等应用其他样式

例如:

renderRow(rowData, sectionID, rowID, highlightRow) {
    if(rowID%2 === 0) {
        return (<View style={{backgroundColor: 'blue'}}/>);
    }
    return (<View style={{backgroundColor: 'red'}}/>);
}
Run Code Online (Sandbox Code Playgroud)