简体   繁体   English

React Native FlatList水平模式根本不起作用

[英]React Native FlatList horizontal mode not working at all

I am using React Native 0.44.0 and I am attempting to make a horizontal FlatList using a card style layout. 我正在使用React Native 0.44.0,我正在尝试使用卡式布局制作水平FlatList。 For whatever reason, no matter what I do, I cannot get the horizontal mode to activate. 无论出于何种原因,无论我做什么,我都无法启动水平模式。 It always seems to render vertically... 似乎总是垂直呈现......

Here is the code I am using: 这是我正在使用的代码:

 <FlatList
    horizontal={true}
    data={this.state.newsFeed}
    refreshing={this.state.refreshing}
    ref={ref => {
        this.newsFeedListRef = ref;
    }}
    renderItem={this.renderNewsFeedRow.bind(this)}
    keyExtractor={(item, index) => `feed_${index}`}
    onRefresh={this.__handleNewsFeedOnRefresh.bind(this)}
    renderScrollComponent={this.renderScrollComponent.bind(this)}
    ListHeaderComponent={this.renderListHeaderComponent.bind(this)}
    getItemLayout={(data, index) => ({
        index,
        length: ITEM_HEIGHT,
        offset: ITEM_HEIGHT * index + headerBarHeight
    })} />;

I can post the code for my component renders but none of them use anything but padding and margins for style, so flex or flexDirection stuff. 我可以发布我的组件渲染的代码,但它们都没有使用任何东西,除了样式的填充和边距,所以flexflexDirection东西。

For anyone coming in off of a Google search, I figured it out. 对于那些来自谷歌搜索的人,我想通了。 Turns out you can't render your own scroll component if you want to be able to dynamically change from horizontal to vertical or vice versa. 如果您希望能够动态地从水平变为垂直,反之亦然,则无法渲染自己的滚动组件。 So, if I take my previous code and comment out the call to renderScrollComponent , then it works... like so: 所以,如果我采用我以前的代码并注释掉对renderScrollComponent的调用,那么它的工作原理......就像这样:

<FlatList
    data={this.state.newsFeed}
    refreshing={this.state.refreshing}
    horizontal={this.state.isHorizontal}
    ref={ref => { this.newsFeedListRef = ref; }}
    renderItem={this.renderNewsFeedRow.bind(this)}
    keyExtractor={(item, index) => `feed_${index}`}
    onRefresh={this.__handleNewsFeedOnRefresh.bind(this)}
    //renderScrollComponent={this.renderScrollComponent.bind(this)}
    ListHeaderComponent={this.renderListHeaderComponent.bind(this)}
    getItemLayout={(data, index) => ({ index, length: ITEM_HEIGHT, offset: (ITEM_HEIGHT * index) })} />

Also I can dynamically update the positioning like so. 我也可以这样动态更新定位。 I add a function level const for calculating my item size like this: 我添加了一个函数级别const来计算我的项目大小,如下所示:

const ITEM_SIZE = this.state.isHorizontal ? ITEM_HEIGHT : this.props.width;

Then I updated my getItemLayout function to look like this: 然后我更新了我的getItemLayout函数,如下所示:

getItemLayout={(data, index) => ({ index, length: ITEM_SIZE, offset: ITEM_SIZE * index })} />

Try use horizontal inside flatlist tag, not value, it work for me :D 尝试使用水平内部flatlist标签,而不是值,它适用于我:D

<FlatList
    horizontal
    data={tab_ad}
    renderItem={(item) => this.renderItem(item.item)}
    keyExtractor={(item, index) => index}
    ={this.state}
/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM