0

I am trying to use only ConstraintLayout in my app to avoid nested layouts. The problem is that I need to make scrollable some elements of my ConstraintLayout and I have seen that the correct way of doing this is to put a ScrollView outside the ConstraintLayout, but doing this all the elements of the Constraint layout would be scrollable and I only need to make scrollable a few elements. I have seen that in other post this was done by adding a LinearLayout to the ConstraintLayout and making scrollable that LinearLayout, but I am avoiding using other layouts that are not ConstraintLayout because of this: answer in other post

So how could I make scrollable only some elements of my ConstraintLayout without the need of using other different layouts inside my ConstraintLayout to avoid nedted layouts?

Thanks for the help

Lechius
  • 189
  • 7
  • Are you looking to scroll groups of views or individual views? If individual views and those views are _TextViews_, then it is possible. Otherwise, it is going to depend on the details of your project. Posting details here may help. If you are scrolling groups of views then it is worse than you think: You will need to enclose the group in another _ViewGroup_ and enclose that _ViewGroup_ inside a _ScrollView_ since a _ScrollView_ can have only one direct child. TBH, I wouldn't worry too much about nesting unless it is excessive and effects performance. – Cheticamp Nov 08 '22 at 23:43
  • @Cheticamp how could I check if my nested layouts are excessive and are affecticng effects performance? I would want to confirm that my nested layouts are not a problem for the performance. Is there a way to check that to know it for sure? – Lechius Nov 09 '22 at 12:38
  • Check if your app is [skipping frame](https://vaibhavtolia.wordpress.com/2013/10/03/79/). If not, you are likely good to go. I would check to make sure that the layout is easy to understand - too much layering would effect that, but that is subjective. If you still think you have a problem, for instance, waiting too long for the screen to display, you could [profile your app](https://developer.android.com/studio/profile). – Cheticamp Nov 09 '22 at 13:16

1 Answers1

0

I don't know if i understand you clearly but i think you should use something like below:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <TextView
        android:id="@+id/outerText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Some text"
        android:gravity="center"
        android:textSize="32sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:fillViewport="true"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/outerText"
        app:layout_constraintBottom_toTopOf="@+id/outerButton">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <!--Add the items you want to scroll inside this layout.-->

        </androidx.constraintlayout.widget.ConstraintLayout>

    </androidx.core.widget.NestedScrollView>

    <Button
        android:id="@+id/outerButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Add"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toBottomOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

Paste this code to some xml and see the result. If that's not what you meant please add more detailed info or an image to show what you want.

Mert
  • 904
  • 4
  • 9
  • 21