ScrollView for only part of a component? (React Native) - android

(New to ReactNative)
I currently have a React Native app which has a component with the following (rough) structure :
<View>
<View>
<TextInput />
<Text />
</View>
<View>
<Image />
<Button />
</View>
</View>
So within that parent View, let's call the children View1 and View2. I can very easily substitute the parent View for ScrollView and have the whole component be scrollable. However, what if I want only View2 to be scrollable and View1 to stay in place? And I mean vertical scrolling, not horizontal scrolling/pagination.
How can I do this? Switching out View2 for ScrollView does not work. I'm also looking for solutions where I wouldn't have to split those View1 and View2 into different components because there's a complex mechanism currently in place which relies on sharing state between the two.
Any suggestions? Happy to provide some of the actual code if necessary but I believe the above should be enough to get a feel for it.
Thanks in advance.

This can be solved using a nested component.
As you note, you can very easily have a whole component be scrollable. So by making View2 a separate component, you can implement the scrolling in View2 only.
From http://reactjstutorial.net/
Nested Components Nested Components are exactly what the name implies.
They are children nested inside parent components. They simply help us
create more complex UI structures. For example, an alert component
probably has another sub-component nested within it to indicate the
actual number of alerts. A message box has an inner input element and
a send button.

Related

An issue with vertical parent scrollview and two children recycler-view in Android?

I've the following use case -
<ScrollView> // parent
<ContentView/> // content
</ScrollView>
I do not have any access to the parent ScrollView because the library only allows setting the content view. And it doesn't provide any API or reference to modify the attributes of the parent ScrollView. And the content of ContentView is arbitrary in a way that it can take recycler-view, list-view and any other views with scrolling effects.
Because I do not have access to the parent ScrollView, I can't change its property / attributes anyhow. This causes the issue will ill-behaved scrolling. This specially shows the problem when
<ScrollView>
<ContentView>
<LinearLayout orientation=horizonal>
<RecyclerView/>
<RecyclerView/>
</LinearLayout>
<ContentView>
<ScrollView
And this causes two recycler-views to scroll together, while I want individual recycler-view scroll independently.
I was thinking of a solution that I can build a container-view and the children of container-view are agnostic of its ScrollView parent. So that my recycler-views have no knowledge of its parent ScrollView.
Is there any way to implement such solution? Have you ever encountered such an issue and how did you fix it?

Is it good idea to use Constrain layout inside Scrollview in Android?

I am working as OEM developer where we are using common GUI library for all Applications. In our application we are extending that GUI library which carry ScrollView as base layout for my application. Now my team is planning to use constrain layout in Application. Can we use constrain layout inside Scroll View?
I converted linear layout into constrain layout using Android Design tool. But
scrolling is not working.
<ScrollView
style="#style/Body_ScrollView"
android:id="#+id/no_sim_layout">
<LinearLayout style="#style/Body.LinearLayout.No_Sim">
<-- All other child views are going here-->
</LinearLayout>
</ScrollView>
Want to convert into
<ScrollView
style="#style/Body_ScrollView"
android:id="#+id/no_sim_layout">
<ConstrainLayout style="#style/Body.LinearLayout.No_Sim">
<-- All other child views are going here-->
</ConstrainLayout>
</ScrollView>
Yes, definitely the Constraint-layout first understand this:
Intention of ConstraintLayout is to optimize and flatten the view hierarchy of your layouts by applying some rules to each view to avoid nesting.Which recommends of Relative-layout rules.
More-ever it provide dynamic view alignment property as constraints, baseline, chaining of views and many other which provide seamless flatten hierarchy. If we used Constraint in Scrollview, we dont need to manage each view property like weighing in LinearLayout and many others. It much more simple and directly obtained benefits from provided dependencies.
<ConstrintLayout>
<ScrollView>
<ConstrintLayout>
//Single ParentConstrain else child
</ConstrintLayout>
</ScrollView>
</ConstrintLayout>
If goes by other way, this make your complex XML code more hard to understand and is heavy to build UI based on weighing and position calculated at run time on machine.
Constraint are directly benefit from input dependencies.
<ConstrintLayout>
<ScrollView>
<LinearLayout>
//Many different view to manage view
</LinearLayout>
</ScrollView>
</ConstrintLayout>

Making a View scroll once bottom is reached

Im trying to create a layout with following looks and behaviour.
<Layout>
<Text "Header"/>
<List> "Contains a couple of texts, most likely LinearLayout" </List>
<Button "Confirm"/>
</Layout>
<Text "Information/Hint"/>
Top-Level Layout is centered on the screen
Information-Text is placed right beneath it (Layout is centered without accounting for this text)
Once the list fills to the point where the Information-Text is pushed out of the screen, the List starts scrolling and the Information-Text remains visible at the bottom of the screen. Distance of the top-border of the top-level-layout to the screen-border is the same as the hight of the information-text
The scrolling-behaviour also takes action if the android-system-settings increased display/text-size to the point where said borders are reached
Its the last two points that give me trouble. I've been experimenting with ConstraintLayout and LinearLayout, but every time the scrolling would work the layout wasn't correct anymore (not centered or margins on the texts screwed)
Any ideas how this could be achived without writing a custom Layout?
Do your view hierarchy like this:
<ScrollView>
<LinearLayout>
<Layout>
<Text "Header"/>
<List> "Contains a couple of texts, most likely LinearLayout" </List>
<Button "Confirm"/>
</Layout>
<Text "Information/Hint"/>
</LinearLayout>
</ScrollView>

