Home > database >  As soon as I scroll , there appears gap between items/viewholders of RecyclerView
As soon as I scroll , there appears gap between items/viewholders of RecyclerView

Time:10-13

I am Trying RecyclerView for the first time in android studio . on Running , before scrolling it works fine and as soon as I scroll it mess up . There appears some big gap between views/items of Recycler View . There are some warnings as well On line 30 : It says ViewHolder is redundant and On Line 49 : It says ViewHolder may be static

Before Scrolling : Before Scrolling It's working well

After Scrolling ( This is where my concern starts ) : Messed Up Recycler View

RecyclerContactAdapter.java :

package com.example.recyclerviewlesson;

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

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

import java.util.ArrayList;

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

    Context context ;
    ArrayList<ContactModel> arrContacts ;

    RecyclerContactAdapter(Context context, ArrayList<ContactModel> arrContacts) {
        this.context = context ;
        this.arrContacts = arrContacts ;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(context).inflate(R.layout.contact_row, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;

    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

        holder.imgContact.setImageResource(arrContacts.get(position).img);
        holder.txtName.setText(arrContacts.get(position).name);
        holder.txtNumber.setText(arrContacts.get(position).number);

    }

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

    public class ViewHolder extends RecyclerView.ViewHolder {

        TextView txtName , txtNumber ;
        ImageView imgContact ;

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

            txtName = itemView.findViewById(R.id.txtName);
            txtNumber = itemView.findViewById(R.id.txtNumber);
            imgContact = itemView.findViewById(R.id.imgContact);

        }
    }
}

activity_main.xml ( which contains RecyclerView ) :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    tools:context=".MainActivity">

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


</LinearLayout>

contact_row.xml ( which is layout file ig ) :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical">


    <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:cardCornerRadius="10dp"
        app:cardUseCompatPadding="true">


        <LinearLayout
            android:id="@ id/llRow"
            android:padding="10dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_vertical">

            <ImageView
                android:id="@ id/imgContact"
                android:layout_width="70dp"
                android:layout_height="70dp"
                android:background="@color/white"
                android:contentDescription="Contact Image"
                android:src="@drawable/g">
            </ImageView>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginStart="10dp"
                android:layout_marginLeft="10dp"
                android:orientation="vertical">
                <TextView
                    android:id="@ id/txtName"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Contact"
                    android:textStyle="bold"
                    android:textSize="25sp"
                    android:textColor="@color/black">
                </TextView>
                <TextView
                    android:id="@ id/txtNumber"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Contact Number"
                    android:textSize="16sp"
                    android:textColor="@color/black">
                </TextView>
            </LinearLayout>


        </LinearLayout>

    </androidx.cardview.widget.CardView>


</LinearLayout>

CodePudding user response:

You are setting the height of each of the items to the height of the screen. Items should probably have either wrap_content or some fixed value.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical">
</LinearLayout>
  • Related