React-native分享類(lèi)似美團(tuán)下拉菜單Demo

上圖:


image.png

這個(gè)Demo我已經(jīng)做了一些處理,所以,有需要的朋友可以自己更改
下面放出源碼

/**
 * 
 * 皓天
 * @memberOf ActionBar
 */
import React, { Component } from "react";
import {
  View,
  Text,
  TouchableHighlight,
  Image,
  TouchableOpacity,
  ScrollView,
  Animated,
  Easing,
  StyleSheet
} from "react-native";
import PropTypes from "prop-types";

class ActionBar extends Component {
  constructor(props, context) {
    super(props, context);

    var selectIndex = new Array(this.props.data.length);
    for (var i = 0; i < selectIndex.length; i++) {
      selectIndex[i] = 0;
    }
    this.state = {
      activityIndex: -1,
      selectIndex: selectIndex,
      rotationAnims: props.data.map(() => new Animated.Value(0))
    };

    this.defaultConfig = {
      bgColor: "grey",
      tintColor: "#333333",
      activityTintColor: "red",
      arrowImg: require("./img/dropdown_arrow.png"),
      checkImage: require("./img/menu_check.png")
    };
  }

  renderChcek(index, title) {
    var activityIndex = this.state.activityIndex;
    if (this.state.selectIndex[activityIndex] == index) {
      var checkImage = this.props.checkImage
        ? this.props.checkImage
        : this.defaultConfig.checkImage;
      return (
        <View
          style={{
            flex: 1,
            justifyContent: "space-between",
            alignItems: "center",
            paddingHorizontal: 15,
            flexDirection: "row"
          }}
        >
          <Text
            style={[
              styles.item_text_style,
              this.props.optionTextStyle,
              {
                color: this.props.activityTintColor
                  ? this.props.activityTintColor
                  : this.defaultConfig.activityTintColor
              }
            ]}
          >
            {title}
          </Text>
          <Image
            source={checkImage}
            style={{
              tintColor: this.props.activityTintColor
                ? this.props.activityTintColor
                : this.defaultConfig.activityTintColor
            }}
          />
        </View>
      );
    } else {
      return (
        <View
          style={{
            flex: 1,
            justifyContent: "space-between",
            alignItems: "center",
            paddingHorizontal: 15,
            flexDirection: "row"
          }}
        >
          <Text
            style={[
              styles.item_text_style,
              this.props.optionTextStyle,
              {
                color: this.props.tintColor
                  ? this.props.tintColor
                  : this.defaultConfig.tintColor
              }
            ]}
          >
            {title}
          </Text>
        </View>
      );
    }
  }

  renderActivityPanel() {
    if (this.state.activityIndex >= 0) {
      var currentTitles = this.props.data[this.state.activityIndex];

      var heightStyle = {};
      if (
        this.props.maxHeight &&
        this.props.maxHeight < currentTitles.length * 44
      ) {
        heightStyle.height = this.props.maxHeight;
      }

      return (
        <View
          style={{
            position: "absolute",
            left: 0,
            right: 0,
            top: 40,
            bottom: 0
          }}
        >
          <TouchableOpacity
            onPress={() => this.openOrClosePanel(this.state.activityIndex)}
            activeOpacity={1}
            style={{
              position: "absolute",
              left: 0,
              right: 0,
              top: 0,
              bottom: 0
            }}
          >
            <View style={{ opacity: 0.4, backgroundColor: "black", flex: 1 }} />
          </TouchableOpacity>

          <ScrollView
            style={[
              {
                position: "absolute",
                top: 0,
                left: 0,
                right: 0,
                backgroundColor: "white"
              },
              heightStyle
            ]}
          >
            {currentTitles.map((title, index) => (
              <TouchableOpacity
                key={index}
                activeOpacity={1}
                style={{ flex: 1, height: 44 }}
                onPress={this.itemOnPress.bind(this, index)}
              >
                {this.renderChcek(index, title)}
                <View
                  style={{
                    backgroundColor: "#F6F6F6",
                    height: 1,
                    marginLeft: 15
                  }}
                />
              </TouchableOpacity>
            ))}
          </ScrollView>
        </View>
      );
    } else {
      return null;
    }
  }

