在我的应用中,我正在保存网址链接(大约4000个网址在Arraylist中有图片)并在recyclerview中显示...它正在运行但是很多显示图像的时间。我使用了毕加索和通用图像加载程序库进行了不同的更改但无法消除图像加载中的延迟/延迟。前20,30个图像很好地显示,然后图像加载过程减慢到我可以说是app用户无法忍受的。有人可以解释一下我需要做什么或者我需要在这个代码逻辑中避免什么才能克服这个问题。下面是我的AsyncTask类
AsyncTask类:
String linkHref;
Context context;
ArrayList<String> linkArray = new ArrayList<>();
final IAsyncTaskListener listener;
String url = "http://www.**************";
public Description(Context context, IAsyncTaskListener listener) {
this.context = context;
this.listener = listener;
}
@Override
protected Void doInBackground(Void... params) {
try {
// Connect to the web site
Document document = Jsoup.connect(url).get();
Elements links = document.getElementsByTag("a");
for (Element eachLink : links) {
linkHref = eachLink.attr("href");
String baseLink = "http://www.*********.com";
baseLink = baseLink.concat(linkHref);
linkArray.add(baseLink);
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
this.listener.onCompleted(linkArray);
}
public interface IAsyncTaskListener {
void onCompleted(ArrayList<String> linkArray);
}
}
onCompleted()实现是:
@Override
public void onCompleted(ArrayList<String> linkArray) {
RecyclerView recyclerView;
RecyclerViewAdapter recyclerViewAdapter;
//linkArray.remove(0);
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
recyclerView.setLayoutManager(layoutManager);
recyclerViewAdapter = new RecyclerViewAdapter(MainActivity.this);
recyclerViewAdapter.setDataArray(linkArray);
recyclerView.setAdapter(recyclerViewAdapter);
recyclerView.setHasFixedSize(true);
recyclerViewAdapter.notifyDataSetChanged();
}
我的Adapter类是:
private LayoutInflater inflater;
Context context;
ArrayList<String> linkArray;
private int lastPosition = -1;
public RecyclerViewAdapter(Context context) {
this.context = context;
inflater = LayoutInflater.from(context);
}
public void setDataArray(ArrayList<String> linkArray) {
this.linkArray = linkArray;
}
@Override
public CustomViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview, null);
View view = inflater.inflate(R.layout.row, parent, false);
CustomViewHolder holder = new CustomViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(CustomViewHolder holder, int position) {
// ImageLoader imageLoader = ImageLoader.getInstance();
// DisplayImageOptions options = new DisplayImageOptions.Builder().cacheInMemory(true)
// .cacheOnDisc(true) .build();
//
//
////download and display image from url
// imageLoader.displayImage(String.valueOf(linkArray.get(position)), holder.image1, options);
Picasso.with(context).load(linkArray.get(position)).into(holder.image1);
}
@Override
public int getItemCount() {
return linkArray.size();
}
public class CustomViewHolder extends RecyclerView.ViewHolder {
ImageView image1;
public CustomViewHolder(View itemView) {
super(itemView);
image1 = (ImageView) itemView.findViewById(R.id.imageView);
}
}
任何帮助/指导/建议都将受到高度赞赏。