滚动

时间:2017-09-09 08:31:18

标签: android listview

这是我的代码:

public class GetAllCategoriesListViewAdapter extends BaseAdapter{

private JSONArray dataArray;
private Activity activity;

private static final String baseUrlForCategoryImage = "link here";

private static LayoutInflater inflater = null;

public GetAllCategoriesListViewAdapter(JSONArray jsonArray, Activity a){
    this.dataArray = jsonArray;
    this.activity = a;

        inflater = (LayoutInflater) this.activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
    return this.dataArray.length();
}

@Override
public Object getItem(int position) {
    return position;
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    final ListCell cell;
    if(convertView == null){
        convertView = inflater.inflate(R.layout.get_all_categories_list_view_cell, null);
        cell = new ListCell();

        cell.category_name = (TextView) convertView.findViewById(R.id.category_name);
        cell.category_image = (ImageView) convertView.findViewById(R.id.category_image);
        cell.category_image.setTag(cell);
        convertView.setTag(cell);
    }else{
        cell = (ListCell) convertView.getTag();
    }

    try{
        JSONObject jsonObject = this.dataArray.getJSONObject(position);
        cell.category_name.setText(jsonObject.getString("category_name"));

        String nameOfImage = jsonObject.getString("category_image");

        String urlForImageInServer = baseUrlForCategoryImage + nameOfImage;

        new AsyncTask<String, Void, Bitmap>(){

            protected Bitmap doInBackground(String... params){
                String url = params[0];
                Bitmap icon = null;

                try{
                    InputStream in = new java.net.URL(url).openStream();
                    icon = BitmapFactory.decodeStream(in);

                }catch (MalformedURLException e){
                    e.printStackTrace();
                }catch (IOException e){
                    e.printStackTrace();
                }

                return icon;
            }

            @Override
            protected void onPostExecute(Bitmap result) {
                cell.category_image.setImageBitmap(result);
            }

        }.execute(urlForImageInServer);
    }catch (JSONException e){
        e.printStackTrace();
    }
    return convertView;
}

private class ListCell{
    private ImageView category_image;
    private TextView category_name;
}
}

代码从我的webhost获取图像并将其放在listvew中的每个单元格中。问题是每次我滚动,图像被洗牌并在几秒钟后返回。滚动时如何阻止图像变化?我尝试在其他帖子上使用该解决方案,但它不起作用。请帮忙。

1 个答案:

答案 0 :(得分:0)

看起来你是Android新手。所以你在getView方法中获取图像。每次绘制新的列表项时都会调用getView方法。因此,对于每个图像,都会向互联网发出新请求。这将是很多要求。你应该首先获取你的图像并将它们放入一些ArryayList中。然后将Arraylist传递给适配器。这是你的教程

使用AsyncTask http://www.devexchanges.info/2015/04/android-custom-listview-with-image-and.html

使用Volley https://www.androidhive.info/2014/07/android-custom-listview-with-image-and-text-using-volley/

为了更好的表现而去排球。干杯!