带有 Gridlayoutmanager 的 RecyclerView 加载和响应速度极慢

Posted

技术标签:

【中文标题】带有 Gridlayoutmanager 的 RecyclerView 加载和响应速度极慢【英文标题】:RecyclerView with Gridlayoutmanager extremely slow to load and respond 【发布时间】:2021-08-19 18:57:21 【问题描述】:

recyclerview 的帮助下,我正在构建一个显示两列网格视图的应用程序。我的代码如下:

activity_main.xml

 <androidx.constraintlayout.widget.ConstraintLayout 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_
 android:layout_
 tools:context=".MainActivity">

 <androidx.recyclerview.widget.RecyclerView
     android:id="@+id/recycle"
     android:layout_
     android:layout_ />
 </androidx.constraintlayout.widget.ConstraintLayout>

custom_item_layout.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android">
  <!--
     stroke with color and width for creating outer line
  -->
  <stroke
      android:
      android:color="#DBE2E9" />
</shape>

row_layout.xml

 <androidx.constraintlayout.widget.ConstraintLayout 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_
  android:layout_
  android:background="@drawable/custom_item_layout"
  android:padding="5dp">
  <!--
  grid items for RecyclerView
  -->
  <ImageView
      android:id="@+id/image"
      android:layout_
      android:layout_
      android:layout_marginStart="5dp"
      android:layout_marginTop="5dp"
      android:layout_marginEnd="5dp"
      android:scaleType="centerCrop"
      android:src="@mipmap/ic_launcher"
      app:layout_constraintBottom_toBottomOf="parent"
      app:layout_constraintEnd_toEndOf="parent"
      app:layout_constraintStart_toStartOf="parent"
      app:layout_constraintTop_toTopOf="parent" />

  </androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java

 public class MainActivity extends AppCompatActivity 

  // ArrayList for all app genres
  ArrayList appNames = new ArrayList<>(Arrays.asList( "Augmented reality",
                 "Beauty", "Books & Reference",  "Comics", "Communication",
                 "Dating", "Entertainment", "Food & Drink",
                  "Games", "Kids", "Music & Audio", "Photography",
                  "Social", "Video Players & Editors"));
  ArrayList appImages = new ArrayList<>(Arrays.asList(R.drawable.a_r, R.drawable.beauty, R.drawable.ic_books,
          R.drawable.ic_smashing_book_icon_comic_book_icon_11553392583tcjc, R.drawable.comms, R.drawable.date,
          R.drawable.entertainment, R.drawable.foods, R.drawable.games,
          R.drawable.kids, R.drawable.music, R.drawable.camera,
          R.drawable.social, R.drawable.video));
  @Override
  protected void onCreate(Bundle savedInstanceState) 
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      // get the reference of RecyclerView
      RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycle);
      // set a GridLayoutManager with default vertical orientation and 2 number of columns
      GridLayoutManager gridLayoutManager = new GridLayoutManager(getApplicationContext(),2);
      recyclerView.setLayoutManager(gridLayoutManager); // set LayoutManager to RecyclerView
      //  call the constructor of CustomAdapter to send the reference and data to Adapter
      CustomAdapter customAdapter = new CustomAdapter(MainActivity.this, appNames,appImages);
      recyclerView.setAdapter(customAdapter); // set the Adapter to RecyclerView
  

CustomAdapter.java

 public class CustomAdapter extends RecyclerView.Adapter 

  ArrayList appNames;
  ArrayList appImages;
  Context context;
  boolean allowed = false;
  LottieAlertDialog.Builder alert;
  public CustomAdapter(Context context, ArrayList appNames, ArrayList appImages) 
      this.context = context;
      this.appNames = appNames;
      this.appImages = appImages;
  
  @NonNull
  @Override
  public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) 
      // infalte the item Layout
      View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_layout, parent, false);
      // set the view's size, margins, paddings and layout parameters
      return new MyViewHolder(v);
  

  @Override
  public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) 
      MyViewHolder mholder=(MyViewHolder) holder;
      mholder.image.setImageResource((Integer) appImages.get(position));  
  

  @Override
  public int getItemCount() 
      return appNames.size();
  
  public static class MyViewHolder extends RecyclerView.ViewHolder 
      public ImageView image;
      public MyViewHolder(View itemView) 
          super(itemView);
          this.image = (ImageView) itemView.findViewById(R.id.image);
      
  

我所有的矢量资产的大小:

android:
android:
android:viewportWidth="512"
android:viewportHeight="512">

问题是,当我运行应用程序时,加载需要很长时间,并且设备一直冻结。我不知道为什么会这样。我该如何解决这个问题?

【问题讨论】:

【参考方案1】:

使用 Glide 等图像处理库将图像加载到 imageview 中。这可能会有所帮助

【讨论】:

以上是关于带有 Gridlayoutmanager 的 RecyclerView 加载和响应速度极慢的主要内容,如果未能解决你的问题,请参考以下文章

使用带有 GridLayoutManager 的 RecyclerView 的简单 Android 网格示例(如旧的 GridView)

如何在recyclerview的gridLayoutManager中从cardview中删除右边距

在 recyclerview Gridlayoutmanager 中创建总线布局时的间距问题

如何摆脱 GridLayoutManager 项目之间的差距

具有 2x1 排列的 Gridlayoutmanager

RecyclerView 项目不在 GridLayoutManager 的中心