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

问题描述

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

  • 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_width="match_parent"
     android:layout_height="match_parent"
     tools:context=".MainActivity">
    
     <androidx.recyclerview.widget.RecyclerView
         android:id="@+id/recycle"
         android:layout_width="match_parent"
         android:layout_height="match_parent" />
     </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:width="5dp"
          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_width="wrap_content"
      android:layout_height="180dp"
      android:background="@drawable/custom_item_layout"
      android:padding="5dp">
      <!--
      grid items for RecyclerView
      -->
      <ImageView
          android:id="@+id/image"
          android:layout_width="180dp"
          android:layout_height="180dp"
          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:width="512dp"
android:height="512dp"
android:viewportWidth="512"
android:viewportHeight="512">

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

解决方法

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