【问题标题】:How to load image in imageview only when on screen in RecyclerView仅在 RecyclerView 的屏幕上时如何在 imageview 中加载图像
【发布时间】:2026-01-28 23:35:01
【问题描述】:

我正在尝试制作一个包含来自存储或网络的无限图像的应用程序。但问题是 recyclerview 卡住或滚动非常缓慢。

我如何在我的应用程序中制作图像列表,例如图库,什么应用程序、Instagram 等?
我想非常顺利地运行我的应用程序

请帮助我!

适配器代码:

public class Recyclerview1Adapter extends RecyclerView.Adapter<Recyclerview1Adapter.ViewHolder> {
      ArrayList<HashMap<String, Object>> _data;
      public Recyclerview1Adapter(ArrayList<HashMap<String, Object>> _arr) {
          _data = _arr;
      }
      
      @Override
      public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
          LayoutInflater _inflater = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
          View _v = _inflater.inflate(R.layout.custom, null);
          return new ViewHolder(_v);
      }
      
      @Override
      public void onBindViewHolder(ViewHolder _holder, final int _position) {
          View _view = _holder.itemView;
          
          final LinearLayout linear1 = (LinearLayout) _view.findViewById(R.id.linear1);
          final ImageView imageview1 = (ImageView) _view.findViewById(R.id.imageview1);
          final TextView textview1 = (TextView) _view.findViewById(R.id.textview1);
          
          textview1.setText(_data.get((int)_position).get("image-name").toString());
          imageview1.setImageBitmap(FileUtil.decodeSampleBitmapFromPath(_data.get((int)_position).get("image-path").toString(), 1024, 1024));
      }
      
      @Override
      public int getItemCount() {
          return _data.size();
      }
      
      public class ViewHolder extends RecyclerView.ViewHolder{
          public ViewHolder(View v){
              super(v);
          }
      }
      
  }

【问题讨论】:

  • 我建议改用线性布局
  • 显示你的适配器实现,总是发布一些不适合你的代码,没有任何 sn-p 所有人都在猜测
  • 请解释如何做 Shivansh Potdar
  • 请不要,那是错误的建议,他需要回收,因为他有“来自存储或网络的无限图像”集合

标签: android performance android-recyclerview smooth-scrolling


【解决方案1】:

你需要用适当的描述清楚地解释问题。

根据我的理解,您一次获取所有图像并将它们传递给 recyclerview 适配器。如果有很多图像,这可能会导致滚动问题。

您需要在运行时使用分页加载有限的图像。仅当用户到达 recyclerview 末尾时才加载新图像。

查看官方 Paging documentation 或此 GitHub library

【讨论】:

    最近更新 更多