0

I would like to display data in a Recyclerview. This works until I have reached the end of the list when scrolling. The problem is that I can scroll down endlessly without any data. If I want to scroll up again, the lines are displayed with a very large space.

It would be great if someone of you could help me!

Here is the Code for the Fragment to display the data:

import android.content.Context;
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;
import java.util.List;

public class UserAbrechAdapter extends RecyclerView.Adapter<UserAbrechAdapter.ViewHolder> {

    List<Abrechnung> abrechnung;
    Context mContext;

    public UserAbrechAdapter(Context context, ArrayList<Abrechnung> list) {
        mContext = context;
        abrechnung = list;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.adapter_abrech, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder holder, int position) {
        holder.item_betrag.setText(String.valueOf(abrechnung.get(position).getBetrag()));
        holder.item_datum.setText(abrechnung.get(position).getDatum());
        holder.item_partner.setText(abrechnung.get(position).getTransaktionspartner());
        holder.item_typ.setText(abrechnung.get(position).getTyp());
    }

    @Override
    public int getItemCount() {
        return abrechnung.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        private TextView item_typ, item_betrag, item_datum, item_partner;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            item_typ = itemView.findViewById(R.id.typ);
            item_betrag = itemView.findViewById(R.id.wert);
            item_datum = itemView.findViewById(R.id.datum);
            item_partner = itemView.findViewById(R.id.partner);
        }
    }
}

Here is the XML code for the fragment layout:

<?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="@drawable/hintergrund">

    <TextView
        android:id="@+id/betrag"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_marginTop="52dp"
        android:fontFamily="@font/segoeui_standard"
        android:text="@string/user_geld_Betrag"
        android:textColor="@color/weiße_schrift"
        android:textSize="20sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/bestellung"
        android:layout_width="wrap_content"
        android:layout_height="31dp"
        android:layout_marginStart="44dp"
        android:layout_marginTop="52dp"
        android:fontFamily="@font/segoeui_standard"
        android:text="@string/user_typ_transaktion"
        android:textColor="@color/weiße_schrift"
        android:textSize="20sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/datum"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="52dp"
        android:layout_marginEnd="32dp"
        android:fontFamily="@font/segoeui_standard"
        android:text="@string/user_datum_transaktion"
        android:textColor="@color/weiße_schrift"
        android:textSize="20sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0" />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerview_scroll"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/betrag"
        app:layout_constraintVertical_bias="0.051"
        tools:layout_editor_absoluteX="0dp">

    </androidx.recyclerview.widget.RecyclerView>

</androidx.constraintlayout.widget.ConstraintLayout>

And this ist the XML for the line Item:

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

    <TextView
        android:id="@+id/typ"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="24dp"
        android:gravity="center"
        android:text="TextView"
        app:layout_constraintBottom_toTopOf="@+id/partner"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/wert"
        app:layout_constraintVertical_bias="0.85" />

    <TextView
        android:id="@+id/partner"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="25dp"
        android:gravity="center"
        android:text="TextView"
        app:layout_constraintStart_toStartOf="@+id/typ"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/wert"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:text="TextView"
        app:layout_constraintBottom_toTopOf="@+id/partner"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/datum"
        app:layout_constraintVertical_bias="1.0" />

    <TextView
        android:id="@+id/datum"
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:layout_marginTop="4dp"
        android:layout_marginEnd="24dp"
        android:text="TextView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


</androidx.constraintlayout.widget.ConstraintLayout>
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Marlo
  • 1
  • 1
  • What is the type of your database? – Alex Mamo May 13 '20 at 13:29
  • I am using Firebase, it´s not the realtime database – Marlo May 13 '20 at 13:34
  • If you are using Cloud Firestore, then **[here](https://stackoverflow.com/questions/50741958/how-to-paginate-firestore-with-android)** is a recommended way in which you can paginate queries by combining query cursors with the limit() method. I also recommend you take a look at this **[video](https://www.youtube.com/watch?v=KdgKvLll07s)** for a better understanding. – Alex Mamo May 13 '20 at 13:41

2 Answers2

1

First, change this android:layout_height="match_parent" to android:layout_height="wrap_content" in line Item XML file

Ritu Suman Mohanty
  • 734
  • 1
  • 6
  • 15
0

You should use recyclerView.setHasFixedSize(true); in your activity
Also check the size of ArrayList you are passing in Adapter.