public class Adapter1 extends RecyclerView.Adapter<Adapter1.ViewHolder> {
ArrayList<CategoryModels> categoryList;
Context context;
public Adapter1(ArrayList<CategoryModels> categoryList, Context context){
this.categoryList =categoryList;
this.context = context;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_scategory_single_layout,null);
final ViewHolder viewHolder=new ViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
CategoryModels categoryModels = categoryList.get(position);
holder.name.setText(categoryModels.getName());
int pos = getItemViewType(position);
if(categoryList.get(pos).getImage() == null) {
holder.image.setVisibility(View.GONE);
} else
new DownloadImageTask(holder.image).execute(baseurl+categoryModels.getImage());
//Picasso.with(categoryList.ge).load("http://www.zawallpaper.com/includes/images/"+categoryModels.getImage()).resize(120, 60).into(holder.image);
}
@Override
public int getItemViewType(int position) {
return super.getItemViewType(position);
}
@Override
public int getItemCount() {
return categoryList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
TextView name;
ImageView image;
public ViewHolder(View itemView) {
super(itemView);
name = (TextView) itemView.findViewById(R.id.name);
image = (ImageView) itemView.findViewById(R.id.imgcategory);
}
}
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
public DownloadImageTask(ImageView bmImage) {
this.bmImage = bmImage;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
//ProgressDialogue.hideProgressDialogue();
}
}
public Bitmap StringToBitMap(String encodedString) {
try {
byte[] encodeByte = Base64.decode(encodedString, Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length);
return bitmap;
} catch (Exception e) {
e.getMessage();
return null;
}
}
}
我知道之前已经问过这个问题。但我无法解决我的问题。我正在以json的形式获取数据。图像通过异步任务加载。但是,当我向上或向下滚动时,互相替换,然后在几秒钟后,图像以原始位置显示。我认为问题出在OnBind Method上。
答案 0 :(得分:0)
不要在onBind上打电话给api
1.在活动中打电话api
2.&amp;在onPostExecute
{}设置适配器
recyclerview.setAdapter(new YourAdapter(dataList))<br>
如果要在滚动列表时进行Api调用,请在onScroll方法中编写逻辑
recyclerView.addOnScrollListener
如果要更改图像,请使用recyclelerview的位置使用任何图像加载库:例如:方形
http://square.github.io/picasso/
Picasso.with(context).load(url).into(view);