2017-07-15 96 views
0

我正在使用react-native 0.46.0並對16.0.0-alpha.12作出反應。我正在構建一個應用程序,向具有特定日期的用戶顯示通知。我正在使用ListView並在粘性標題中顯示日期。我看到我的頭部在頂部粘滯,但當我滾動ListView的行組件時會疊加粘性頭部,所以我們可以看到粘性頭部。我不知道問題是什麼。在rendeRow方法中,我正在渲染一個基於類的組件。我在Android設備上運行我的應用程序。這是我使用ListView的組件。反應原生ListView的stickyHeader被ListView的行組件重疊

import React, { Component } from 'react'; 
import { View, Text, Image, ListView,TouchableOpacity,LayoutAnimation, TextInput, DeviceEventEmitter, ScrollView } from 'react-native'; 
import EachNotification from '../components/EachNotification'; 
import HeaderNotification from '../components/HeaderNotification'; 

import { WHITE } from '../assets/Colors.js'; 

const DATA = [ 
{ 
    date: '1-1-2000', 
    type: 'comment' 
}, 
{ 
    date: '1-1-2000', 
    type: 'favourite' 
}, 
{ 
    date: '1-1-2000', 
    type: 'comment' 
}, 
{ 
    date: '10-10-2000', 
    type: 'comment' 
} 
]; 


class Notification extends Component { 
     constructor(props) { 
     super(props); 

     const getSectionData = (dataBlob, sectionId) => dataBlob[sectionId]; 
     const getRowData = (dataBlob, sectionId, rowId) => dataBlob[`${rowId}`]; 

     const ds = new ListView.DataSource({ 
      rowHasChanged: (r1, r2) => r1 !== r2, 
      sectionHeaderHasChanged: (s1, s2) => s1 !== s2, 
      getSectionData, 
      getRowData, 
      }); 

     const { dataBlob, sectionIds, rowIds } = this.formatData(DATA); 
     this.state = { 
     dataSource: ds.cloneWithRowsAndSections(dataBlob, sectionIds, rowIds), 
      }; 
    } 

     formatData(data) { 
      const dateArray = []; 

      for (let i = 0; i < data.length; i++) { 
       dateArray.push(data[i].date); 
      } 

      const uniqueDateArray = dateArray.filter((elem, index, self) => { 
       return index === self.indexOf(elem); 
      }); 

      const dataBlob = {}; 
      const sectionIds = []; 
      const rowIds = []; 

      for (let sectionId = 0; sectionId < uniqueDateArray.length; sectionId++) { 
      const currentDate = uniqueDateArray[sectionId]; 

      const notifications = data.filter((notification) => notification.date === currentDate); 

      if (notifications.length > 0) { 
       sectionIds.push(sectionId); 

       dataBlob[sectionId] = { date: currentDate }; 

       rowIds.push([]); 

       for (let i = 0; i < notifications.length; i++) { 
        const rowId = `${sectionId}:${i}`; 

        rowIds[rowIds.length - 1].push(rowId); 

        dataBlob[rowId] = notifications[i]; 
       } 
      } 
     } 
     return { dataBlob, sectionIds, rowIds }; 
     } 
     render() { 
      return (

      <ListView 
       style={styles.container} 
       stickySectionHeadersEnabled 
       dataSource={this.state.dataSource} 
       renderRow={(data) => { 
        return <EachNotification />; 
       }} 
       renderSectionHeader={(sectionData) => <HeaderNotification {...sectionData} />} 
      /> 
     ); 
     } 
    } 

    const styles = { 
    container: { 
     backgroundColor: WHITE, 
    } 
    }; 

    export default Notification; 

這是我的HeaderNotification。

 import React from 'react'; 
     import { View, Text } from 'react-native'; 
     import { HEADER_GREY, BLACK } from '../assets/Colors.js'; 

     const HeaderNotification = (props) => { 
     return (
       <View style={styles.container}> 
       <Text style={styles.timeText}>{props.date}</Text> 
       </View> 
      ); 
      }; 

     const styles = { 
     container: { 
      height: 40, 
      backgroundColor: HEADER_GREY, 
      justifyContent: 'center', 
      alignItems: 'center', 
      zIndex: 200 
      }, 
     timeText: { 
      color: BLACK, 
      fontFamily: 'ubuntuBold', 
      fontSize: 14 
      } 
     }; 

    export default HeaderNotification; 

這是我的EveryNotification組件。

 import React, { Component } from 'react'; 
    import { View, Text, Image, TouchableOpacity } from 'react-native'; 
    import Icon from 'react-native-vector-icons/FontAwesome'; 

    import { LIGHT_WHITE, BLACK, WHITE, PURPLE } from '../assets/Colors.js'; 

    class EachNotification extends Component { 
     render() { 
      return (
      <TouchableOpacity style={{ zIndex: -100 }}> 
      <View style={[styles.container, { zIndex: -100 }]}> 
      <View style={styles.profileImgCircle}> 
      <Image 
        style={styles.profileImg} 
        source={require('../assets/images/profile.png')} 
       /> 
      </View> 
      <View style={styles.descriptionPart}> 
      <Text style={styles.username}>Sankalp Singha</Text> 
       <View style={styles.follower}> 
       <Icon name='child' size={18} color={PURPLE} style={styles.followerIcon} /> 
       <Text style={styles.followingText}>Started following you.</Text> 
       </View> 
      </View> 
      </View> 
      </TouchableOpacity> 
    ); 
    } 
    } 

    export default EachNotification; 

回答

0

首先,我建議使用FlatList或SectionList,因爲它們更具有高性能。我懷疑你的部分頭部組件需要一個堅實的背景顏色,以便它不與你的行重疊。截圖會更好。