  openOrClosePanel(index) {
    this.props.bannerAction ? this.props.bannerAction() : null;

    if (this.state.activityIndex == index) {
      this.closePanel(index);
      this.setState({
        activityIndex: -1
      });
    } else {
      if (this.state.activityIndex > -1) {
        this.closePanel(this.state.activityIndex);
      }
      this.openPanel(index);
      this.setState({
        activityIndex: index
      });
    }
  }

  openPanel(index) {
    Animated.timing(this.state.rotationAnims[index], {
      toValue: 0.5,
      duration: 300,
      easing: Easing.linear
    }).start();
  }

  closePanel(index) {
    Animated.timing(this.state.rotationAnims[index], {
      toValue: 0,
      duration: 300,
      easing: Easing.linear
    }).start();
  }

  split(data){
    return data.length > 6 ? data.substring(0,6) + '...' : data
  }

  itemOnPress(index) {
    if (this.state.activityIndex > -1) {
      var selectIndex = this.state.selectIndex;
      selectIndex[this.state.activityIndex] = index;
      this.setState({
        selectIndex: selectIndex
      });
      if (this.props.handler) {
        this.props.handler(this.state.activityIndex, index);
      }
    }
    this.openOrClosePanel(this.state.activityIndex);
  }

  renderDropDownArrow(index) {
    var icon = this.props.arrowImg
      ? this.props.arrowImg
      : this.defaultConfig.arrowImg;
    return (
      <Animated.Image
        source={icon}
        style={{
          width: 6,
          height: 4,
          marginLeft: 8,
          tintColor:
            index === this.state.activityIndex
              ? this.props.activityTintColor
                ? this.props.activityTintColor
                : this.defaultConfig.activityTintColor
              : this.props.tintColor
                ? this.props.tintColor
                : this.defaultConfig.tintColor,
          transform: [
            {
              rotateZ: this.state.rotationAnims[index].interpolate({
                inputRange: [0, 1],
                outputRange: ["0deg", "360deg"]
              })
            }
          ]
        }}
      />
    );
  }

  render() {
    
    return (
      <View style={{ flexDirection: "column", flex: 1 }}>
        <View
          style={{
            flexDirection: "row",
            backgroundColor: this.props.bgColor
              ? this.props.bgColor
              : this.defaultConfig.bgColor
          }}
        >
          {this.props.data.map((rows, index) => (
            <TouchableOpacity
              activeOpacity={1}
              onPress={this.openOrClosePanel.bind(this, index)}
              key={index}
              style={{
                flex: 1,
                height: 48,
                alignItems: "center",
                justifyContent: "center"
              }}
            >
              <View
                style={{
                  flexDirection: "row",
                  alignItems: "center",
                  justifyContent: "center",
                }}
              >
                <Text
                  style={[
                    styles.title_style,
                    this.props.titleStyle,
                    {
                      color:
                        index === this.state.activityIndex
                          ? this.props.activityTintColor
                            ? this.props.activityTintColor
                            : this.defaultConfig.activityTintColor
                          : this.props.tintColor
                            ? this.props.tintColor
                            : this.defaultConfig.tintColor
                    }
                  ]}
                >
                  {/* {rows[this.state.selectIndex[index]]} */}
                  {this.split(rows[this.state.selectIndex[index]])}
                </Text>
                {this.renderDropDownArrow(index)}
              </View>
            </TouchableOpacity>
          ))}
        </View>
        {this.props.children}

        {this.renderActivityPanel()}
      </View>
    );
  }
}

ActionBar.propTypes = {
  bgColor: PropTypes.string,
  tintColor: PropTypes.string,
  activityTintColor: PropTypes.string,
  arrowImg: PropTypes.number,
  checkImage: PropTypes.number,
  data: PropTypes.array,
  bannerAction: PropTypes.func,
  optionTextStyle: PropTypes.object,
  titleStyle: PropTypes.object,
  maxHeight: PropTypes.number
};

