Search code examples
androidandroid-recyclerviewlag

Recycler view laggy behaviour


The problem is when recyclerview sroll for the first time it lagged on moment when new rv item appear in sight, beacause of method onCreateViewHolder() that inflates new item layout. Any solution to speed up the process or preinflate layout? Beacuse it start scrolling smoothly when enough layouts created to recycle items.

public class PersonInReviewRvAdapter extends RecyclerView.Adapter<PersonInReviewRvAdapter.PersonHolder> {

private Context context;
private OnItemClickListener itemClickedInterface;
private OnItemLongClickListener itemLongClickedInterface;


public List<SoundingBoardPersonModel> daterList = new ArrayList<>();

public PersonInReviewRvAdapter(Context context, List<SoundingBoardPersonModel> daterList) {
    this.context = context;
    this.daterList = daterList;
}

@Override
public PersonInReviewRvAdapter.PersonHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View layoutView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.item_review_match_sb, parent, false);
    return new PersonInReviewRvAdapter.PersonHolder(layoutView);
}


@Override
public void onBindViewHolder(PersonInReviewRvAdapter.PersonHolder holder, int position) {

}

public void setItemClickListener(OnItemClickListener itemClickedInterface) {
    this.itemClickedInterface = itemClickedInterface;
}

public void setLongItemClickListener(OnItemLongClickListener itemLongClickedInterface) {
    this.itemLongClickedInterface = itemLongClickedInterface;
}

public void addContact(String name, String location) {
    SoundingBoardPersonModel soundingBoardPersonModel = new SoundingBoardPersonModel();
    soundingBoardPersonModel.name = name;
    soundingBoardPersonModel.location = location;
    daterList.add(soundingBoardPersonModel);
    notifyDataSetChanged();
}

public void removeContact(int position) {
    daterList.remove(position);
    notifyDataSetChanged();
}


@Override
public int getItemCount() {
    return 10;
}

static class PersonHolder extends RecyclerView.ViewHolder {

    @BindView(R.id.item_container)
    RelativeLayout itemContainer;

    @BindView(R.id.person_subscriber_name)
    TextViewNexaBold nameTxt;

    @BindView(R.id.person_subscriber_location)
    TextViewNexaLight personLocation;

    public PersonHolder(View itemView) {
        super(itemView);
        ButterKnife.bind(this, itemView);
    }
}}

And item layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/item_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/selector_gray_bottom_top_stroke">


<RelativeLayout
    android:id="@+id/subscriber_container"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="@dimen/size_24dp"
    android:layout_marginTop="@dimen/size_24dp"
    android:layout_toLeftOf="@+id/center_container">

    <de.hdodenhof.circleimageview.CircleImageView
        android:id="@+id/person_subscribre_icon"
        android:layout_width="@dimen/size_80dp"
        android:layout_height="@dimen/size_80dp"
        android:layout_centerHorizontal="true" />


    <com.qazaqfound.thesetup.custom_views.TextViewNexaBold
        android:id="@+id/person_subscriber_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/person_subscribre_icon"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="@dimen/size_16dp"
        android:ellipsize="marquee"
        android:maxLines="1"
        android:maxWidth="@dimen/size_120dp"
        android:text="Melisa"
        android:textColor="@color/dark_grey_main"
        android:textSize="@dimen/text_size_16sp" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/person_subscriber_name"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="@dimen/size_8dp"
        android:orientation="horizontal">

        <ImageView
            android:layout_width="@dimen/size_16dp"
            android:layout_height="@dimen/size_16dp"
            android:layout_gravity="center_vertical"
            android:src="@drawable/ic_location" />


        <com.qazaqfound.thesetup.custom_views.TextViewNexaLight
            android:id="@+id/person_subscriber_location"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="@dimen/size_2dp"
            android:layout_marginTop="@dimen/size_1dp"
            android:ellipsize="marquee"
            android:maxLines="1"
            android:text="USA, New York" />

    </LinearLayout>
</RelativeLayout>

<RelativeLayout
    android:id="@+id/target_match_container"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="@dimen/size_24dp"
    android:layout_marginTop="@dimen/size_24dp"
    android:layout_toRightOf="@+id/center_container">

    <de.hdodenhof.circleimageview.CircleImageView
        android:id="@+id/person_match_icon"
        android:layout_width="@dimen/size_80dp"
        android:layout_height="@dimen/size_80dp"
        android:layout_centerHorizontal="true" />


    <com.qazaqfound.thesetup.custom_views.TextViewNexaBold
        android:id="@+id/person_target_match_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/person_match_icon"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="@dimen/size_16dp"
        android:ellipsize="marquee"
        android:maxLines="1"
        android:maxWidth="@dimen/size_120dp"
        android:text="Melisa"
        android:textColor="@color/dark_grey_main"
        android:textSize="@dimen/text_size_16sp" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/person_target_match_name"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="@dimen/size_8dp"
        android:orientation="horizontal">

        <ImageView
            android:layout_width="@dimen/size_16dp"
            android:layout_height="@dimen/size_16dp"
            android:layout_gravity="center_vertical"
            android:src="@drawable/ic_location" />


        <com.qazaqfound.thesetup.custom_views.TextViewNexaLight
            android:id="@+id/ic_location_match"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="@dimen/size_2dp"
            android:layout_marginTop="@dimen/size_1dp"
            android:ellipsize="marquee"
            android:maxLines="1"
            android:text="USA, New York" />

    </LinearLayout>
</RelativeLayout>


<LinearLayout
    android:id="@+id/center_container"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="@dimen/size_36dp"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/ic"
        android:layout_width="@dimen/size_40dp"
        android:layout_height="@dimen/size_40dp"
        android:layout_gravity="center_horizontal"
        android:src="@drawable/ic_match_arrow" />

    <com.qazaqfound.thesetup.custom_views.TextViewNexaBold
        android:id="@+id/time_remaining_txt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="@dimen/size_8dp"
        android:text="20 minutes"
        android:textColor="@color/dark_pink" />

    <com.qazaqfound.thesetup.custom_views.TextViewNexaBold
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="@dimen/size_8dp"
        android:text="@string/text_remaining" />
</LinearLayout>


<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/subscriber_container"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="@dimen/size_16dp">

    <ImageView
        android:id="@+id/like_btn"
        android:layout_width="@dimen/size_70dp"
        android:layout_height="@dimen/size_70dp"
        android:src="@drawable/ic_like" />


    <ImageView
        android:id="@+id/dislike_btn"
        android:layout_width="@dimen/size_70dp"
        android:layout_height="@dimen/size_70dp"
        android:layout_toRightOf="@+id/like_btn"
        android:src="@drawable/ic_dislike" />

</RelativeLayout>


Solution

  • I notice you used <com.qazaqfound.thesetup.custom_views.TextViewNexaBold> custom views. I used the same approach to create TextView with custom font. If so, please avoid retaking fonts from assets folder.