Android 滚动时栅格视图滞后

Android 滚动时栅格视图滞后,android,android-studio,gridview,android-gridview,Android,Android Studio,Gridview,Android Gridview,我有一个包含32个元素或图像的网格视图。图像质量很低,不超过300kb。每次我滚动时,网格视图都会滞后,而且滞后非常明显。我想了解下一步要做什么的想法或实际代码 我试着制作质量更低的图片,但没有效果。只有当我对网格视图中的所有32个元素使用一个图像时,一切都会好起来,但当我有更多的difrent图像时,它就会滞后 我的班级: private GridView gridView; ArrayList<CreateProduct> products; @Override

我有一个包含32个元素或图像的网格视图。图像质量很低,不超过300kb。每次我滚动时,网格视图都会滞后,而且滞后非常明显。我想了解下一步要做什么的想法或实际代码

我试着制作质量更低的图片,但没有效果。只有当我对网格视图中的所有32个元素使用一个图像时,一切都会好起来,但当我有更多的difrent图像时,它就会滞后

我的班级:

private GridView gridView;
    ArrayList<CreateProduct> products;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);setContentView(R.layout.activity_all_products_grid_view);

        products = new ArrayList<CreateProduct>();
        createAllProducts2();

        gridView = (GridView) findViewById(R.id.allProductGrid);
        GridAdapter customAdapter = new GridAdapter(this, products);
        gridView.setAdapter(customAdapter);
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                CreateProduct x = products.get(i);
                goToProductDetails(x.productName, x.id, x.productDesc,x.productKulise, x.image);
            }
        });

    }
private-GridView-GridView;
ArrayList产品;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);setContentView(R.layout.activity\u all\u products\u grid\u view);
products=新的ArrayList();
createAllProducts2();
gridView=(gridView)findViewById(R.id.allProductGrid);
GridAdapter customAdapter=新的GridAdapter(此,产品);
setAdapter(customAdapter);
setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
公共无效onItemClick(AdapterView AdapterView、View视图、int i、long l){
CreateProduct x=产品。获取(i);
goToProductDetails(x.productName、x.id、x.productDesc、x.productKulise、x.image);
}
});
}
我的适配器:

public class GridAdapter extends BaseAdapter {

    ArrayList<CreateProduct> arrayGrid;
    Context context;

    public GridAdapter(Context context, ArrayList<CreateProduct> arrayList){
        this.arrayGrid = arrayList;
        this.context = context;
    }
    @Override
    public int getCount() {
        return arrayGrid.size();
    }
    @Override
    public long getItemId(int i) {
        return 0;
    }
    @Override
    public Object getItem(int i) {
        return null;
    }


    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        final CreateProduct product = arrayGrid.get(i);
        if (view == null){
            final LayoutInflater layoutInflater = LayoutInflater.from(context);
            view = layoutInflater.inflate(R.layout.grid_row, null);
        }
        final TextView title33 = view.findViewById(R.id.titleGrid);
        final ImageView image33 = view.findViewById(R.id.grid_image);

        title33.setText(product.productName);
        image33.setImageResource(product.image);

        return view;
    }
}
公共类GridAdapter扩展BaseAdapter{
ArrayList arrayGrid;
语境;
公共GridAdapter(上下文上下文,ArrayList ArrayList){
this.arrayGrid=arrayList;
this.context=上下文;
}
@凌驾
public int getCount(){
返回arrayGrid.size();
}
@凌驾
公共长getItemId(int i){
返回0;
}
@凌驾
公共对象getItem(int i){
返回null;
}
@凌驾
公共视图getView(int i、视图视图、视图组视图组){
最终CreateProduct产品=arrayGrid.get(i);
如果(视图==null){
final LayoutInflater LayoutInflater=LayoutInflater.from(上下文);
视图=布局更平坦。充气(R.layout.grid\u行,空);
}
final TextView title33=view.findViewById(R.id.titleGrid);
最终ImageView image33=view.findViewById(R.id.grid\u图像);
title33.setText(product.productName);
image33.setImageResource(product.image);
返回视图;
}
}
ma网格行xml文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/grid_image"
        android:layout_gravity="center"
        android:layout_width="130sp"
        android:layout_height="130sp"
        android:contentDescription="@string/comment_button_product_details"
        tools:srcCompat="@tools:sample/avatars" />

    <TextView
        android:id="@+id/titleGrid"
        android:layout_width="130sp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="@string/product_name"
        android:textAlignment="center" />
</LinearLayout>


我没有收到任何错误消息。我希望网格视图不滞后。

如果要解决滞后问题,应在网格视图适配器中使用ViewHolder模式。我试着用谷歌搜索一下,发现了这个示例,它可以帮助您:

特别要检查如何使
ViewHolder
setTag
查看容器,这样适配器就可以在没有任何性能设备的情况下正确回收项目

对于下一步,我建议您放弃
GridView
,改用
RecyclerView
,因为它更强大,性能更高


要加载图像,请使用Glide或任何其他类似库高效加载图像。

注释image33.setImageResource(product.image);这一行,并验证仍然滞后或注意,如果我评论这一行,请尝试使用Glide或迁移到回收服务。它也可以正常工作,最多8张图像,但随后它开始滞后。谢谢你,我将谷歌滑翔,并尝试看看会发生什么。好的,非常感谢你!我将使用“回收者”视图,并尝试使用Glide加载图像,希望能奏效。另外,如果您知道关于ViewHolder和setTag的任何文章或很好的解释,请告诉我,因为我从未听说过或做过任何事情,在这里你可以多读一点。。这是一个老教程,但解释仍然有效,相当不错。我刚刚使用了glide,现在一切都很好。但我仍然想做得更“恰当”,所以感谢您的链接和解释:)