ListView延迟从下到上滚动

时间:2018-03-24 18:14:59

标签: java android listview lag

我的列表视图中存在滞后问题。

因此,当我向下滚动列表视图时,性能非常出色,但当我向上滚动时,发现我的行时会出现延迟。

我使用滑行来观看图像。

我的适配器中的方法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 类中:

      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;
}

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

结果相同,因为您没有正确使用View Holder。每次调用getView时,您都在膨胀视图。

public class GPSdata
{
    public TimeSpan time { get; set; }     //time (in seconds, advances by 0.2)
    public int latDegrees { get; set; }      //Latitude
    public int latMinutes { get; set; }      //Latitude
    public int latSeconds { get; set; }      //Latitude
    public string NS { get; set; }       //North/South
    public int lonDegrees { get; set; }      //Longtitude
    public int lonMinutes { get; set; }      //Longtitude
    public int lonSeconds { get; set; }      //Longtitude
    public string EW { get; set; }       //East/West
    public decimal knots { get; set; }    //Speed in Knots
    public DateTime date { get; set; }     //Date [ddmmyy]
    public int sats { get; set; }     //**No clue**
    public decimal HDOP { get; set; }     //Satelite Horizontal error
    public decimal alt { get; set; }      //Elevation (above msl)
    public int rawSV { get; set; }    //Space Vehicle
}
public static List<GPSdata> LoadCSV(string filepath)
{
    List<GPSdata> data = new List<GPSdata>();
    using (StreamReader reader = new StreamReader(filepath))
    {
        while (!reader.EndOfStream)
        {
            string line = reader.ReadLine();
            string[] values = line.Split(',');

            GPSdata gpsdata = new GPSdata();                    

            gpsdata.time = new TimeSpan((long)(decimal.Parse(values[0]) * (decimal)1.0E07));
            int latDecimalPoint = values[1].IndexOf(".");
            gpsdata.latSeconds = int.Parse(values[1].Substring(latDecimalPoint + 1));
            gpsdata.latMinutes = int.Parse(values[1].Substring(latDecimalPoint - 2, 2));
            gpsdata.latDegrees = int.Parse(values[1].Substring(0, latDecimalPoint - 2));
            gpsdata.NS = values[2];
            int lonDecimalPoint = values[3].IndexOf(".");
            gpsdata.lonSeconds = int.Parse(values[3].Substring(lonDecimalPoint + 1));
            gpsdata.lonMinutes = int.Parse(values[3].Substring(lonDecimalPoint - 2, 2));
            gpsdata.lonDegrees = int.Parse(values[3].Substring(0, lonDecimalPoint - 2));
            gpsdata.EW = values[4];
            gpsdata.knots = decimal.Parse(values[5]);
            int dateLen = values[6].Length;
            gpsdata.date = new DateTime(int.Parse(values[6].Substring(dateLen - 2)), int.Parse(values[6].Substring(0, dateLen - 4)), int.Parse(values[6].Substring(dateLen - 4, 2)));
            gpsdata.sats = int.Parse(values[7]);
            gpsdata.HDOP = decimal.Parse(values[8]);
            gpsdata.rawSV = int.Parse(values[9]);
            gpsdata.alt = decimal.Parse(values[10]);

            data.Add(gpsdata);
        }
    }
    return data;
}