CardView with RecyclerView/ListView in ScrollView

tl;dr: How to achieve the layout shown in the screenshot below? Placing ListView to a ScrollView is apparently not recommended, but is there actually any other way to achieve it?
The whole question: I want to have multiple CardViews in my app, and one (or more) of them will have either RecyclerView or ListView in it (it doesn't really matter to me which one of those). The whole view is supposed to be scrollable - not only the ListViews in their parent CardViews. I basically need to achieve similar layout as the Play Store app has.
The first option I tried was this (the code is obviously simplified):
<LinearLayout android:orientation="vertical">
<CardView>
<!-- Some content of the first card. -->
</CardView>
<CardView>
<ListView/>
</CardView>
</LinearLayout>
The result was not what I wanted, the ListView was only scrollable in its parent CardView but the whole view wasn't scrollable like it is in the Play Store app. So now I wrapped it all in a ScrollView:
<ScrollView
android:fillViewport="true"
android:isScrollContainer="true">
<LinearLayout orientation="vertical">
<CardView>
<!-- Some content of the first card. -->
</CardView>
<CardView>
<ListView/>
</CardView>
</LinearLayout>
</ScrollView>
And I programmatically set the height of the bottom card to fit the ListView's height (number of elements in the ListView * height of one list item element). Now the whole view is scrollable, and the bottom card's height is the same as the height of the ListView, so the ListView isn't scrollable inside the CardView which is exactly what I wanted.
Now the actual problem: I got it working as described above, but I know this particular issue (ListView in a ScrollView) has been asked about many times before and the answer has always been the same - don't put neither RecyclerView nor ListView in a ScrollView because it causes performance problems. Well, so what's the correct approach then? How did Google do it in the Play Store app? I tried decompiling the Play Store app with APKTool but there weren't any layout files (maybe I did something wrong). Is my approach correct? My ListView will only display a few items (I guess it will be at most 20 items) - will it cause some performance issues in this case?
I wouldn't ask about this if all the answers wouldn't always mention that we shouldn't put ListView in a ScrollView. Is there any other way how to achieve the layout described by the screenshot above?
The first thing to address is why you're "not supposed to" use wrap_content on a ListView or a RecyclerView and put it in a scrollable container: it defeats the entire view-recycling purpose of these components.
What makes a ListView or RecyclerView better than a LinearLayout inside a ScrollView is that the system only needs to create enough views to display everying that fits inside the visible area. When you "scroll" the visible area, the views that disappear off one end can be re-used for the views that scroll into view from the other end. When you make your list/recycler wrap_content, this recycling is impossible, so you might as well just manually add your views to a LinearLayout instead.
That being said, RecyclerView does support using wrap_content... it just means you won't get view recycling. If this performance hit doesn't cause you problems, there's no objectively evil code here.
The only way to know for sure if the performance penalty is problematic or not is to just try it, test it, measure it, and decide for yourself. With 20 items, I suspect you have nothing to worry about.
The next thing to think about is the fact that Google has tons of resources and manpower and can afford to be extremely clever. Perhaps the Play Store app is as you say, with some sort of scrollable parent container that holds cards, each of which have some sort of adapter view within. But it's equally possible that they're doing something completely different, like using a single RecyclerView and "faking" the appearance of cards by using an ItemDecoration. Or perhaps they are using some sort of custom view subclass that the public doesn't have access to.
As for how you could recreate something similar, I suspect a hierarchy like this will work just fine:
<NestedScrollView>
<LinearLayout>
<CardView>
<RecyclerView/>
</CardView>
<CardView>
<RecyclerView/>
</CardView>
<CardView>
<RecyclerView/>
</CardView>
</LinearLayout>
</NestedScrollView>
I would recommend you to use Sectioned RecyclerView for this purpose. Every single item layout would have a cardView in it instead of creating a cardView as a parent.
Refer to this library: https://github.com/luizgrp/SectionedRecyclerViewAdapter

Controlling the scroll bar in my Android application

I have three linear sub layouts in my activity window in my Android application. Each layout has one scroll bar. My problem is when I am trying scroll in one layout area other layouts scroll bars also activating. Could you please suggest a solution for this?
ONE <scrollView> can hold at max ONE component inside it....... this ONE component can be either a layout holding several other views or further layouts inside it, or it can be a single view.
in order to have 3 separate scrolling linear layouts (meaning scrolling one linearLayout does not affect other LinearLayouts)..... you should have 3 separate <ScrollView> elements - each scrollview containing at max ONE of your THREE linearLayout.
for example:
<ScrollView>
<LinearLayout>
all sub-components of **LinearLayout1** goes here
<LinearLayout>
</ScrollView>
<ScrollView>
<LinearLayout>
all sub-components of **LinearLayout2** goes here
<LinearLayout>
</ScrollView>
<ScrollView>
<LinearLayout>
all sub-components of **LinearLayout3** goes here
<LinearLayout>
</ScrollView>
hope it helps you.

Categories

Resources