const styles = StyleSheet.create({
  title_style: {
    fontSize: 16
  },
  item_text_style: {
    color: "#333333",
    fontSize: 16
  }
});

export default ActionBar;

下面是使用方法

  render() {
    var data = [["第一項(xiàng)目項(xiàng)目項(xiàng)目", "第二項(xiàng)目","1111111111"], ["第三項(xiàng)目", "第四項(xiàng)目","22222222222222"]];
    return (
      <View style={styles.container}>
        <ActionBar
          style={{ flex: 1 }}
          bgColor={"white"}
          tintColor={"#000000"}
          activityTintColor={"#3BB4F2"}
          // arrowImg={}
          // checkImage={}
          optionTextStyle={{ color: "#333333" }}
          titleStyle={{color: '#3BB4F2'}}
          // maxHeight={300}
          handler={(selection, row) =>
            // this.setState({ text: data[selection][row] })
            (HomeStore.firstName = data[selection][row])
          }
          data={data}
          // data={HomeStore.dataTest}
        >
          <View style={{ flex: 1 }}>
            <Text>{HomeStore.firstName} -------這里是選擇的內(nèi)容</Text>
          </View>
        </ActionBar>
      </View>
    );
  }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子搓扯,更是在濱河造成了極大的恐慌摧扇,老刑警劉巖捂敌,帶你破解...
    沈念sama閱讀 218,682評(píng)論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件蟹演,死亡現(xiàn)場(chǎng)離奇詭異阴颖,居然都是意外死亡愤诱,警方通過(guò)查閱死者的電腦和手機(jī)云头,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,277評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)淫半,“玉大人溃槐,你說(shuō)我怎么就攤上這事】瓶裕” “怎么了竿痰?”我有些...
    開(kāi)封第一講書(shū)人閱讀 165,083評(píng)論 0 355
  • 文/不壞的土叔 我叫張陵脆粥,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我影涉,道長(zhǎng)变隔,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,763評(píng)論 1 295
  • 正文 為了忘掉前任蟹倾,我火速辦了婚禮匣缘,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘鲜棠。我一直安慰自己肌厨,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,785評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布豁陆。 她就那樣靜靜地躺著柑爸,像睡著了一般。 火紅的嫁衣襯著肌膚如雪盒音。 梳的紋絲不亂的頭發(fā)上表鳍,一...
    開(kāi)封第一講書(shū)人閱讀 51,624評(píng)論 1 305
  • 那天,我揣著相機(jī)與錄音祥诽,去河邊找鬼譬圣。 笑死,一個(gè)胖子當(dāng)著我的面吹牛雄坪,可吹牛的內(nèi)容都是我干的厘熟。 我是一名探鬼主播,決...
    沈念sama閱讀 40,358評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼维哈,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼绳姨!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起阔挠,我...
    開(kāi)封第一講書(shū)人閱讀 39,261評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤就缆,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后谒亦,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,722評(píng)論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡空郊,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,900評(píng)論 3 336
  • 正文 我和宋清朗相戀三年份招,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片狞甚。...
    茶點(diǎn)故事閱讀 40,030評(píng)論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡锁摔,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出哼审,到底是詐尸還是另有隱情谐腰,我是刑警寧澤孕豹,帶...
    沈念sama閱讀 35,737評(píng)論 5 346
  • 正文 年R本政府宣布,位于F島的核電站十气,受9級(jí)特大地震影響励背,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜砸西,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,360評(píng)論 3 330
  • 文/蒙蒙 一叶眉、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧芹枷,春花似錦衅疙、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,941評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至走芋,卻和暖如春绩郎,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背绿聘。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,057評(píng)論 1 270
  • 我被黑心中介騙來(lái)泰國(guó)打工嗽上, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人熄攘。 一個(gè)月前我還...
    沈念sama閱讀 48,237評(píng)論 3 371
  • 正文 我出身青樓兽愤,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親挪圾。 傳聞我的和親對(duì)象是個(gè)殘疾皇子浅萧,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,976評(píng)論 2 355