RecyclerView的加载时间问题

时间:2019-04-24 19:49:04

标签: java android android-recyclerview recycler-adapter

我正在开发一个列表中包含50多个项目的应用,问题是我的RecyclerView打开得太晚了(例如,在另一活动中点按以打开它的1秒后)。我在TextView卡中使用了两个RecyclerView和一张图像。数据存储在本地的字符串中,图像来自可绘制文件夹。有人对此有解决方案吗?

我尝试设置hasfixedsize=true,但是没有用。

这是适配器

public class HotelRecyclerViewAdapter extends RecyclerView.Adapter<HotelRecyclerViewAdapter.HotelViewHolder> {
    public ArrayList<HotelItem> mArrayList;
    public OnItemClickListener mListener;

    public interface OnItemClickListener {
        public void OnClic(int position);
    }

    public void SetOnItemClickListener(OnItemClickListener onItemClick) {
        mListener = onItemClick;
    }

    public static class HotelViewHolder extends RecyclerView.ViewHolder {
        TextView mTextView;
        ImageView mImageView;
        TextView star;

        public HotelViewHolder(@NonNull View itemView, final OnItemClickListener listener) {
            super(itemView);
            mTextView = itemView.findViewById(R.id.t1);
            mImageView = itemView.findViewById(R.id.image);
            star = itemView.findViewById(R.id.star);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        int pos = getAdapterPosition();
                        if (pos != RecyclerView.NO_POSITION) {
                            listener.OnClic(pos);
                        }
                    }
                }
            });

        }
    }

    public HotelRecyclerViewAdapter(ArrayList<HotelItem> items) {
        mArrayList = items;
    }

    @NonNull
    @Override
    public HotelViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.hotelitem, viewGroup, false);
        HotelViewHolder viewHolder = new HotelViewHolder(view, mListener);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull HotelViewHolder hotelViewHolder, int i) {
        HotelItem curretitem = mArrayList.get(i);
        hotelViewHolder.mImageView.setImageResource(curretitem.getImageid());
        hotelViewHolder.mTextView.setText(curretitem.getTitle());
        hotelViewHolder.star.setText(curretitem.getStar());
    }

    @Override
    public int getItemCount() {
        if (mArrayList == null) {
            return 0;
        } else {
            return mArrayList.size();
        }
    }
}

MainActivity如下所示,其中我设置了RecyclerView

mView = findViewById(R.id.recyler);
mView.setHasFixedSize(true);
mManager = new LinearLayoutManager(this);
mView.setLayoutManager(mManager);
mAdapter = new HotelRecyclerViewAdapter(hotelItems);
mView.setAdapter(mAdapter);
mAdapter.notifyDataSetChanged();

问题正在模拟器中发生。它可以在物理设备上正常工作。

1 个答案:

答案 0 :(得分:0)

我认为问题在于图像加载,因为您直接将图像设置为ImageView。我建议使用Glide将图像加载到ImageView中,这样可以在不暂停主UI线程的情况下将图像获取到后台。

Glide.with(this)
    .load(getImage(drawable_image_name))
    .into(hotelViewHolder.mImageView);

// This is just a function for getting images from your drawables. 
public int getImage(String imageName) {
    int drawableResourceId = this.getResources().getIdentifier(imageName, "drawable", this.getPackageName());

    return drawableResourceId;
}

要在项目中添加Glide,您需要在build.gradle中添加以下内容。请检查here了解更多信息。

repositories {
  mavenCentral()
  google()
}

dependencies {
  implementation 'com.github.bumptech.glide:glide:4.9.0'
  annotationProcessor 'com.github.bumptech.glide:compiler:4.9.0'
}