I have a little problem with a lag in my listview.
So, when I scroll down my listview the performance are excellent but when I scroll up there is a lag when I discover my row.
I'm using glide to view images.
The method text3Lines in my Adapter set three line of text in my textView. The method checkClass in my Adapter contains a simple if condition.
In my OnCreate method:
display = getWindowManager().getDefaultDisplay();
size = new Point();
display.getSize(size);
width_square = (size.x)/100;
height_square = (size.x)/100;
list = new ListView(this);
list.setDivider(null);
list.setDividerHeight(0);
setContentView(list);
In MyAdapter class:
private class MyAdapter extends ArrayAdapter<SOCIAL_POST> {
TextView listText;
ImageView listImage;
CardView listCard;
RelativeLayout listLayout;
RelativeLayout.LayoutParams paramsTxt;
RelativeLayout.LayoutParams paramsImg;
RelativeLayout.LayoutParams paramsCardView;
public MyAdapter(Context context, ArrayList<SOCIAL_POST> posts) {
super(context, -1, -1, posts);
listLayout = new RelativeLayout(SocialPage.this);
paramsCardView = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
paramsCardView.leftMargin = width_square*5;
paramsCardView.rightMargin = width_square*5;
paramsCardView.topMargin = height_square*5;
paramsCardView.width = width_square*100;
paramsImg = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
paramsImg.topMargin = height_square*5;
paramsImg.height = width_square*50;
paramsTxt = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
paramsTxt.leftMargin = width_square*10;
paramsTxt.rightMargin = width_square*10;
paramsTxt.topMargin = height_square*5;
paramsTxt.width = width_square*80;
paramsCardView.addRule(RelativeLayout.CENTER_HORIZONTAL);
paramsImg.addRule(RelativeLayout.CENTER_HORIZONTAL);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
text_height = 0;
convertView = getLayoutInflater().inflate(R.layout.row_list_social,null);
convertView.setPaddingRelative(width_square*5, height_square*3,
width_square*5, height_square*3);
ViewHolder holder = new ViewHolder();
holder.imageView = convertView.findViewById(R.id.rowImage);
holder.textView = convertView.findViewById(R.id.rowText);
holder.cardView = convertView.findViewById(R.id.rowCard);
convertView.setTag(holder);
holder.textView.setTextColor(Color.BLACK);
holder.cardView.setContentPadding(width_square*5, height_square*3,
width_square*5, height_square*3);
holder.cardView.setCardBackgroundColor(Color.parseColor("#FFFFFF"));
paramsTxt.addRule(RelativeLayout.BELOW, R.id.rowImage);
paramsCardView.addRule(RelativeLayout.BELOW, R.id.rowText);
//check social
String social = checkClass(String.valueOf(getItem(position).getClass()));
switch (social){
case "FACEBOOK":
new_fb_post = (FB_POST) getItem(position);
if(new_fb_post.full_picture != null && !new_fb_post.full_picture.equals("")){
paramsImg.height = width_square*50;
Glide.with(mContext)
.load(new_fb_post.full_picture)
.into(listImage);
} else {
paramsImg.height = width_square*1;
}
text3Lines(new_fb_post.message, listText);
break;
case "OFFICIAL":
new_of_post = (OF_POST) getItem(position);
if(new_of_post.full_picture != null && !new_of_post.full_picture.equals("")){
paramsImg.height = width_square*50;
Glide.with(mContext)
.load(new_of_post.full_picture)
.into(listImage);
} else {
paramsImg.height = width_square*5;
}
text3Lines(new_of_post.message, listText);
break;
default:
break;
}
listCard.setLayoutParams(paramsCardView);
listImage.setLayoutParams(paramsImg);
listText.setLayoutParams(paramsTxt);
return convertView;
}
}
Here is my ViewHolder struct:
static class ViewHolder {
TextView textView;
ImageView imageView;
CardView cardView;
}
Thanks in advance for the help.
Result is same because you are not using View Holder properly. You are inflating the view each time when getView called.
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
// if convertView is null
convertView = mInflater.inflate(R.layout.mylayout, parent, false);
holder = new ViewHolder();
// initialize views
convertView.setTag(holder);
// set tag on view
}
else {
holder = (ViewHolder) convertView.getTag();
// if not null get tag // no need to initialize
} //update views here return convertView;
}