简体   繁体   中英

android UI lag on some devices

I have an android project that contains viewpager and navigation drawer. in each fragment of viewpager I have a gridview with load data from a server! My question is, everything is work fine in all devices from low hardware to powerful ones, but in some devices like galaxy s4 and some sony xperia it gives too much lag on scrolling gridview or opening navigation drawer. what's wrong with it?

PS: below is the code for gridview adapter:

public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        convertView = inflater.inflate(R.layout.grid_item, parent, false);
    }
    SimpleData item = myDatas.get(position);
    ((TextView) convertView.findViewById(R.id.title)).setText(item.getName());
    ImageView thumbnail = (ImageView) convertView.findViewById(R.id.thumbnail);
    thumbnail.getLayoutParams().height = thumbnail.getLayoutParams().width;

    if (item.getThumbnail() != null && !item.getThumbnail().isEmpty()) {
        if (item.getThumbnail().startsWith("http")) {
            Picasso.with(mContext)
                    .load(item.getThumbnail())
                    .placeholder(R.drawable.loading)
                    .into(thumbnail);
        } else {
            Picasso.with(mContext)
                    .load(Uri.fromFile(new File(item.getThumbnail())))
                    .placeholder(R.drawable.loading)
                    .into(thumbnail);
        }
    } else {
        thumbnail.setImageResource(R.drawable.noimage);
    }

    ((TextView) convertView.findViewById(R.id.province)).setText(item.getProvince());
    ((TextView) convertView.findViewById(R.id.likesCount)).setText(item.getLikesCount() + "");

    convertView.forceLayout();
    return convertView;
}

Finally I found the problem ! the lag was because of the else part of the code which set the image on the UI thread, below Line:

 thumbnail.setImageResource(R.drawable.noimage);

so I replace the "noimage" drawable with the compress one and change this line to load the file using Picasso as below, and everything works great.

 Picasso.with(mContext)
                .load(R.drawable.noimage)
                .placeholder(R.drawable.loading)
                .into(thumbnail);

Thanks all for helping.

You are doing a lot of things which are not recommended here and which can be the cause of your lags.

First do not use convertView.forceLayout(); , according to its documentation it'll force your layout to render when it's not needed. Second, you inflate the convertView , you should not do that in the getView of an adapter. Remember, you are overriding a class and it's not your job to inflate the layout, it's done automatically by the adapter class at an upper level.

Finally you do not make use of the adapter design pattern in the form of:

if (convertView == null) {
  ...
} else {
  ...
}

For more informations on the latter, refer yourself to this documentation , there is an ImageAdapter example.

您的getView方法正在做很多事情,您应该了解如何在GridView中异步加载位图,并且这样做不会滞后。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM