Solution 1 :
Try creating a release build of the app. Debug builds often display minor amounts of animation lag.
Problem :
So I have a pretty basic recycler view where Items are in cardViews.
There are no pictures loading , it’s just data represented on cardViews. When data is loaded from server , while trying to scroll though initially it starts to lag. after a while a lag is gone. What could be wrong ?
My adapter code is as follows.
public class TeacherJobBoardAdapter extends
RecyclerView.Adapter<TeacherJobBoardAdapter.ViewHolder> {
public class ViewHolder extends RecyclerView.ViewHolder {
TextView salary, location,numberOfStudents,curriculum,teacherGender,class1,class2,gender1,gender2,daysInWeek, std1Subject, std2Subject,time;
LinearLayout linearLayoutStd2;
ImageView doneTick;
CardView v;
SlideToActView slider;
AnimatedVectorDrawable animated;
Context context;
public ViewHolder(View itemView, Context context) {
super(itemView);
this.context = context;
v = itemView.findViewById(R.id.card);
slider = itemView.findViewById(R.id.slider_teacherJobBoard);
doneTick = itemView.findViewById(R.id.doneTick_teacherJobBoard);
animated = (AnimatedVectorDrawable) doneTick.getDrawable();
numberOfStudents = itemView.findViewById(R.id.spn_teacherJobBoard_numberOfStudents);
linearLayoutStd2 = itemView.findViewById(R.id.linlayout_teacherJobBoard_std2);
location = itemView.findViewById(R.id.txt_teacherJobBoard_location);
salary = itemView.findViewById(R.id.txt_teacherJobBoard_salary);
curriculum = itemView.findViewById(R.id.txt_teacherJobBoard_curriculum);
class1 = itemView.findViewById(R.id.txt_teacherJobBoard_std1Class);
class2 = itemView.findViewById(R.id.txt_teacherJobBoard_std2Class);
gender1 = itemView.findViewById(R.id.txt_teacherJobBoard_std1Gender);
gender2 = itemView.findViewById(R.id.txt_teacherJobBoard_std2Gender);
teacherGender = itemView.findViewById(R.id.txt_teacherJobBoard_tutorGender);
daysInWeek = itemView.findViewById(R.id.txt_teacherJobBoard_daysInWeek);
std1Subject = itemView.findViewById(R.id.txt_teacherJobBoard_std1Subject);
std2Subject = itemView.findViewById(R.id.txt_teacherJobBoard_std2Subject);
time = itemView.findViewById(R.id.txt_teacherJobBoard_time);
}
}
private List<ParseObject> mObjects;
public TeacherJobBoardAdapter(List<ParseObject> object) {
mObjects = object;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
// Inflate the custom layout
View contactView = inflater.inflate(R.layout.item_teacher_job_board, parent, false);
// Return a new holder instance
return new ViewHolder(contactView,context);
}
@Override
public void onBindViewHolder(@NonNull final ViewHolder holder, int pos) {
int position = pos;
holder.salary.setText(""+mObjects.get(pos).getInt("salary"));
holder.location.setText(mObjects.get(pos).getString("location"));
holder.numberOfStudents.setText(mObjects.get(pos).getInt("numberOfStudents")+"");
holder.curriculum.setText(mObjects.get(pos).getString("curriculum"));
holder.teacherGender.setText(mObjects.get(pos).getString("teacherGender"));
if (mObjects.get(pos).getInt("numberOfStudents")==1){
holder.linearLayoutStd2.setVisibility(View.GONE);
}else{
holder.gender2.setText(mObjects.get(pos).getString("gender2"));
holder.class2.setText(mObjects.get(pos).getString("class2"));
holder.std2Subject.setText(mObjects.get(pos).getString("subject2")+"");
}
holder.gender1.setText(mObjects.get(pos).getString("gender1"));
holder.class1.setText(mObjects.get(pos).getString("class1"));
holder.std1Subject.setText(mObjects.get(pos).getString("subject1")+"");
holder.daysInWeek.setText(mObjects.get(pos).getInt("daysInWeek")+"");
holder.time.setText(mObjects.get(pos).getString("time"));
holder.slider.setOnSlideCompleteListener(new SlideToActView.OnSlideCompleteListener() {
@Override
public void onSlideComplete(SlideToActView slideToActView) {
TeacherJobBoard myActivity = (TeacherJobBoard) holder.context;
myActivity.apply(slideToActView,holder.doneTick,holder.animated);
}
});
}
@Override
public int getItemCount() {
return mObjects.size();
}
}
Here is the xml for the items layout
<LinearLayout 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="wrap_content"
android_orientation="vertical">
<androidx.cardview.widget.CardView
android_id="@+id/card"
android_layout_width="match_parent"
android_layout_height="match_parent"
android_layout_marginLeft="@dimen/_4sdp"
android_layout_marginRight="@dimen/_4sdp"
app_cardBackgroundColor="#FFFFFF"
app_cardCornerRadius="@dimen/_5sdp"
app_cardElevation="@dimen/_3sdp"
app_cardUseCompatPadding="true">
<LinearLayout
android_layout_width="match_parent"
android_layout_height="wrap_content"
android_layout_gravity="center"
android_orientation="vertical"
android_weightSum="10">
<LinearLayout
android_layout_width="match_parent"
android_layout_height="match_parent"
android_orientation="horizontal"
android_weightSum="3">
<TextView
android_id="@+id/txt_teacherJobBoard_location"
android_layout_width="0dp"
android_layout_height="wrap_content"
android_layout_weight="2"
android_fontFamily="@font/kiona_regular"
android_paddingLeft="@dimen/_8sdp"
android_paddingTop="@dimen/_8sdp"
android_paddingBottom="@dimen/_8sdp"
android_text="ADABOR"
android_textColor="@color/tutor_bear_dark_blue"
android_textSize="@dimen/_20sdp" />
<LinearLayout
android_layout_width="0dp"
android_layout_height="wrap_content"
android_layout_weight="1"
android_orientation="vertical">
<TextView
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_layout_gravity="end"
android_layout_weight="1"
android_fontFamily="@font/kiona_regular"
android_paddingTop="@dimen/_8sdp"
android_paddingEnd="@dimen/_8sdp"
android_text="SALARY"
android_textAlignment="textEnd"
android_textColor="@color/tutor_bear_dark_blue"
android_textSize="@dimen/_20sdp" />
<TextView
android_id="@+id/txt_teacherJobBoard_salary"
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_layout_gravity="end"
android_layout_weight="1"
android_drawableEnd="@drawable/ic_taka"
android_fontFamily="@font/kiona_regular"
android_paddingBottom="@dimen/_8sdp"
android_text="9000"
android_textAlignment="textEnd"
android_textColor="@color/tutor_bear_dark_blue"
android_textSize="@dimen/_20sdp" />
</LinearLayout>
</LinearLayout>
<View
android_layout_width="match_parent"
android_layout_height="@dimen/_1sdp"
android_layout_marginLeft="@dimen/_10sdp"
android_layout_marginRight="@dimen/_10sdp"
android_background="@color/tutor_bear_dark_blue" />
<LinearLayout
android_layout_width="match_parent"
android_layout_height="wrap_content"
android_layout_margin="@dimen/_2sdp"
android_orientation="vertical">
<LinearLayout
android_layout_width="match_parent"
android_layout_height="match_parent"
android_layout_marginTop="@dimen/_7sdp"
android_orientation="horizontal"
android_weightSum="2">
<TextView
android_layout_width="0dp"
android_layout_height="wrap_content"
android_layout_weight="1"
android_fontFamily="@font/roboto_light"
android_paddingStart="@dimen/_7sdp"
android_text="@string/studentViewMyPost_numberOfStudents"
android_textColor="@color/tutor_bear_dark_blue"
android_textSize="@dimen/_13sdp" />
<TextView
android_id="@+id/spn_teacherJobBoard_numberOfStudents"
android_layout_width="0dp"
android_layout_height="wrap_content"
android_layout_weight="1"
android_fontFamily="@font/roboto_light"
android_paddingEnd="@dimen/_7sdp"
android_text="2"
android_textAlignment="textEnd"
android_textColor="@color/tutor_bear_dark_blue"
android_textSize="@dimen/_13sdp" />
</LinearLayout>
<LinearLayout
android_layout_width="match_parent"
android_layout_height="match_parent"
android_layout_marginTop="@dimen/_6sdp"
android_orientation="horizontal">
<TextView
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_fontFamily="@font/roboto_light"
android_paddingLeft="@dimen/_7sdp"
android_text="@string/teacherJobBoard_curriculum"
android_textColor="@color/tutor_bear_dark_blue"
android_textSize="@dimen/_13sdp" />
<View
android_layout_width="0dp"
android_layout_height="0dp"
android_layout_weight="1" />
<TextView
android_id="@+id/txt_teacherJobBoard_curriculum"
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_fontFamily="@font/roboto_light"
android_paddingEnd="@dimen/_7sdp"
android_text="English Medium"
android_textAlignment="textEnd"
android_textColor="@color/tutor_bear_dark_blue"
android_textSize="@dimen/_13sdp" />
</LinearLayout>
<LinearLayout
android_layout_width="match_parent"
android_layout_height="wrap_content"
android_layout_marginTop="@dimen/_7sdp"
android_orientation="horizontal">
<TextView
android_layout_width="0dp"
android_layout_height="wrap_content"
android_layout_weight="1"
android_fontFamily="@font/roboto_light"
android_paddingStart="@dimen/_7sdp"
android_text="Student 1"
android_textColor="@color/tutor_bear_dark_blue"
android_textSize="@dimen/_13sdp" />
<LinearLayout
android_layout_width="0dp"
android_layout_height="wrap_content"
android_layout_weight="1"
android_orientation="vertical">
<TextView
android_id="@+id/txt_teacherJobBoard_std1Subject"
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_layout_gravity="end"
android_fontFamily="@font/roboto_light"
android_paddingEnd="@dimen/_7sdp"
android_text="Physics, Chemistry, Maths, Psycology"
android_textAlignment="textEnd"
android_textColor="@color/tutor_bear_dark_blue"
android_textSize="@dimen/_13sdp" />
<TextView
android_id="@+id/txt_teacherJobBoard_std1Gender"
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_layout_gravity="end"
android_layout_marginTop="@dimen/_3sdp"
android_drawableStart="@drawable/ic_male"
android_fontFamily="@font/roboto_light"
android_paddingEnd="@dimen/_7sdp"
android_text="Male"
android_textAlignment="textEnd"
android_textColor="@color/tutor_bear_dark_blue"
android_textSize="@dimen/_13sdp" />
<TextView
android_id="@+id/txt_teacherJobBoard_std1Class"
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_layout_gravity="end"
android_layout_marginTop="@dimen/_3sdp"
android_layout_marginBottom="@dimen/_3sdp"
android_fontFamily="@font/roboto_light"
android_paddingEnd="@dimen/_7sdp"
android_text="Class 8"
android_textAlignment="textEnd"
android_textColor="@color/tutor_bear_dark_blue"
android_textSize="@dimen/_13sdp" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android_id="@+id/linlayout_teacherJobBoard_std2"
android_layout_width="match_parent"
android_layout_height="wrap_content"
android_layout_marginTop="@dimen/_7sdp"
android_orientation="horizontal">
<TextView
android_layout_width="0dp"
android_layout_height="wrap_content"
android_layout_weight="1"
android_fontFamily="@font/roboto_light"
android_paddingStart="@dimen/_7sdp"
android_text="Student 2"
android_textColor="@color/tutor_bear_dark_blue"
android_textSize="@dimen/_13sdp" />
<LinearLayout
android_layout_width="0dp"
android_layout_height="wrap_content"
android_layout_weight="1"
android_orientation="vertical">
<TextView
android_id="@+id/txt_teacherJobBoard_std2Subject"
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_layout_gravity="end"
android_fontFamily="@font/roboto_light"
android_paddingEnd="@dimen/_7sdp"
android_text="Physics, Chemistry, Maths, Psycology"
android_textAlignment="textEnd"
android_textColor="@color/tutor_bear_dark_blue"
android_textSize="@dimen/_13sdp" />
<TextView
android_id="@+id/txt_teacherJobBoard_std2Gender"
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_layout_gravity="end"
android_layout_marginTop="@dimen/_3sdp"
android_drawableStart="@drawable/ic_male"
android_fontFamily="@font/roboto_light"
android_paddingEnd="@dimen/_7sdp"
android_text="Male"
android_textAlignment="textEnd"
android_textColor="@color/tutor_bear_dark_blue"
android_textSize="@dimen/_13sdp" />
<TextView
android_id="@+id/txt_teacherJobBoard_std2Class"
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_layout_gravity="end"
android_layout_marginTop="@dimen/_3sdp"
android_layout_marginBottom="@dimen/_3sdp"
android_fontFamily="@font/roboto_light"
android_paddingEnd="@dimen/_7sdp"
android_text="Class 8"
android_textAlignment="textEnd"
android_textColor="@color/tutor_bear_dark_blue"
android_textSize="@dimen/_13sdp" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android_layout_width="match_parent"
android_layout_height="match_parent"
android_layout_marginTop="@dimen/_6sdp"
android_orientation="horizontal">
<TextView
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_fontFamily="@font/roboto_light"
android_paddingLeft="@dimen/_7sdp"
android_text="@string/studentViewMyPost_preferredTutorGender"
android_textColor="@color/tutor_bear_dark_blue"
android_textSize="@dimen/_13sdp" />
<View
android_layout_width="0dp"
android_layout_height="0dp"
android_layout_weight="1" />
<TextView
android_id="@+id/txt_teacherJobBoard_tutorGender"
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_drawableStart="@drawable/ic_female"
android_fontFamily="@font/roboto_light"
android_paddingEnd="@dimen/_7sdp"
android_text="Female"
android_textAlignment="textEnd"
android_textColor="@color/tutor_bear_dark_blue"
android_textSize="@dimen/_13sdp" />
</LinearLayout>
<LinearLayout
android_layout_width="match_parent"
android_layout_height="match_parent"
android_layout_marginTop="@dimen/_6sdp"
android_orientation="horizontal">
<TextView
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_fontFamily="@font/roboto_light"
android_paddingLeft="@dimen/_7sdp"
android_text="@string/studentViewMyPost_daysInWeek"
android_textColor="@color/tutor_bear_dark_blue"
android_textSize="@dimen/_13sdp" />
<View
android_layout_width="0dp"
android_layout_height="0dp"
android_layout_weight="1" />
<TextView
android_id="@+id/txt_teacherJobBoard_daysInWeek"
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_drawableStart="@drawable/ic_calender"
android_fontFamily="@font/roboto_light"
android_paddingEnd="@dimen/_7sdp"
android_text="5"
android_textColor="@color/tutor_bear_dark_blue"
android_textSize="@dimen/_13sdp" />
</LinearLayout>
<LinearLayout
android_layout_width="match_parent"
android_layout_height="match_parent"
android_layout_marginTop="@dimen/_6sdp"
android_layout_marginBottom="@dimen/_8sdp"
android_orientation="horizontal">
<TextView
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_fontFamily="@font/roboto_light"
android_paddingLeft="@dimen/_7sdp"
android_text="@string/studentViewMyPost_preferredTime"
android_textColor="@color/tutor_bear_dark_blue"
android_textSize="@dimen/_13sdp" />
<View
android_layout_width="0dp"
android_layout_height="0dp"
android_layout_weight="1" />
<TextView
android_id="@+id/txt_teacherJobBoard_time"
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_drawableStart="@drawable/ic_time_new"
android_fontFamily="@font/roboto_light"
android_paddingEnd="@dimen/_7sdp"
android_text="9:00 PM - 6:00 AM"
android_textColor="@color/tutor_bear_dark_blue"
android_textSize="@dimen/_13sdp" />
</LinearLayout>
<com.ncorti.slidetoact.SlideToActView
android_id="@+id/slider_teacherJobBoard"
android_layout_width="@dimen/_220sdp"
android_layout_height="wrap_content"
android_layout_gravity="center"
android_layout_marginTop="@dimen/_10sdp"
android_layout_marginBottom="@dimen/_10sdp"
android_clickable="false"
android_hapticFeedbackEnabled="true"
app_animation_duration="250"
app_area_margin="@dimen/_5sdp"
app_bump_vibration="60"
app_complete_icon="@drawable/anim_loading_spinner"
app_icon_margin="@dimen/_5sdp"
app_inner_color="@color/color_white"
app_outer_color="@color/tutor_bear_dark_blue"
app_slider_height="@dimen/_40sdp"
app_slider_icon="@drawable/ic_next_arrow"
app_text="Slide to Apply" />
<ImageView
android_id="@+id/doneTick_teacherJobBoard"
android_layout_width="@dimen/_40sdp"
android_layout_height="@dimen/_40sdp"
android_layout_gravity="center"
android_layout_marginTop="@dimen/_10sdp"
android_layout_marginBottom="@dimen/_10sdp"
android_src="@drawable/done_tick_new_anim"
android_visibility="gone" />
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
Comments
Comment posted by Gavin Wright
No, but you didn’t include the part of the code where you did the network call. if you’re doing that on the main thread, it could be the reason. But I think it’s more likely just due to the debug build.
Comment posted by Tanzim Chowdhury
Network request is on the worker thread. Okay so after a bit of research I found out the lags occur on the first 6 items. And while scrolling through these items the onCreateViewHolder is called. After that it’s all smooth. So every onCreate for adapter is called , the lag happens. Any suggestions ?
Comment posted by Gavin Wright
My suggestion is to try the release build. I spent days trying to optimize a RecyclerView before I realized it had no problems whatsoever on the release build.
Comment posted by Tanzim Chowdhury
Brah, Genius solution. No lags on release build. But how and why ? This just confuses us developers.
Comment posted by Gavin Wright
I think the release build strips out all of the debugging features, and just generally optimizes the code.