0

Good evening,

I have an Android app with two ListView elements on a layout. between them is a TextView element. The whole is framed by a LinearLayout and a ScrollView and a ConstraintLayout.

activity_recipe.xml

<?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">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbarAlwaysDrawVerticalTrack="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <LinearLayout
            android:id="@+id/linearLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="start"
            android:orientation="vertical"
            android:padding="15dp"

            android:scrollbars="vertical"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <TextView
                android:id="@+id/tv_recipe_txt"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="@string/txt_recipe_entry_txt"
                android:textAppearance="?android:attr/textAppearanceLarge" />

            <TextView
                android:id="@+id/pancake_ingredients"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="15dp"
                android:gravity="center"
                android:text="@string/pancake_ingredients"
                android:textAppearance="?android:attr/textAppearanceLarge" />

            <ListView
                android:id="@+id/listViewIngredients"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
            </ListView>

            <TextView
                android:id="@+id/pancake_preparation"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="15dp"
                android:gravity="center"
                android:text="@string/pancake_preparation"
                android:textAppearance="?android:attr/textAppearanceLarge" />

            <ListView
                android:id="@+id/listViewPreparation"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
        </LinearLayout>
    </ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>

Die Daten für die ListViews werden aus zwei string-array geladen. In meiner Activity arbeite ich mit einem ``ÀrrayAdapter```

RecipeActivity.java

package com.git.amarradi.palatschinkencounter;

import android.os.Build;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

public class RecipeActivity extends AppCompatActivity {
    ListView listView_Ingredients;
    ListView listView_Preparation;
    String[] listItem_ingredients;
    String[] listItem_Preparation;

    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recipe);
        listView_Ingredients = (ListView) findViewById(R.id.listViewIngredients);
        listView_Preparation = (ListView) findViewById(R.id.listViewPreparation);


        listItem_ingredients = getResources().getStringArray(R.array.ingredients_array);
        listItem_Preparation = getResources().getStringArray(R.array.preparation_array);
        final ArrayAdapter<String> arrayAdapter_Ingredients = new ArrayAdapter<>(this,
                R.layout.simple_list_item_1, listItem_ingredients);
        listView_Ingredients.setAdapter(arrayAdapter_Ingredients);

        final ArrayAdapter<String> arrayAdapter_Preparation = new ArrayAdapter<>(this,
                R.layout.simple_list_item_1,listItem_Preparation);
        listView_Preparation.setAdapter(arrayAdapter_Preparation);

    }
}

Now to my question, why I am hanging with the layout. How can I set the height of the ListViews so that all data can be loaded without scrolling. I have taken extra ScrollView for this, but the from the ListView

android:layout_height="wrap_content"

remains single-line.

What is the best way to do such things? I have the ScrollView right after the ConstraintLayout so i can scroll through the page. Where is my mistake in logic?

Maybe someone can help me. That would be very kind. I say now already many thanks.

The new Activity_recipe.xml

activity_recipe.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    android:overScrollMode="always">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:paddingBottom="8dp"
        android:text="@string/txt_recipe_entry_txt"
        android:textAppearance="?android:attr/textAppearanceLarge"/>

        <TextView
            android:id="@+id/textView3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:paddingBottom="8dp"
            android:text="@string/pancake_ingredients"
            android:textAppearance="?android:attr/textAppearanceLarge"
            />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView_ingredients"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:paddingBottom="8dp"
        android:text="@string/pancake_preparation"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <androidx.recyclerview.widget.RecyclerView
        android:scrollbarAlwaysDrawVerticalTrack="false"
        android:id="@+id/RecyclerView_preparation"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="8dp"
        app:layout_constraintTop_toBottomOf="@+id/textView4" />

    <TextView
        android:id="@+id/textViewThanks"
        android:text="@string/thanks"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
</androidx.core.widget.NestedScrollView>

The new RecipeActivity.java

RecipeActivity.java


    package com.git.amarradi.palatschinkencounter;
    
    import android.os.Bundle;
    
    import androidx.annotation.Nullable;
    import androidx.appcompat.app.AppCompatActivity;
    import androidx.recyclerview.widget.LinearLayoutManager;
    import androidx.recyclerview.widget.RecyclerView;
    
    import java.util.ArrayList;
    
    public class RecipeActivity extends AppCompatActivity {
    
        ArrayList<IngredientsModel> ingredientsModels = new ArrayList<>();
        ArrayList<PreparationModel> preparationModels = new ArrayList<>();
    
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_recipe);
    
            RecyclerView recyclerView_Ingredients = findViewById(R.id.recyclerView_ingredients);
    
            setupIngredientsModels();
    
            RecylerIngredientsViewAdapter recylerIngredientsViewAdapter =
                    new RecylerIngredientsViewAdapter(this,ingredientsModels);
            recyclerView_Ingredients.setAdapter(recylerIngredientsViewAdapter);
            recyclerView_Ingredients.setLayoutManager(new LinearLayoutManager(this));
    
    
            RecyclerView recyclerView_Preparation = findViewById(R.id.RecyclerView_preparation);
    
            setupPreparationModels();
            RecylerPreparationViewAdapter recylerPreparationViewAdapter =
                    new RecylerPreparationViewAdapter(this,preparationModels);
            recyclerView_Preparation.setNestedScrollingEnabled(false);
            recyclerView_Preparation.setAdapter(recylerPreparationViewAdapter);
            recyclerView_Preparation.setLayoutManager(new LinearLayoutManager(this));
        }
    
        private void setupPreparationModels() {
            String[] strings_preparation = getResources().getStringArray(R.array.preparation_array);
            for (String s : strings_preparation) {
                preparationModels.add(new PreparationModel(s));
            }
        }
    
        public void setupIngredientsModels(){
            String[] strings_ingredients = getResources().getStringArray(R.array.ingredients_array);
            for (String strings_ingredient : strings_ingredients) {
                ingredientsModels.add(new IngredientsModel(strings_ingredient));
            }
        }
    
    }

I added two RecylerView -Adapter, for every ArrayList one adapter


package com.git.amarradi.palatschinkencounter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;

public class RecylerIngredientsViewAdapter extends RecyclerView.Adapter<RecylerIngredientsViewAdapter.MyViewHolder> {

    Context context;
    ArrayList<IngredientsModel> ingredientsModels;

    public RecylerIngredientsViewAdapter(Context context, ArrayList<IngredientsModel> ingredientsModels) {
        this.context = context;
        this.ingredientsModels = ingredientsModels;

    }

    public static class MyViewHolder extends RecyclerView.ViewHolder {

        TextView CVtextView;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);

            CVtextView = itemView.findViewById(R.id.CVtextView);
        }
    }
    
    @NonNull
    @Override
    public RecylerIngredientsViewAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.recycler_view_row, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecylerIngredientsViewAdapter.MyViewHolder holder, int position) {

        holder.CVtextView.setText(ingredientsModels.get(position).getIngredientsLine());

    }

    @Override
    public int getItemCount() {
        Log.d("getItemCount", String.valueOf(ingredientsModels.size()) );
        return ingredientsModels.size();
    }


}

I have also created two small models


    package com.git.amarradi.palatschinkencounter;

public class PreparationModel {
    private final String preparationLine;

    public PreparationModel(String preparationLine) {
        this.preparationLine = preparationLine;
    }

    public String getPreparationLine() {
        return preparationLine;
    }
}

And my row_view

recycler_view_row.xml

    <?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"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <androidx.cardview.widget.CardView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:cardCornerRadius="2dp"

        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

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

            <TextView

                android:paddingStart="8dp"
                android:paddingEnd="0dp"
                android:paddingBottom="16dp"
                android:id="@+id/CVtextView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center_vertical"
                android:textAppearance="?android:attr/textAppearanceMedium"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
        </androidx.constraintlayout.widget.ConstraintLayout>
    </androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
amarradi
  • 109
  • 3
  • 16

1 Answers1

0

Try this:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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="wrap_content"
android:scrollbarAlwaysDrawVerticalTrack="true">

<LinearLayout
    android:id="@+id/linearLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="start"
    android:orientation="vertical"
    android:padding="15dp"
    android:scrollbars="vertical">

    <TextView
        android:id="@+id/tv_recipe_txt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="@string/txt_recipe_entry_txt"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/pancake_ingredients"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="15dp"
        android:gravity="center"
        android:text="@string/pancake_ingredients"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <ListView
        android:id="@+id/listViewIngredients"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/pancake_preparation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="15dp"
        android:gravity="center"
        android:text="@string/pancake_preparation"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <ListView
        android:id="@+id/listViewPreparation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>
</ScrollView>
i_A_mok
  • 2,744
  • 2
  • 11
  • 15
  • Thank you for your Answer i tried it in my app but it looks always i little bit buggy an i dont know way – amarradi Jun 24 '22 at 09:10
  • https://www.marcusradisch.de/palatschinkencounter/pics/buggy.png here you can see the result. The ListView has scrollbars but not the hole "page" – amarradi Jun 24 '22 at 09:14
  • 1
    this may help https://stackoverflow.com/questions/4338185/how-to-get-a-non-scrollable-listview – i_A_mok Jun 24 '22 at 21:21
  • Yes, that helped. Thank you! I finally realised it with two RecylerView elements and a corresponding change in the code base. I will now adjust the question a little. – amarradi Jun 27 '22 at 18:28
  • If you have time, you can try RecyclerView with 3 view types. Make a layout file to include all the views above **RecyclerView_preparation** (header). Another layout file for the last TextView (footer). Therefore everything is in one RecyclerView, no more ScrollView. – i_A_mok Jul 01 '22 at 10:49