滚动时网格视图滞后

问题描述 投票:0回答:1

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

我曾尝试使图片质量更低,但这没有帮助。仅当我在网格视图中的所有32个元素中使用一张图像时,所有分叉才可以,但是一旦我拥有更多不同的图像,它就会滞后。

我的班级:

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

    }

我的适配器:

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

ma grid_row 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>

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

android android-studio gridview android-gridview
1个回答
0
投票

如果要解决滞后问题,应在gridView适配器中使用ViewHolder模式。我尝试了一下Google,发现了这个示例,它可以为您提供帮助:https://github.com/first087/Android-ViewHolder-Example/blob/master/app/src/main/java/com/artitk/android_viewholder_example/GridViewActivity.java

特别检查如何制作ViewHoldersetTag以查看容器,以便适​​配器可以在没有任何性能设备的情况下正确回收项目。

下一步,我建议您摆脱GridView,而改用RecyclerView,因为它更强大且性能更高。

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

© www.soinside.com 2019 - 2024. All rights reserved.