简体   繁体   English

React Native 浮动操作按钮 Touchable 不起作用

[英]React Native Floating action button Touchable not working

I have a FlatList and FAB on screen.我在屏幕上有一个 FlatList 和 FAB。 FAB is positioned absolute. FAB 是绝对定位的。 whenever I try to click on FAB a FlatList item behind fab gets clicked.每当我尝试点击 FAB 时,都会点击 fab 后面的 FlatList 项目。

When I remove the position absolute FAB works, I tried wrapping touchable inside View and also tried changing the view order but nothing worked当我删除 position absolute FAB works 时,我尝试在 View 内包装 touchable 并尝试更改视图顺序但没有任何效果

Attaching the screenshot附上截图

Note: it's working on iOS but Not on Android注意:它适用于 iOS 但不适用于 Android

import React from "react";
import {
  View,
  Text,
  FlatList,
  StyleSheet,
  Platform,
  Image,
  TouchableOpacity
} from "react-native";
import RF from "react-native-responsive-fontsize";
import {
  widthPercentageToDP as wp,
  heightPercentageToDP as hp
} from "react-native-responsive-screen";

import AddIcon from "../../libs/fabActions/AddIcon";

export default class TemplateScreen extends React.Component {
  constructor(props) {
    super(props);

    this.state = {
      data: [{}, {}, {}, {}, {}, {}, {}, {}, {}, {}]
    };
  }

  keyExtractor = (item, index) => {
    return index.toString();
  };

  renderItem = ({ item, index }) => {
    const { navigate } = this.props.navigation;

    return (
      <TouchableOpacity>
        <View style={styles.item}>
          <Text style={styles.name}>Christmas Template</Text>
        </View>
      </TouchableOpacity>
    );
  };

  render() {
    const { navigate } = this.props.navigation;

    return (
      <View style={styles.container}>
        <FlatList
          contentContainerStyle={styles.list}
          data={this.state.data}
          extraData={this.state}
          renderItem={this.renderItem}
          keyExtractor={this.keyExtractor}
        />

          <TouchableOpacity>
            <View style={styles.addButton}>
              <AddIcon width={wp(4)} height={wp(4)} />
            </View>
          </TouchableOpacity>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: "#f0f2ff"
  },
  item: {
    backgroundColor: "#ffffff",
    marginHorizontal: "4%",
    marginVertical: "1.2%",
    paddingHorizontal: "4.5%",
    paddingVertical: "5%",
    ...Platform.select({
      ios: {
        shadowColor: "rgba(71, 75, 165, 0.1)",
        shadowOffset: {
          width: 0,
          height: 1
        },
        shadowRadius: 9.7,
        shadowOpacity: 1
      },
      android: {
        elevation: 1
      }
    })
  },
  name: {
    fontSize: RF(2.3),
    color: "#474ba5"
  },
  addButton: {
    position: "absolute",
    bottom: 20,
    right: 30,
    width: 46,
    height: 46,
    backgroundColor: "#ffffff",
    borderRadius: 46,
    alignContent: "center",
    justifyContent: "center",
    zIndex: 10,
    ...Platform.select({
      ios: {
        shadowColor: "rgba(71, 75, 165, 0.1)",
        shadowOffset: {
          width: 0,
          height: 1
        },
        shadowRadius: 9.7,
        shadowOpacity: 1
      },
      android: {
        elevation: 4
      }
    })
  },
  list: {
    paddingBottom: hp(8),
    paddingTop: "2%"
  }
});

the thing to understand is in Android if the absolute positioned button is outside its parent it will not catch events.需要了解的是在 Android 中,如果绝对定位按钮在其父级之外,它将不会捕获事件。 You can verify this hypothesis by making a square view and float the button in such a way that half of it is outside that view, and the other half is inside the view.您可以通过制作一个方形视图并浮动按钮来验证这个假设,使其一半在该视图之外,另一半在该视图内。 when u click on the half that is inside the parent view, the event will trigger;当您单击父视图内的一半时,事件将触发; but if u click on the half outside the parent view it will not work.但是如果你点击父视图之外的一半,它将不起作用。

so the conclusion is to always make sure that the button is inside the parent view.所以结论是始终确保按钮在父视图内。 in the case of floating action button, if u want it to work u need to hook it up to the root view of the screen, or in other words it should be a direct child of the root view of the screen.在浮动操作按钮的情况下,如果你想让它工作,你需要将它连接到屏幕的根视图,或者换句话说,它应该是屏幕根视图的直接子视图。 sure u will make it last child also to not be hidden.确定你会让它成为最后一个孩子也不会被隐藏。 this way u will be sure it always inside the parent view.这样你就可以确保它总是在父视图中。 another things to look to is if u create a project in android studio say to make native app, and u select the option with floating action button, u will see that the FAB is a direct child of the ConstraintLayout container which validate what I'm saying here.另一件要注意的事情是,如果你在 android studio 中创建一个项目说要制作本机应用程序,并且你选择带有浮动操作按钮的选项,你会看到 FAB 是 ConstraintLayout 容器的直接子级,它验证了我的内容在这里说。

In The case provided in the question the TouchableOpacity who needs to be positioned absolutely not the view inside it在问题中提供的情况下,绝对需要定位的 TouchableOpacity 而不是其中的视图

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

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