2
我想創建一個從服務器下載圖像的片段的RecyclerView,但我遇到了一些問題: 當滾動到快速,你可以看到以前的圖片,我真的不希望這樣。第二件事是我有一些OOM崩潰,我真的不知道該怎麼做,一些更有經驗的程序員可以幫助我嗎?用加載圖像創建RecyclerView的正確方法是什麼?
我的片段適配器代碼:
public class MyPliksAdapter extends RecyclerView.Adapter <MyPliksAdapter.MyViewHolder>{
private LayoutInflater inflater;
private List<MyPlikClass> myPliksList = Collections.emptyList();
VolleySingleton volleySingleton;
ImageLoader imageLoader;
public MyPliksAdapter (Context context, List<MyPlikClass> myPliks) {
inflater = LayoutInflater.from(context);
myPliksList =myPliks;
volleySingleton = VolleySingleton.getsInstance();
imageLoader = volleySingleton.getImageLoader();
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.fragment_my_pliks_single,parent,false);
MyViewHolder holder = new MyViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(final MyViewHolder holder, int position) {
final MyPlikClass currentItem = myPliksList.get(position);
//TODO:
if (currentItem.myPlikUserFromImgUrl != null){
imageLoader.get(currentItem.myPlikUserFromImgUrl, new ImageLoader.ImageListener() {
@Override
public void onResponse(ImageLoader.ImageContainer imageContainer, boolean b) {
holder.myPlikUserFromImg.setImageBitmap(imageContainer.getBitmap());
}
@Override
public void onErrorResponse(VolleyError volleyError) {
}
});
}
if (currentItem.myPlikPhotoUrl != null){
holder.myPlikPhoto.setImageUrl(currentItem.myPlikPhotoUrl,imageLoader);
//(, "Wrong sesion, pls relogin", Toast.LENGTH_SHORT).show();
}
//holder.myPlikGetDate.setText(currentItem.myPlikGetDate);
byte[] titleData = Base64.decode(currentItem.myPlikTitle, Base64.DEFAULT);
byte[] descriptionData = Base64.decode(currentItem.myPlikDescription, Base64.DEFAULT);
byte[] nameData = Base64.decode(currentItem.myPlikUserFromName, Base64.DEFAULT);
//byte[] data = Base64.decode(currentItem.myPlikTitle, Base64.DEFAULT);
String title = null;
String name = null;
String description = null;
try {
title = new String(titleData, "UTF-8");
name = new String(nameData, "UTF-8");
description = new String(descriptionData, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
if(description != null && !description.isEmpty()) {
holder.myPlikDeskription.setText(description);
holder.descriptionLayout.setVisibility(View.VISIBLE);
}
holder.myPlikTitle.setText(title);
holder.myPlikUserFromName.setText(name);
holder.myPlikUserFromNickname.setText(currentItem.myPlikUserFromNickname);
}
@Override
public int getItemCount() {
return myPliksList.size();
}
class MyViewHolder extends RecyclerView.ViewHolder {
NetworkImageView myPlikPhoto;
ImageView myPlikUserFromImg;
TextView myPlikUserFromName;
ImageView myPlikDeskriptionIcon;
TextView myPlikDeskription;
TextView myPlikUserFromNickname;
//TextView myPlikGetDate;
TextView myPlikTitle;
RelativeLayout descriptionLayout;
//RecyclerView recyclerView;
//GalleryAdapter adapter;
public MyViewHolder(View itemView) {
super(itemView);
myPlikTitle = (TextView) itemView.findViewById(R.id.myPlikTitle);
myPlikUserFromName = (TextView) itemView.findViewById(R.id.myPlikUserFromName);
myPlikUserFromNickname = (TextView) itemView.findViewById(R.id.myPlikUserFromNickname);
myPlikUserFromImg = (ImageView) itemView.findViewById(R.id.myPlikUserFromImg);
myPlikPhoto = (NetworkImageView) itemView.findViewById(R.id.profile_plick_content_img);
myPlikDeskription = (TextView) itemView.findViewById(R.id.profile_plick_content_description);
myPlikDeskriptionIcon = (ImageView) itemView.findViewById(R.id.profile_plik_content_comment_icon);
descriptionLayout = (RelativeLayout) itemView.findViewById(R.id.descripdion_block);
//adapter = new GalleryAdapter(inflater.getContext(), getImgIds());
//recyclerView = (RecyclerView) itemView.findViewById(R.id.galleryList);
}
}
}
謝謝,真的。 ) –