Android Studio 循环视图滞后很多

vybvopom  于 2022-11-16  发布在  Android
关注(0)|答案(1)|浏览(145)

这是我的代码这是我的recyclerview适配器代码..我不知道为什么我的回收视图滞后后schroll请帮助我

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 = getActivity().getLayoutInflater();
   View _v = _inflater.inflate(R.layout.video_item, null);
   RecyclerView.LayoutParams _lp = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
   _v.setLayoutParams(_lp);
   return new ViewHolder(_v);
  }
  
  @Override
  public void onBindViewHolder(ViewHolder _holder, final int _position) {
   View _view = _holder.itemView;
   
   final LinearLayout linear1 = _view.findViewById(R.id.linear1);
   final LinearLayout linear2 = _view.findViewById(R.id.linear2);
   final LinearLayout linear4 = _view.findViewById(R.id.linear4);
   final androidx.cardview.widget.CardView cardview2 = _view.findViewById(R.id.cardview2);
   final LinearLayout linear3 = _view.findViewById(R.id.linear3);
   final LinearLayout linear7 = _view.findViewById(R.id.linear7);
   final ImageView imageview1 = _view.findViewById(R.id.imageview1);
   final TextView textview1 = _view.findViewById(R.id.textview1);
   final TextView textview2 = _view.findViewById(R.id.textview2);
   final LinearLayout linear5 = _view.findViewById(R.id.linear5);
   final TextView textview3 = _view.findViewById(R.id.textview3);
   final TextView textview4 = _view.findViewById(R.id.textview4);
   final LinearLayout linear10 = _view.findViewById(R.id.linear10);
   final ImageView imageview3 = _view.findViewById(R.id.imageview3);
   
   _shapeRadius(textview1, "#000000", 6);
   _shapeRadius(textview4, "#272731", 8);
   textview1.setTypeface(Typeface.createFromAsset(getContext().getAssets(),"fonts/spartan_regular.ttf"), 0);
   textview2.setTypeface(Typeface.createFromAsset(getContext().getAssets(),"fonts/spartan_bold.ttf"), 0);
   textview3.setTypeface(Typeface.createFromAsset(getContext().getAssets(),"fonts/spartan_regular.ttf"), 0);
   textview4.setTypeface(Typeface.createFromAsset(getContext().getAssets(),"fonts/spartan_bold.ttf"), 0);
   textview1.setTextSize((int)8);
   cardview2.setBackground(new GradientDrawable() { public GradientDrawable getIns(int a, int b) { this.setCornerRadius(a); this.setColor(b); return this; } }.getIns((int)18, 0xFF272731));
   duration = Double.parseDouble(allVideosList.get((int)_position).get("videoDuration").toString());
   videoDuration = stringForTime((int)duration);
   textview1.setText(videoDuration);
   path = allVideosList.get((int)_position).get("videoPath").toString();
   //using cardview programmatically inside listview is not good. use recyclerview instead of listview.
   androidx.cardview.widget.CardView cardview1 = new androidx.cardview.widget.CardView(VideoListFragmentActivity.this.getContext());
   cardview1.setCardElevation(0);
   cardview1.setCardBackgroundColor(0xFF272731);
   cardview1.setRadius(18);
   ViewGroup imageParent = ((ViewGroup)imageview1.getParent()); imageParent.removeView(imageview1);
   cardview1.addView(imageview1);
   imageParent.addView(cardview1);
   textview2.setText(Uri.parse(path).getLastPathSegment());
   size = Double.parseDouble(allVideosList.get((int)_position).get("videoSize").toString());
   humanReadableSize = bytesIntoHumanReadable((long)size);
   textview3.setText(allVideosList.get((int)_position).get("formattedDate").toString());
   com.bumptech.glide.Glide.with(getContext().getApplicationContext())
   .load(path)
   .transition(DrawableTransitionOptions.withCrossFade(factory))
   .placeholder(R.drawable.video_background) 
   .error(R.drawable.video_background) 
   .fitCenter() 
   .into(imageview1);
   textview4.setText(humanReadableSize);
  }
  
  @Override
  public int getItemCount() {
   return _data.size();
  }
  
  public class ViewHolder extends RecyclerView.ViewHolder {
   public ViewHolder(View v) {
    super(v);
   }
  }
 }

我试图创建一个视频播放器应用程序后,添加rcyclerview与glide图像加载器库,recyclerview是滞后滚动后,获得所有的视频从存储使用mediastore然后添加在一个列表中,并添加列表在recyclerview,但加载超过100个视频后,回收视图是滞后到多,所以请帮助我。

5fjcxozz

5fjcxozz1#

我认为你应该试着把onBindViewHolder中的东西移到onCreateViewHolder中,比如所有的setTypeface调用。你不应该每次绑定时都调用这些调用。
你需要为每个视图夹中的每个文本视图创建这些字体吗?也许你可以在其他地方加载一次,比如在构造函数中。
为什么你在onBindViewHolder中找到所有这些线性布局?它们似乎没有在代码中使用。
不过,我认为大部分延迟可能来自com.bumptech.glide.Glide组件,它看起来像是为每个项目加载一个视频。您可以尝试创建一个测试循环,它所做的就是使用此组件加载所有这些视频,并记录所需的时间。

相关问题