Reactjs 是否将每个ListView行设置为ListView容器的宽度和高度?
我想创建一个水平Reactjs 是否将每个ListView行设置为ListView容器的宽度和高度?,reactjs,react-native,Reactjs,React Native,我想创建一个水平列表视图,其中每一行都是列表视图容器的宽度和高度,它给人一种分页的感觉,屏幕上显示一行 问题是我不确定如何设置行的样式,使它们与列表视图的宽度和高度相匹配。有什么想法吗 import React, { Component } from 'react' import { StyleSheet, Text, View, ListView, TouchableHighlight } from 'react-native' import Spinner from
列表视图
,其中每一行都是列表视图
容器的宽度和高度,它给人一种分页的感觉,屏幕上显示一行
问题是我不确定如何设置行的样式,使它们与列表视图的宽度和高度相匹配。有什么想法吗
import React, { Component } from 'react'
import {
StyleSheet,
Text,
View,
ListView,
TouchableHighlight
} from 'react-native'
import Spinner from 'react-native-loading-spinner-overlay';
var ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
export default class ParcelView extends Component {
static navigationOptions = {
title: 'Parcel Screen',
};
constructor() {
super();
this.renderRow = this.renderRow.bind(this);
this.pressRow = this.pressRow.bind(this);
this.fetchData = this.fetchData.bind(this);
this.state = {
dataSource: ds.cloneWithRows([]),
loadingVisible: false
};
}
componentDidMount() {
this.fetchData();
}
render() {
const { navigate } = this.props.navigation;
return (
<View>
<Spinner visible={this.state.loadingVisible} textContent={"Loading..."} textStyle={{color: '#FFF'}} />
<View style={styles.listWrapper} >
<ListView
dataSource={this.state.dataSource}
renderRow={this.renderRow}
horizontal={true}
style={styles.listView}
/>
</View>
</View>
);
}
renderRow( rowData ){
return (
<TouchableHighlight onPress={() => this.pressRow()}>
<View>
<View style={styles.row}>
<Text style={styles.text}>{rowData.userId}</Text>
</View>
</View>
</TouchableHighlight>
);
}
pressRow( rowID: number ){
const { navigate } = this.props.navigation;
var text = "Testing";
// Navigate To Parcel Screen
navigate('Parcel', { name: 'Jane' });
}
fetchData(){
this.setState({loadingVisible: true});
fetch("https://jsonplaceholder.typicode.com/posts", {method: "GET"})
.then((response) => response.json())
.then((responseData) =>
{
this.setState({ dataSource: this.state.dataSource.cloneWithRows(responseData)});
})
.done(() => {
this.setState({loadingVisible: false});
});
}
}
var styles = StyleSheet.create({
listWrapper: {
padding: 10,
backgroundColor: '#F6F6F6',
borderWidth: 1,
borderColor: "#000",
height: 300,
margin: 10
},
row: {
backgroundColor: '#ccc',
borderWidth: 1,
borderColor: "#000",
alignSelf: 'stretch',
},
listView: {
backgroundColor: '#ccc',
}
});
import React,{Component}来自“React”
进口{
样式表,
文本,
看法
ListView,
可触摸高光
}从“反应本机”
从“react native loading Spinner overlay”导入微调器;
var ds=新的ListView.DataSource({rowHasChanged:(r1,r2)=>r1!==r2});
导出默认类ParcelView扩展组件{
静态导航选项={
标题:“包裹屏幕”,
};
构造函数(){
超级();
this.renderRow=this.renderRow.bind(this);
this.pressRow=this.pressRow.bind(this);
this.fetchData=this.fetchData.bind(this);
此.state={
数据源:ds.cloneWithRows([]),
加载可见:false
};
}
componentDidMount(){
这是fetchData();
}
render(){
const{navigate}=this.props.navigation;
返回(
);
}
renderRow(行数据){
返回(
this.pressRow()}>
{rowData.userId}
);
}
按Row(rowID:number){
const{navigate}=this.props.navigation;
var text=“测试”;
//导航到包裹屏幕
导航('包裹',{名称:'简'});
}
fetchData(){
this.setState({loadingVisible:true});
取回(“https://jsonplaceholder.typicode.com/posts“,{method:“GET”})
.then((response)=>response.json())
.然后((响应数据)=>
{
this.setState({dataSource:this.state.dataSource.cloneWithRows(responseData)});
})
.完成(()=>{
this.setState({loadingVisible:false});
});
}
}
var styles=StyleSheet.create({
列表包装器:{
填充:10,
背景颜色:“#F6F6F6”,
边框宽度:1,
边框颜色:“000”,
身高:300,
差额:10
},
行:{
背景颜色:“#ccc”,
边框宽度:1,
边框颜色:“000”,
自我定位:“拉伸”,
},
列表视图:{
背景颜色:“#ccc”,
}
});
您的ParcelView组件可以为ListView设置一个onLayout
道具。调用onLayout
时,可以将ListView的宽度和高度保存到状态。然后,当需要渲染箭头时,ParcelView可以为其渲染的行设置宽度
和高度
。
还要确保在ListView上将PaginEnabled
设置为true,以便您可以让本机ScrollView处理刷卡手势