ListView 延迟从底部滚动到顶部
ListView lag scrolling from bottom to top
我的列表视图有点滞后。
所以,当我向下滚动我的列表视图时,性能非常好,但是当我向上滚动时,当我发现我的行时会有延迟。
我正在使用 glide 查看图片。
我的适配器中的 text3Lines 方法在我的 textView 中设置了三行文本。
我的适配器中的方法 checkClass 包含一个简单的 if 条件。
在我的 OnCreate 方法中:
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);
在 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;
}
}
这是我的 ViewHolder 结构:
static class ViewHolder {
TextView textView;
ImageView imageView;
CardView cardView;
}
在此先感谢您的帮助。
结果相同,因为您没有正确使用 View Holder。每次调用 getView 时,您都在膨胀视图。
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;
}
我的列表视图有点滞后。
所以,当我向下滚动我的列表视图时,性能非常好,但是当我向上滚动时,当我发现我的行时会有延迟。
我正在使用 glide 查看图片。
我的适配器中的 text3Lines 方法在我的 textView 中设置了三行文本。 我的适配器中的方法 checkClass 包含一个简单的 if 条件。
在我的 OnCreate 方法中:
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);
在 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;
}
}
这是我的 ViewHolder 结构:
static class ViewHolder {
TextView textView;
ImageView imageView;
CardView cardView;
}
在此先感谢您的帮助。
结果相同,因为您没有正确使用 View Holder。每次调用 getView 时,您都在膨胀视图。
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;
}