簡體   English   中英

使用React Native查看列表中的項目

[英]Look of items in list with react native

我的第一個反應原生應用程序列出了印度的熱門廣告,並提供了指向該項目網上詳細信息的超鏈接。

在此處輸入圖片說明

以下是打印項目的代碼部分。

<View style={{flex: 1, paddingTop:20}}> <FlatList data={this.state.dataSource} renderItem={({item}) => <Text style={styles.TextStyle} onPress={ ()=> Linking.openURL(item.url) } >{item.title}</Text> } keyExtractor={(item, index) => index} /> </View>

我想學習如何設置列表中項目的樣式,以使其看起來更好。 該項目的Web版本具有以下外觀列表

在此處輸入圖片說明

如何設置樣式以使其看起來更有吸引力,更類似於Web外觀並清楚地顯示可單擊項?

import _ from 'lodash';

import Expo from 'expo';
import React, { Component } from 'react';
import {FlatList, ActivityIndicator,
  View,
  ScrollView,
  StyleSheet,
  Image,
  TouchableHighlight,
  ListView,Linking
} from 'react-native';

import {
  Text,
  Card,
  ButtonGroup,
  Tile,
  Col,
  Row,
  Icon,
  List,
  ListItem,
  Avatar
} from 'react-native-elements';

import colors from 'HSColors';

const users = [
  {
    name: 'brynn',
    avatar: 'https://s3.amazonaws.com/uifaces/faces/twitter/brynn/128.jpg',
  },
  {
    name: 'thot leader',
    avatar: 'https://s3.amazonaws.com/uifaces/faces/twitter/evagiselle/128.jpg',
  },
  {
    name: 'jsa',
    avatar: 'https://s3.amazonaws.com/uifaces/faces/twitter/jsa/128.jpg',
  },
  {
    name: 'talhaconcepts',
    avatar: 'https://s3.amazonaws.com/uifaces/faces/twitter/talhaconcepts/128.jpg',
  },
  {
    name: 'andy vitale',
    avatar: 'https://s3.amazonaws.com/uifaces/faces/twitter/andyvitale/128.jpg',
  },
  {
    name: 'katy friedson',
    avatar: 'https://s3.amazonaws.com/uifaces/faces/twitter/kfriedson/128.jpg',
  },
];

const log = () => console.log('this is an example method');

const list1 = [
  {
    title: 'Appointments',
    icon: 'av-timer',
  },
  {
    title: 'Trips',
    icon: 'flight-takeoff',
  },
  {
    title: 'Passwords',
    icon: 'fingerprint',
  },
  {
    title: 'Pitches',
    icon: 'lightbulb-outline',
  },
  {
    title: 'Updates',
    icon: 'track-changes',
  },
];

const list2 = [
  {
    name: 'Amy Farha',
    avatar_url: 'https://s3.amazonaws.com/uifaces/faces/twitter/ladylexy/128.jpg',
    subtitle: 'Vice President',
  },
  {
    name: 'Chris Jackson',
    avatar_url: 'https://s3.amazonaws.com/uifaces/faces/twitter/adhamdannaway/128.jpg',
    subtitle: 'Vice Chairman',
  },
  {
    name: 'Amanda Martin',
    avatar_url: 'https://s3.amazonaws.com/uifaces/faces/twitter/brynn/128.jpg',
    subtitle: 'CEO',
  },
  {
    name: 'Christy Thomas',
    avatar_url: 'https://s3.amazonaws.com/uifaces/faces/twitter/kfriedson/128.jpg',
    subtitle: 'Lead Developer',
  },
  {
    name: 'Melissa Jones',
    avatar_url: 'https://s3.amazonaws.com/uifaces/faces/twitter/nuraika/128.jpg',
    subtitle: 'CTO',
  },
];

const list3 = [
  {
    image_url: 'https://s3.amazonaws.com/uifaces/faces/twitter/ladylexy/128.jpg',
    icon: null,
    title: null,
  },
  {
    image_url: 'https://s3.amazonaws.com/uifaces/faces/twitter/adhamdannaway/128.jpg',
    icon: null,
    title: null,
  },
  {
    image_url: null,
    icon: null,
    title: 'LR',
  },
  {
    image_url: null,
    icon: {name: 'user', type: 'font-awesome'},
    title: null,
  },
  {
    image_url: null,
    icon: {name: 'user-female', type: 'simple-line-icon'},
    title: null,
  },
  {
    image_url: null,
    icon: {name: 'baidu', type: 'entypo'},
    title: null,
  },
]

