RecyclerView形象不断刷新,当上下滚动,我们

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;

}

}

}RecyclerView形象不断刷新,当上下滚动,我们

我知道这个问题已经被问过。但我无法解决我的问题。我以json的形式获取数据。图像通过异步任务加载。但是当我向上滚动或向下滚动时,互相替换,然后几秒钟后,图像显示在原始位置。我认为问题出在OnBind方法中。

回答:

不要让API调用onBind
在活动
2. onPostExecute {}设置适配器& 1.进行API调用

recyclerview.setAdapter(new YourAdapter(dataList))<br> 

如果你想上的滚动API调用列表中,在onScroll方法中编写逻辑

recyclerView.addOnScrollListener 

如果要更改图像wrt position of recyclerview使用Y图像加载库:例如:方
http://square.github.io/picasso/

Picasso.with(context).load(url).into(view); 

以上是 RecyclerView形象不断刷新,当上下滚动,我们 的全部内容, 来源链接: utcz.com/qa/265082.html

回到顶部