简体   繁体   English

安卓。 LazyAdapter 加载图像不正确

[英]Android. LazyAdapter is loading images incorrectly

I'm new to Android development.我是 Android 开发的新手。 I'm trying to load several images to ListView using LazyAdapter.我正在尝试使用 LazyAdapter 将多个图像加载到 ListView。 I store the images in the drawable folder.我将图像存储在 drawable 文件夹中。 I have four images: image1.jpg, image2.jpg, image3.jpg and image4.jpg.我有四个图像:image1.jpg、image2.jpg、image3.jpg 和 image4.jpg。 But in the ListView they go in the following order: image1.jpg, image2.jpg, image1.jpg, image2.jpg.但在 ListView 中,它们按以下顺序排列:image1.jpg、image2.jpg、image1.jpg、image2.jpg。 I tried to change the methods getItemId() and getItem() but it doesn't help to load all the images in the ListView, I still see only two of them.我试图更改 getItemId() 和 getItem() 方法,但它无助于加载 ListView 中的所有图像,我仍然只看到其中的两个。 I cannot undestand what I'm doing wrong.我无法理解我做错了什么。 Please refer to my code请参考我的代码

public class MainActivityFragment extends Fragment {

CustomAdapter imagesAdapter;

public MainActivityFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    int[] images = {R.drawable.image1,
            R.drawable.image2,
            R.drawable.image3,
            R.drawable.image4};

    imagesAdapter = new CustomAdapter(getActivity(), images);

    View rootView =  inflater.inflate(R.layout.fragment_main, container, false);
    ListView listView = (ListView) rootView.findViewById(R.id.listView);
    listView.setAdapter(imagesAdapter);

    return rootView;
}
}

Here is my LazyAdapter:这是我的 LazyAdapter:

public class CustomAdapter extends BaseAdapter {

LayoutInflater inflater;
int[] imagePaths;

public CustomAdapter(Activity activity, int[] data) {
    imagePaths = data;
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

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

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

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

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

    View view = convertView;

    if(view == null) {

        view = inflater.inflate(R.layout.list_view_item, parent, false);
        ImageView imageView = (ImageView) view.findViewById(R.id.imageView);
        imageView.setImageResource(imagePaths[position]);
    }

    return view;
}

}

Please advice.请指教。 How to make my code work correctly (ie to show all the images in the ListView, not only two of them)?如何使我的代码正常工作(即显示 ListView 中的所有图像,而不仅仅是其中两个)?

You are getting this behaviour because listview is designed in a way to re-use the items.您之所以会出现这种行为,是因为 listview 的设计方式是重复使用这些项目。

@Override

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

View view = convertView;

if(view == null) {

    view = inflater.inflate(R.layout.list_view_item, parent, false);
    ImageView imageView = (ImageView) view.findViewById(R.id.imageView);
    imageView.setImageResource(imagePaths[position]);
}

return view;

} }

This code explains this.这段代码解释了这一点。 You are creating a view only if it is null, otherwise it will re-use the already created view.仅当它为空时才创建视图,否则它将重新使用已经创建的视图。

In your case, for items 1 and 2, the view was null so the adapter created new views and set the image resources correctly, but when you scrolled down to items 3 and 4, the adapter is using already created views, so it will show the previous images.在您的情况下,对于第 1 项和第 2 项,视图为空,因此适配器创建了新视图并正确设置了图像资源,但是当您向下滚动到第 3 项和第 4 项时,适配器正在使用已创建的视图,因此它将显示以前的图像。 As LuksProg suggested, you need to move setImageResource method outside the if clause, so that even when it is reusing the view, it will update it with new correct image.正如 LuksProg 所建议的,您需要将 setImageResource 方法移到 if 子句之外,以便即使在重用视图时,它也会用新的正确图像更新它。

@Override

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

View view = convertView;

if(view == null) {

    view = inflater.inflate(R.layout.list_view_item, parent, false);
    ImageView imageView = (ImageView) view.findViewById(R.id.imageView);
}

    imageView.setImageResource(imagePaths[position]);

return view;

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM