0

嗨,我想水平显示具有 2 列的图像,我正在使用滚动视图,但我不知道该怎么做,我正在使用以下代码

获取api的代码

 componentDidMount(){
return fetch('https://www.json-generator.com/api/json/get/ccLAsEcOSq?indent=1')
  .then((response) => response.json())
  .then((responseJson) => {
    this.setState({
      isLoading: false,
      dataSource: responseJson.book_array,
    }, function(){
    });
  })
  .catch((error) =>{
    console.error(error);
  });

}

渲染代码

render() {
if (this.state.isLoading === true) {
  return <ActivityIndicator color={'red'} />;
}
return (
  <View style={styles.container}>
    <ScrollView horizontal={true}>
      {this.state.dataSource.map(item => this.renderItem(item))}
    </ScrollView>
  </View>
);

} }

renderItem 的代码

renderItem(item) {
return (
  <View style={{ margin: 5 }}>
    <View style={{
        backgroundColor: 'red',
        width: 150,
        height: 150,
        marginBottom: 1,
      }}>
       <Image  style={{ width: 150,height: 150}}
                      source={{uri: item.image}}/>  
    </View> 

    <View style={{
        backgroundColor: 'red',
        width: 150,
        height: 150,
        marginBottom: 1,
      }}>
       <Image  style={{ width: 150,height: 150}}
                      source={{uri: item.image}}/>  
    </View> 

  </View>
);}
4

5 回答 5

3

而不是ScrollViewtry FlatListwhich 提供numColumns道具,让您允许根据您的选择使用列。

取而代之的是,

<ScrollView horizontal={true}>
      {this.state.dataSource.map(item => this.renderItem(item))}
</ScrollView>

用这个,

<FlatList 
   data={this.state.dataSource}
   numColumns={2}
   renderItem={this.renderItem}
/>

有关详细信息,FlatList请参见此处的官方文档

于 2019-03-25T05:18:41.610 回答
1

试试 flex-direction 属性:

renderItem(item) {
return (
 <View style={{ margin: 5, flex: 1, flexDirection: "row", justifyContent: "space-around" }} >
    <View style={{ backgroundColor: "red", width: 150, height: 150, marginBottom: 1 }} >
      <Image style={{ width: 150, height: 150 }} source={{ uri: item.image }} />
    </View>
    <View style={{ backgroundColor: "red", width: 150, height: 150, marginBottom: 1 }} >
      <Image style={{ width: 150, height: 150 }} source={{ uri: item.image }} />
    </View>
  </View>
);}
于 2019-03-25T05:19:28.503 回答
1

要根据需要创建视图,需要实现自定义逻辑。在render函数中调用一个中间函数来获取两行中的列:

render() {
if (this.state.isLoading === true) {
  return <ActivityIndicator color={'red'} />;
}
return (
  <View style={styles.container}>
    <ScrollView horizontal={true}>
        {this.renderHorizantol(this.state.dataSource)}
    </ScrollView>
  </View>
);

renderHorizantol需要为偶数行或奇数行设置逻辑的函数中,我在以下索引上实现此功能dataSource Array

renderHorizantol = (dataSource) =>{
    let view = []
    for(let i=0 ; i < data.length ; i = i+2)
    {
        let subView = this.renderItem(dataSource[i],dataSource[i+1])
        view.push(subView)            
    }           
    return view 
}

renderItem函数中传递两个元素来绘制上下行内容:

renderItem(item1,item2) {
    let image1 = item1["imageUrl"]
    let image2 = item2 ? item2["imageUrl"] : null
    return (
        <View style={{ margin: 5 }}>
        <View style={{
            backgroundColor: 'red',
            width: 150,
            height: 150,
            marginBottom: 1,
            }}>
            <Image  style={{ width: 150,height: 150}}
                            source={{uri: image1}}/>  
        </View> 

        <View style={{
            backgroundColor: 'red',
            width: 150,
            height: 150,
            marginBottom: 1,
            }}>
            <Image  style={{ width: 150,height: 150}}
                            source={{uri: image2}}/>  
        </View>     
    </View>
);}
于 2019-03-25T09:48:45.040 回答
1

像这样在滚动视图中使用平面列表

<FlatList
    horizontal={true}
    data={this.state.dataSource}
    renderItem={({ item }) => (this.renderItem({item}))}
    />

像这样重新排列数据源

array1 = [obj1,obj2,obj3,obj4,obj5,obj6,obj7,
array2=[[obj1,obj2],[obj3,obj4],[obj5,obj6],[obj7,obj8]]

然后用 2 行渲染项目。

没有找到其他方法

于 2019-03-25T05:22:52.363 回答
1

修改你的 ScrollView 组件像这样:

<ScrollView horizontal={true} contentContainerStyle={{height:300, flexWrap:'wrap'}}>
{this.state.dataSource.map(item => this.renderItem(item))}
</ScrollView>
于 2019-03-25T06:44:21.290 回答