我一直在使用回收站视图在片段上显示15张图像。图像位于可绘制文件夹中。我尝试使用外部视图来执行此操作,但是它的速度要慢得多,并且滞后很多。因此,我使用了回收站视图来解决此问题。
片段仅需8秒钟即可打开,而且非常缓慢。
任何出路还是替代方案?
 这是我的片段代码:

public class GifFragment extends Fragment {
private View view;
RecyclerView rvMain;

public GifFragment() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    view=inflater.inflate(R.layout.fragment_gif,container,false);


    rvMain =  view.findViewById(R.id.rvMain);

    int sticker[]={R.drawable.kitty1, R.drawable.kitty2, R.drawable.kitty3, R.drawable.kitty4, R.drawable.kitty5
            , R.drawable.kitty6, R.drawable.kitty7, R.drawable.kitty8, R.drawable.kitty9, R.drawable.kitty10
            , R.drawable.kitty11, R.drawable.kitty12, R.drawable.kitty13, R.drawable.kitty14, R.drawable.kitty15};

    MyAdapter adapter = new MyAdapter(sticker);
    rvMain.setLayoutManager(new GridLayoutManager(getContext(), 3));
    adapter.setHasStableIds(true);
    rvMain.setAdapter(adapter);
    return view;
}

private class MyAdapter extends RecyclerView.Adapter<MyViewHolder> {


    int sticker[];

    public MyAdapter( int sticker[]) {
        this.sticker=sticker;
    }


    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item, parent, false);
        MyViewHolder viewHolder = new MyViewHolder(v);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {
        holder.logo.setImageResource(sticker[position]);
        holder.logo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getActivity(), "This is: " , Toast.LENGTH_SHORT).show();
            }
        });

    }

    @Override
    public int getItemCount() {
        return sticker.length;
    }
}
private class MyViewHolder extends RecyclerView.ViewHolder{

    public ImageView logo;
    public TextView name;

    public MyViewHolder(View itemView) {
        super(itemView);
        logo = (ImageView)itemView.findViewById(R.id.ivLogo);

    }
}


}

最佳答案

使用Glide可以有效地加载图像。


在应用模块gradle中添加此

repositories {
  mavenCentral()
  google()
}

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


然后使用滑行从drawable加载图像

Glide.with(this).load(R.drawable.image_name).into(imageView);




onBindViewHolder做到这一点

Glide.with(this).load(sticker[position]).into(holder.logo);

关于android - 回收站 View 非常落后,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56489104/

10-12 04:26