【发布时间】:2017-07-25 15:05:05
【问题描述】:
我正在尝试使用 Recycler 视图和 Sub Sampling Gallery 来实现 Gallery 应用程序。 由于我的图像计数约为 850。当我尝试将图像加载到图库时,图库滞后。
这是我的 Recyclerview 适配器:-
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.RecyclerViewHolders> {
private ArrayList<String> yeniliste;
private Context context;
public RecyclerViewAdapter(Context context, ArrayList<String> itemList) {
this.yeniliste = itemList;
this.context = context;
}
@Override
public RecyclerViewHolders onCreateViewHolder(ViewGroup parent, int viewType) {
View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.gallery_item, null);
RecyclerViewHolders rcv = new RecyclerViewHolders(layoutView);
return rcv;
}
@Override
public void onBindViewHolder(final RecyclerViewHolders holder, final int position) {
try {
Bitmap bitmap = BitmapFactory.decodeFile(yeniliste.get(position));
holder.countryPhoto.setImage(ImageSource.bitmap(bitmap).dimensions(50,50));
}catch (Exception e){
e.printStackTrace();
}
holder.countryPhoto.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(),GalleryFullImage.class);
intent.putExtra("realid",String.valueOf(holder.getAdapterPosition()));
v.getContext().startActivity(intent);
}
});
}
@Override
public int getItemCount() {
return this.yeniliste.size();
}
public static class RecyclerViewHolders extends RecyclerView.ViewHolder implements View.OnClickListener{
public SubsamplingScaleImageView countryPhoto;
public RecyclerViewHolders(View itemView) {
super(itemView);
countryPhoto = (SubsamplingScaleImageView)itemView.findViewById(R.id.country_photo);
}
@Override
public void onClick(View view) {
Toast.makeText(view.getContext(), "Clicked Country Position = " + getAdapterPosition(), Toast.LENGTH_SHORT).show();
}
}
public void removeItem(int position)
{
yeniliste.remove(position);
notifyDataSetChanged();
}}
【问题讨论】:
-
这可能需要太长时间:“Bitmap bitmap = BitmapFactory.decodeFile(yeniliste.get(position));”尝试使用像 picasso 或 glide 这样的库。主要原因是在主线程中完成。如果您不想使用库,则需要在不同的线程中执行该操作
-
我尝试用毕加索加载,但是图片没有加载
-
你试过滑翔吗:stackoverflow.com/questions/34443171/…。如果是毕加索,你可以检查一下:stackoverflow.com/questions/24097576/…
-
但是无论如何.. 原因是解码到位图的操作很昂贵,因此您需要在不同的线程中执行此操作。这些库以干净的方式为您做到这一点
-
String completePath = yeniliste.get(position);文件文件 = 新文件(完整路径); Uri imageUri = Uri.fromFile(file); Glide.with(context) .load(imageUri) .into(holder.countryPhoto);
标签: android image performance android-recyclerview