简体   繁体   English

某些设备上的android UI滞后

[英]android UI lag on some devices

I have an android project that contains viewpager and navigation drawer. 我有一个包含viewpager和导航抽屉的android项目。 in each fragment of viewpager I have a gridview with load data from a server! 在viewpager的每个片段中,我都有一个包含服务器负载数据的gridview! 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. 我的问题是,从低硬件到功能强大的所有设备,一切都可以正常工作,但是在某些设备(例如galaxy s4和某些Sony xperia)中,它在滚动gridview或打开导航抽屉时会产生很大的延迟。 what's wrong with it? 它出什么问题了?

PS: below is the code for gridview adapter: PS:下面是gridview适配器的代码:

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: 滞后的原因是代码的其他部分在UI线程的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. 因此,我用compress之一替换了可绘制的“ noimage”,并更改了这一行以使用Picasso加载文件,如下所示,一切正常。

 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(); 首先不要使用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. 其次,你膨胀convertView ,你应该做的是,在getView适配器的。 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. 有关后者的更多信息,请参考本文档 ,其中有一个ImageAdapter示例。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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