在阿拉伯语从右到左,ReactNative FlatList无法正确呈现

时间:2018-05-29 10:25:45

标签: android react-native react-native-flatlist

我有一个FlatList来显示用户列表,当我将设备语言更改为阿拉伯语时,RTL未正确呈现。箭头应指向另一个方向。

以下是它在移动设备上的外观截图。 enter image description here

你能否建议我是否遗漏了什么。

这是我的代码。

FlatListScreen.js

    import React, {Component} from 'react';
import {View, Text, FlatList} from 'react-native';
import {List, ListItem, SearchBar} from 'react-native-elements';

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

        this.state = {
            loading: false,
            date: [],
            page: 1,
            seed: 1,
            error: null,
            refreshing: false
        };
    }

    componentDidMount(){
        this.makeRemoteRequest();
    }

    makeRemoteRequest = () => {
        const {page, seed } = this.state;
        const url = 'https://randomuser.me/api/?seed=${seed}&page=${page}&results=20';
        this.setState({loading: true});
        fetch(url)
            .then(res => res.json())
            .then(res => {
                this.setState({
                    data: page === 1 ? res.results : [...this.state.data, ...res.results],
                    error: res.error || null,
                    loading: false,
                    refreshing: false
                });
            })
            .catch(error => {
                this.setState({error, loading: false});
            });
    };

    renderSeparator = () => {
        return (
            <View
                style={{
                    height: 1,
                    width: '86%',
                    backgroundColor: '#CED0CE',
                    marginLeft: '14%',
                }}
            />
        );
    };

    renderFooter = () => {
        if(!this.state.loading) return null;

        return (
            <View
                style={{
                    paddingVertical: 20,
                    borderTopWidth: 1,
                    borderTopColor: "#CED0CE"
                }}
            />
        )
    };

    renderHeader = () => {
        return <SearchBar 
        placeholder="Search.."
        lightTheme
        round />;
    }

    onLearnMore = (item) => {
        this.props.navigation.navigate('Profile', { ...item });
      };

    render (){
        return (
            <List containerStyle={{ borderTopWidth: 0, borderBottomWidth: 0}}>
                <FlatList
                    data = {this.state.data}
                    renderItem={({item}) => (
                        <ListItem
                            roundAvatar
                            title={`${item.name.first} ${item.name.last}`}
                            subtitle={item.email}
                            avatar={{uri: item.picture.thumbnail}}
                            containerStyle={{ borderBottomWidth: 0 }}
                            onPress={() => this.onLearnMore(item)}
                        />
                    )} 
                    keyExtractor = {item => item.email}
                    ItemSeparatorComponent={this.renderSeparator}
                    ListHeaderComponent={this.renderHeader}
                    ListFooterComponent={this.renderFooter}
                />
            </List>
        )
    }
    }

    export default FlatListScreen;

从React-native文档中,我添加了以下RTL支持代码 https://facebook.github.io/react-native/blog/2016/08/19/right-to-left-support-for-react-native-apps.html#writing-rtl-ready-components

MainActivity.java

 @Override
    protected String getMainComponentName() {
        I18nUtil sharedI18nUtilInstance = I18nUtil.getInstance();
        sharedI18nUtilInstance.allowRTL(context, true);
        return "navapp";
    }

的AndroidManifest.xml

<application
      android:name=".MainApplication"
      android:label="@string/app_name"
      android:icon="@mipmap/ic_launcher"
      android:allowBackup="false"
      android:theme="@style/AppTheme"
      android:supportsRtl="true">

1 个答案:

答案 0 :(得分:1)

FlatList中显示的箭头来自您用于显示数据中每条记录的ListItem组件。您需要检查当前语言/ RTL支持并相应地渲染箭头。

要实现所需的行为,您可以对ListItem组件使用rightIcon属性。

  

<强> rightIcon

     右图标的

图标配置(可选),来自的名称   图标库(如材质)或像Image一样的React Native元素。   除非设置了hideChevron,否则显示

<强>示例

<ListItem
  roundAvatar
  title={'title'}
  subtitle={'sub title'}
  avatar={{uri: item.picture.thumbnail}}
  containerStyle={{ borderBottomWidth: 0 }}
  onPress={() => this.onLearnMore(item)}
  rightIcon={{name: (I18nManager.isRTL ? 'chevron-left': 'chevron-right')}}
/>