React Native:试图在滚动条上隐藏搜索栏

kd12345

我目前正在尝试在滚动时隐藏搜索栏。

屏幕上显示的组件是顶部的搜索栏,然后是带有标签的水平滚动视图,然后是我的平面列表。

在当前的实现中,当我加载屏幕时,搜索栏将覆盖选项卡,而当我向下滚动时,搜索栏将隐藏并显示选项卡。

我想做的是,当我加载屏幕时,搜索栏,选项卡和列表的所有内容都应该可见,然后当我开始滚动搜索栏时隐藏,选项卡和列表的内容会填充顶部的可用空间。

另外,我遇​​到的另一个问题是当我不断向上滚动时,搜索栏会不断向上移动直到消失。

这是我的代码:

const scrollY = new Animated.Value(0)
const diffClamp=Animated.diffClamp(scrollY,0,100)
const translateY = diffClamp.interpolate({
inputRange:[0,100],
outputRange:[0,-100]
})

return(

<Animated.View style={{transform: 
    [{translateY:translateY}],elevation:4,zIndex:100}}>
    <SearchBar 
     containerStyle={{height:40,marginBottom:20,position:"absolute",
     left:0,right:0,top:0}} 
    inputContainerStyle={{height:40}}
    />
    </Animated.View>

    <View style={{paddingHorizontal:6}}>
    <ScrollView 
    horizontal 
    style={{
    marginBottom:10,
    height:30,
    }} 
    showsHorizontalScrollIndicator={false}
    >
    {categories.map(e=>(
            <TouchableOpacity 
            style={[
                {paddingHorizontal:10,paddingBottom:0},
                label===e.label && 
                {
                borderRadius:2,
                marginBottom:-20,
            }
                ]} 
                onPress={()=>}
                >
            <AppText>{e.label}</AppText>
            </TouchableOpacity>
    ))}
    </ScrollView>
    </View>
    <FlatList
      data={posts} // to have all the data
      keyExtractor={(post) => post.id.toString()}
      renderItem={({ item,index }) => (
       <Card
          title={item.title}
          subTitle={item.subTitle}
        />
      )}
      onScroll={(e)=>{
          scrollY.setValue(e.nativeEvent.contentOffset.y)
      }}
    />
)

位置:“绝对”,搜索栏位于选项卡的顶部,因此当我向下滚动搜索栏时,我尝试使用“相对”而不是位置:“绝对”,但选项卡上方有空白。

我还使用了react native elements搜索栏组件。

将不胜感激,在此先感谢您。

更新

在遵循了J.Doe解决方案(这是解决翻译问题的正确解决方案)之后,我现在面临与此相关的新问题。

由于我可以在scrollView的不同选项卡之间切换,因此每个选项卡都有其对应的Flatlist数据。如果FlatList包含3个以上的数据项,则该功能正常工作,但是如果包含1个或2个数据项,则转换功能将停止工作。

如果我位于选项卡1(具有许多数据项)上并向下滚动(即搜索栏已翻译),然后点击仅包含1个数据项的选项卡2,我将无法滚动屏幕来查看搜索栏。

这是我的代码:

const categories = [
{
  label: "Sports",
  id: 1,
},
{
  label: "News",
  id: 2,
},
{
  label: "Food",
  id: 3,
},
]

const[label,setLabel]=useState(categories[0]?.label)

const [currentCategoryId, setCurrentCategoryId] = useState(1)

const setLabelFilter=label=>{
setLabel(label)
}

const toggleBrands = (categoryId) => {
setCurrentCategoryId(categoryId)
};

这是我的scrollView:

<ScrollView 
    horizontal 
    style={{
    flexDirection:"row",
    alignContent:"center",
    marginTop: 20,// space between tabs and search bar
    height:30,
    }} 
    showsHorizontalScrollIndicator={false}
    >
    {categories.map((e)=>(
            <TouchableOpacity 
                key={e.id}
                onPress={()=>{
                    toggleBrands(e.id),
                    setLabelFilter(e.label)
                    }}
                selected={e.id === currentCategoryId}
                >
            <AppText style={[{fontWeight:"500"},label===e.label && {fontWeight:"700"}]}>{e.label}</AppText>
            </TouchableOpacity>
    ))}
    </ScrollView> 
莱里·格格扎兹
import React from 'react';
import {
  View,
  Animated,
  Text,
  StyleSheet,
  ScrollView,
  RefreshControl,
} from 'react-native';

const data = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10'];

