我花了两天时间试图找到解决此问题的方法,却无法在任何地方找到它。我的问题是:我有一个列表视图,其中每个项目有4个文本视图和一个ImageView。我正在使用Volley从网站上获取数据。数据正常加载,当我向下滚动时,一切都完美无缺。问题是当我向后滚动时。当我向后滚动时,图像和文本视图会出现0.5延迟(首先我看到最后一项的图片),然后显示内容。我已经使用viewholder尝试了recyclelerview和listview。两种解决方案都存在这种“轻弹”现象。
public class BigCategoryListViewAdapter extends BaseAdapter{
private Context context;
private String[] smallCatsList;
private News news;
private ImageLoader imageLoader;
static class myViewHolder{
TextView smallCatName;
RelativeLayout spinner;
ImageView newsImageView;
TextView newsTitle;
TextView newsContent;
}
public BigCategoryListViewAdapter(Context context, String[] smallCatsList){
this.context = context;
this.smallCatsList = smallCatsList;
this.imageLoader = ImageLoader.getInstance();
if(!this.imageLoader.isInited()) { this.imageLoader.init(ImageLoaderConfiguration.createDefault(context));}
}
@Override
public int getCount() {
return smallCatsList.length;
}
@Override
public Object getItem(int position) {
return smallCatsList[position];
}
@Override
public long getItemId(int position) {return 0;} //modificar aqui
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final myViewHolder mvh;
LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.bigcat_listview,null);
mvh = new myViewHolder();
mvh.smallCatName = (TextView) convertView.findViewById(R.id.bigcat_viewpager_smallcat);
mvh.spinner = (RelativeLayout) convertView.findViewById(R.id.spinner);
mvh.newsImageView = (ImageView) convertView.findViewById(R.id.bigcat_viewpager_imageView);
mvh.newsTitle = (TextView) convertView.findViewById(R.id.bigcat_viewpager_news_title);
mvh.newsContent = (TextView) convertView.findViewById(R.id.bigcat_viewpager_content);
convertView.setTag(mvh);
mvh.smallCatName.setText(smallCatsList[position]);
JSONObject params = new JSONObject();
try{
params.put("slug",smallCatsList[position]);
params.put("startIndex", 0);
params.put("endIndex", 0);
}catch (Exception e) {
Log.e(getClass().toString(), "Error setting params for communication with server");
e.printStackTrace();
}
Volley volley = Volley.getVolley(context);
volley.runRequest(Request.Method.POST, VolleyConstants.PROD_URL + VolleyConstants.NEWS_FETCH_ROUTE,
params, new Response.Listener<JSONObject>() {
//---------------------Volley------------------//
@Override
public void onResponse(JSONObject response) {
JSONArray responseArr = null;
try {
responseArr = response.getJSONArray("newsArr");
} catch (Exception e) {
e.printStackTrace();
}
for (int i = 0; i < responseArr.length(); i++) {
try {
JSONObject object = responseArr.getJSONObject(i);
String photoURLString = object.getString("photo");
String titleString = object.getString("title");
String publisherString = object.getString("publisher");
String dateString = object.getJSONObject("date").getString("date");
String contentString = object.getString("contents");
String urlString = object.getString("url");
news = new News(photoURLString, titleString, publisherString, dateString, contentString,urlString);
} catch (Exception e) {
news = new News("Unknown", "Unknown", "Unknown", "Unknown", "Unknown","Unknown");
}
}
//tv2.setText(news.getNewsDate());
mvh.newsContent.setText(news.getNewsContent());
mvh.newsTitle.setText(news.getNewsHead());
//---------------------Image Loading------------------//
try {
URL url = new URL(news.getNewsPic());
imageLoader.loadImage(news.getNewsPic(), new ImageLoadingListener() {
@Override
public void onLoadingStarted(String imageUri, View view) {
}
@Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
}
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
mvh.newsImageView.setImageBitmap(loadedImage);
mvh.spinner.setVisibility(View.GONE);
}
@Override
public void onLoadingCancelled(String imageUri, View view) {
}
});
}catch(Exception e){
mvh.newsImageView.setImageDrawable(context.getResources().getDrawable(R.drawable.nopic));
mvh.spinner.setVisibility(View.GONE);
}
}
}, new CustomErrorListener("ERROR"));
return convertView;
}
这是我的适配器。我的问题是,是否有办法消除此延迟以显示内容?或者是我的网络或列表视图本身的问题? 我只想加载所有内容一次,然后用户可以上下滚动,而不必每次都刷新内容。
答案 0 :(得分:0)
您每次调用getView时都会加载内容,这就是为什么它会像这样加载的原因。将Volley请求与getView分开,它应该完全在适配器之外,而不是在每个列表的每个负载上调用。你有你的新闻对象,你可以将一个ArrayList传递给适配器并从那里填充它,这将解决你的加载问题。此外,您在主线程而不是新线程中调用volley,您应该将两者分开,并在完成正在加载的内容时填充ListView适配器。
答案 1 :(得分:0)
其一,ViewHolder模式示例应该是这样的:
if (convertView == null) {
convertView = inflater.inflate(R.layout.bigcat_listview,null);
mvh = new myViewHolder();
//... rest of your code
} else {
mvh = (myViewHolder) convertView.getTag();
//... rest of your code
}
其次,主要的Volley加载东西不应该在适配器中,而是事先在别处处理然后传入。这个example应该可以帮到你很多。