Search code examples
androidandroid-listviewandroid-scrollview

How to reach the last items of a scroll view?


I'm trying to display a list in a fixed size scrollview, at first it was the first items who weren't showing and I fixed it but now it's the last ones that aren't reachable.

Here is my xml code:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorPrimary"
    tools:context=".ui.home.AperoDetailFragment">

    <TextView
        android:id="@+id/name_apero"
        android:layout_width="156dp"
        android:layout_height="53dp"
        android:textSize="18sp"
        android:gravity="center"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0" />

    <TextView
        android:id="@+id/date_apero"
        android:layout_width="238dp"
        android:layout_height="53dp"
        android:textSize="18sp"
        android:ems="10"
        android:gravity="center"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.907"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0" />

    <ScrollView
        android:id="@+id/ingredient_apero"
        android:layout_width="410dp"
        android:layout_height="607dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/ingredient_title_apero"
        app:layout_constraintVertical_bias="0.0">

        <LinearLayout
            android:id="@+id/vertical_layout_ingredient"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" />
    </ScrollView>

    <TextView
        android:id="@+id/ingredient_title_apero"
        android:layout_width="115dp"
        android:layout_height="28dp"
        android:text="Liste d'achat:"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.005"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.125" />
</androidx.constraintlayout.widget.ConstraintLayout>  

and here is my java code to populate the list:

public class AperoDetailFragment extends Fragment {

    private View root;
    private Apero detailApero;

    public AperoDetailFragment(Apero apero) {
        this.detailApero = apero;
    }

    @Override
    public View onCreateView(@NonNull final LayoutInflater inflater,
                             final ViewGroup container, Bundle savedInstanceState) {
        root = inflater.inflate(R.layout.fragment_detail_apero, container, false);

        TextView name = (TextView)root.findViewById(R.id.name_apero);
        name.setText(detailApero.getName());
        TextView date = (TextView)root.findViewById(R.id.date_apero);
        date.setText(detailApero.getDate());

        LinearLayout ll = (LinearLayout)root.findViewById(R.id.vertical_layout_ingredient);
        LinearLayout a = new LinearLayout(root.getContext());
        a.setOrientation(LinearLayout.VERTICAL);
        for(int i = 0; i < 20; i++)
        {
            Button b = new Button(root.getContext());
            b.setText("Button "+i);
            a.addView(b);
        }
        ll.addView(a);

        return root;
    }
}  

When I scroll I can reach the number 16 but not the other, it's like they are under the layout I don't really know how to explain better.

So the question is how can I scroll my list until the last items ?


Solution

  • There are a few issues with your code: first and foremost if you want to display data as a list you should use a RecyclerView instead of a ScrollView. ScrollViews are there to allow the content (or part of it) in your activity/fragment to be scrollable.

    Second, it's not a good practice to set specific sizes to your views, especially when using ConstraintLayout.

    Third, onCreateView is meant to be a method that will simply inflate your fragment's layout and return it as a View. For handling the UI, use onViewCreated. That way you will guarantee that your UI will never be handled before your fragment is actually attached to the activity.

    So answering your question, use a RecyclerView to display your items as a list instead of the ScrollView and you'll be good to go from there