const App = () => {
  const scrollY = React.useRef(new Animated.Value(0)).current;
  const diffClamp = Animated.diffClamp(scrollY, 0, 100);

  const translateY = diffClamp.interpolate({
    inputRange: [0, 100],
    outputRange: [0, -60],
    extrapolate: 'clamp',
  });

  const marginTop = diffClamp.interpolate({
    inputRange: [0, 100],
    outputRange: [0, -60],
    extrapolate: 'clamp',
  });

  const paddingTop = diffClamp.interpolate({
    inputRange: [0, 100],
    outputRange: [10, 110],
    extrapolate: 'clamp',
  });

  const opacity = diffClamp.interpolate({
    inputRange: [0, 100],
    outputRange: [1, 0],
    extrapolate: 'clamp',
  });

  const renderItem = ({item}) => {
    return (
      <View style={styles.card}>
        <Text>{`Card ${item}`}</Text>
      </View>
    );
  };

  return (
    <View style={{flex: 1, backgroundColor: 'red'}}>
      <Animated.View
        style={{
          zIndex: 100,
          paddingBottom: 10,
          transform: [{translateY}],
        }}>
        <Animated.View style={[styles.searchBar, {opacity}]}>
          <Text>Search Bar</Text>
        </Animated.View>
        <ScrollView horizontal showsHorizontalScrollIndicator={false}>
          {data.map((num) => {
            return (
              <View key={num} style={styles.tab}>
                <Text>Tab</Text>
              </View>
            );
          })}
        </ScrollView>
      </Animated.View>
      <Animated.FlatList
        style={{marginTop, paddingTop}}
        // contentContainerStyle={{paddingTop: 150}}
        refreshControl={
          <RefreshControl
            tintColor="#fff"
            onRefresh={() => {
              console.warn('Refreshing');
            }}
            refreshing={false}
          />
        }
        bounces={true}
        data={data}
        keyExtractor={(item) => item}
        scrollEventThrottle={16}
        renderItem={renderItem}
        onScroll={(e) => {
          if (e.nativeEvent.contentOffset.y > 0)
            scrollY.setValue(e.nativeEvent.contentOffset.y);
        }}
      />
    </View>
  );
};

const styles = StyleSheet.create({
  searchBar: {
    marginHorizontal: '5%',
    width: '90%',
    marginTop: 40,
    height: 40,
    borderRadius: 10,
    borderColor: 'lightgray',
    borderWidth: 1,
    backgroundColor: '#f4f4f4',
    justifyContent: 'center',
    alignItems: 'center',
  },
  tab: {
    justifyContent: 'center',
    alignItems: 'center',
    marginTop: 20,
    marginHorizontal: 20,
    width: 70,
    height: 30,
    borderRadius: 10,
    backgroundColor: 'pink',
  },
  card: {
    width: '90%',
    marginLeft: '5%',
    height: 100,
    borderRadius: 10,
    backgroundColor: 'yellow',
    marginBottom: 20,
    justifyContent: 'center',
    alignItems: 'center',
  },
});

export default App;

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

隐藏滚动条,但在react.js中保留滚动功能

来自分类Dev

物料设计清单:在滚动条上隐藏应用栏?

来自分类Dev

导航栏不会隐藏在滚动条上

来自分类Dev

React-native-tag-autocomplete:如何在建议列表中添加滚动条

来自分类Dev

在RecyclerView滚动条上隐藏工具栏会导致文本移到通知栏

来自分类Dev

滚动条上的固定侧栏

来自分类Dev

滚动条上的粘性导航栏

来自分类Dev

在React中修改滚动条而无需外部依赖

来自分类Dev

增加滚动条的大小 react-custom-scrollbar

来自分类Dev

隐藏滚动条上的工具栏将无法正常工作[CoordinatorLayout]

来自分类Dev

使用Headroom JS隐藏滚动条上的导航栏并在需要时显示

来自分类Dev

在片段内而不是活动内使用recyclerview时,在滚动条上隐藏工具栏

来自分类Dev

使用Headroom JS隐藏滚动条上的导航栏,并在需要时显示

来自分类Dev

在BottomSheet视图中隐藏滚动条上的工具栏

来自分类Dev

在React Native中隐藏导航栏

来自分类Dev

在React Native中隐藏createBottomTabNavigator标签栏

来自分类Dev

试图添加滚动条

来自分类Dev

隐藏RecyclerView滚动条

来自分类Dev

隐藏DIV的滚动条

来自分类Dev

滚动条未隐藏

来自分类Dev

滚动条部分隐藏

来自分类Dev

滚动条未隐藏

来自分类Dev

动态更改滚动条上的导航栏颜色

来自分类Dev

在水平滚动条上显示固定宽度的顶部栏

来自分类Dev

使用CSS在导航栏上创建“不可见”滚动条(其中隐藏不起作用)

来自分类Dev

在窗口滚动条上显示/隐藏div

来自分类Dev

隐藏仅在Windows上显示的无用滚动条

来自分类Dev

如何在表格上隐藏水平滚动条

来自分类Dev

底部导航在滚动条上隐藏Recycler视图问题

Related 相关文章

热门标签

归档