简体   繁体   English

如何使用嵌套滚动视图(如 twitter 或 instagram 配置文件屏幕 [REACT-NATIVE])创建粘性选项卡选择器

[英]How to create a sticky tab selector with nested scrollviews like twitter or instagram profile screens [REACT-NATIVE]

I'm trying to create a ScrollView which contains one sticky selector, that allow the selection between two nested ScollViews.我正在尝试创建一个包含一个粘性选择器的 ScrollView,它允许在两个嵌套的 ScollViews 之间进行选择。 It's like the twitter profile screen, or the instagram screen, where you can switch between my posts and posts where I was tagged.就像 twitter 个人资料屏幕或 instagram 屏幕一样,您可以在我的帖子和我被标记的帖子之间切换。 Now my problem actually is that this two nested ScollViews, let's say "MY POSTS" and "TAGGED" could have different sizes, but the RootScrollView consider only the biggest height of the two scrollviews, so if in the first I've 20 items, and let's say height=1000, in the second if I don't have items, or less items, I'll have an empty space y offset like the first.现在我的问题实际上是这两个嵌套的 ScollViews,假设“MY POSTS”和“TAGGED”可能有不同的大小,但是 RootScrollView 只考虑两个滚动视图的最大高度,所以如果在第一个我有 20 个项目,假设高度 = 1000,如果我没有物品或更少的物品,我将有一个空白空间 y 偏移量,就像第一个一样。

I know it's not so clear, but if you open instagram or twitter profile screens you'll immediately get it, the problem of the different heights.我知道这不是很清楚,但是如果你打开 instagram 或 twitter 个人资料屏幕你会立即得到它,不同高度的问题。

Now as you'll see, what I've tried to do is create a RootScrollView, put inside it two views, the header and the sticky selector, in twitter it's the "Tweet", "Tweets and replies"... , and the a NestedScrollView which initially has scrollEnabled=false, and then, by scroll the root I'll update it to true and to false the root one.现在您将看到,我尝试创建一个 RootScrollView,在其中放入两个视图,header 和粘性选择器,在 twitter 中,它是“推文”、“推文和回复”......最初具有scrollEnabled = false的NestedScrollView,然后,通过滚动根,我将其更新为true并将根更新为false。 But it seems not to work correctly.但它似乎无法正常工作。

推特个人资料 代码说明

Here's the code:这是代码:

const HEADER_HEIGHT = height / 3;
const STIKY_SELECTOR_HEIGHT = 100;

const App = () => {
  const rootScrollRef = useRef();
  const nestedScrollRef = useRef();

  const [offset, setOffset] = useState(0);
  const [scrollEnabled, setScrollEnabled] = useState(false);

  const onRootScroll = ({
    nativeEvent: {
      contentOffset: { y },
    },
  }) => {
    const direction = y > offset ? "down" : "up";
    setOffset(y);

    if (y > HEADER_HEIGHT - 10 && direction == "down") {
      setScrollEnabled(true);
    }
  };

  const onNestedScroll = ({
    nativeEvent: {
      contentOffset: { y },
    },
  }) => {
    if (y < 20) setScrollEnabled(false);
  };

  const renderItem = () => {
    return <View style={styles.cell} />;
  };

  return (
    <View style={{ flex: 1 }}>
      {/* ROOT SCROLLVIEW */}

      <ScrollView
        simultaneousHandlers={nestedScrollRef}
        scrollEventThrottle={16}
        ref={rootScrollRef}
        onScroll={onRootScroll}
        stickyHeaderIndices={[1]}
        scrollEnabled={!scrollEnabled}
        style={{ flex: 1, backgroundColor: "gray" }}
      >
        {/* HEADER */}

        <View
          style={{ width, height: HEADER_HEIGHT, backgroundColor: "darkblue" }}
        ></View>

        {/* STIKY SELECTOR VIEW */}
        <View
          style={{ height: STIKY_SELECTOR_HEIGHT, backgroundColor: "red" }}
        ></View>

        {/* NESTED SCROLLVIEW */}

        <View style={{ height: height - STIKY_SELECTOR_HEIGHT }}>
          <FlatList
            data={[1, 2, 3, 4, 5, 6, 7]}
            ref={nestedScrollRef}
            scrollEventThrottle={16}
            onScroll={onNestedScroll}
            scrollEnabled={scrollEnabled}
            renderItem={renderItem}
            numColumns={2}
            contentContainerStyle={{
              justifyContent: "space-between",
            }}
          />
        </View>
      </ScrollView>
    </View>
  );
};

If someone is facing the same problem there a component for that react-native-collapsible-tab-view如果有人面临同样的问题,那么该react-native-collapsible-tab-view的组件

<Tabs.Container
  renderHeader={Header}
  headerHeight={HEADER_HEIGHT} // optional>
  <Tabs.Tab name="A">
    <Tabs.FlatList
      data={DATA}
      renderItem={renderItem}
      keyExtractor={identity}
    />
  </Tabs.Tab>
  <Tabs.Tab name="B">
    <Tabs.ScrollView>
      <View style={[styles.box, styles.boxA]} />
      <View style={[styles.box, styles.boxB]} />
    </Tabs.ScrollView>
  </Tabs.Tab>
</Tabs.Container>

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

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