Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

createMaterialTopTabNavigator inside ScrollView with dynamic height

Is there any way to create something like a ScrollView with dynamic height?

Details on what we're trying to do:

We created a Top Tab Bar (using createMaterialTopTabNavigator) within a ScrollView. Everything works fine, except the height of the ScrollView. Let's assume there are 3 tab screens with different heights: TabScreen1: 800, TabScreen2: 400, TabScreen3: 300... At rendering, the ScrollView takes the greatest height, and when Tab2 or 3 is selected, the height of our ScrollView remains at 800, so there is empty space for scrolling in Tab2 and 3. enter image description here

like image 514
M_droid Avatar asked Mar 27 '20 15:03

M_droid


People also ask

How do I add height to ScrollView?

In order to bound the height of a ScrollView, either set the height of the view directly (discouraged) or make sure all parent views have bounded height. Forgetting to transfer {flex: 1} down the view stack can lead to errors here, which the element inspector makes quick to debug.

Is FlatList better than ScrollView?

As opposed to the ScrollView, the FlatList renders only those elements that are currently being displayed on the screen (default: 10 items). Thus, it does not have any impact on the performance of the application. So, it is preferable to use the FlatList Component to display a large list of data.


1 Answers

As I promised, here is how we solved it.

In your class where you have your ScrollView you need a state like

const [activeTab, setActiveTab] = useState('tab1') //your default (first) tab

Later, you want to change that state every time you change tab:

<TabsStack.Screen
  name={'tab1'}
  component={
    activeTab === 'tab1' ? Tab1Screen : DefaultScreen
  }
  listeners={{ focus: () => setActiveTab('tab1') }}
/>

Now every time a tab is unselected or rather not focused it will show DefaultScreen which is an empty view and has no height like:

const DefaultScreen = () => (
  <Box
    flex={1}
    py={20}
    alignItems="center"
    justifyContent="center"
    bg="background"
  >
    <ActivityIndicator size="large" color="white" animating />
  </Box>
)

In my case (see question above), every time I switch from Tab1.1.1 to Tab1.1.2, Tab1.1.1 will change to DefaultScreen

like image 128
M_droid Avatar answered Oct 06 '22 21:10

M_droid