class Icons extends Component {
  constructor() {
    super();
    const ds = new ListView.DataSource({
      rowHasChanged: (r1, r2) => r1 !== r2,
    });

    this.state = {
      selectedIndex: 0,
      value: 0.5,
      dataSource: ds.cloneWithRows(list1),
        isLoading: true
    };

    this.updateIndex = this.updateIndex.bind(this);
    this.renderRow = this.renderRow.bind(this);
  }

  updateIndex(selectedIndex) {
    this.setState({ selectedIndex });
  }

  renderRow(rowData, sectionID) {
    return (
      <ListItem
        key={sectionID}
        onPress={log}
        title={rowData.title}
        icon={{ name: rowData.icon }}
      />
    );
  }
    componentDidMount(){
        return fetch('https://www.koolbusiness.com/in.json')
            .then((response) => response.json())
            .then((responseJson) => {

                this.setState({
                    isLoading: false,
                    dataSource: responseJson.movies,
                }, function(){

                });

            })
            .catch((error) =>{
                console.error(error);
            });
    }
  render() {
      if(this.state.isLoading){
          return(
              <View style={{flex: 1, padding: 20}}>
                  <ActivityIndicator/>
              </View>
          )
      }
    const { navigation } = this.props;
    const buttons = ['Button1', 'Button2'];
    const { selectedIndex } = this.state;
      if(this.state.isLoading){
          return(
              <View style={{flex: 1, padding: 20}}>
                  <ActivityIndicator/>
              </View>
          )
      }
    return (
      <ScrollView>
        <View style={styles.headerContainer}>
          <Icon color="white" name="invert-colors" size={62} />
          <Text style={styles.heading}>Trending Ads India</Text>
        </View>
          <View style={styles.MainContainer}>


          </View>
          <View style={{flex: 1, paddingTop:20}}>
              <FlatList
                  data={this.state.dataSource}
                  renderItem={({item}) =>      <Text style={styles.TextStyle} onPress={ ()=> Linking.openURL(item.url) } >{item.title}</Text>
                  }
                  keyExtractor={(item, index) => index}
              />
          </View>

      </ScrollView>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
  },
  headerContainer: {
    justifyContent: 'center',
    alignItems: 'center',
    padding: 40,
    backgroundColor: '#FD6B78'
  },
  heading: {
    color: 'white',
    marginTop: 10,
    fontSize: 22,
  },
  fonts: {
    marginBottom: 8,
  },
  user: {
    flexDirection: 'row',
    marginBottom: 6,
  },
  image: {
    width: 30,
    height: 30,
    marginRight: 10,
  },
  name: {
    fontSize: 16,
    marginTop: 5,
  },
  social: {
    flexDirection: 'row',
    justifyContent: 'center',
  },
  subtitleView: {
    flexDirection: 'row',
    paddingLeft: 10,
    paddingTop: 5,
  },
  ratingImage: {
    height: 19.21,
    width: 100,
  },
  ratingText: {
    paddingLeft: 10,
    color: 'grey',
  },
});

export default Icons;

請嘗試這個。 它會幫助你。

<View style={styles.mainWrapper} >
            <FlatList data={this.state.data} renderItem={this._renderList} />

        </View>

_Renderlist將呈現您的項目。 您可以操縱它來滿足您的需求。

_renderList = ({ item }) => {
    return (
        <TouchableWithoutFeedback onPress={(event) => this._selectedItem(item.key)}>
            <View style={styles.listRowContainer}>
                <View style={styles.listinside1Container}>
                    <Image style={styles.listImage} source={item.icon} />
                    <View style={styles.listContainer} onPress={(event) => this._selectedItem(item.text)}  >
                        <Text style={styles.listHeader} >{item.header}</Text>
                        <Text style={styles.listValue} >{item.value}</Text>
                    </View>
                </View>
                <Image style={styles.listimgArrow} source={require('../../../resources/toolbar/chevron_right_grey.png')} />
            </View>
        </TouchableWithoutFeedback>
    );

}

在渲染列表中,您可以創建供您使用的項目。

請告訴我。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM