【问题标题】:Images in ListView keep shufflingListView 中的图像不断洗牌
【发布时间】:2018-05-25 11:54:01
【问题描述】:

我是一名 Android 新手,正在尝试使用异步任务加载一些图像并将它们填充到 ListView 中。但是,使用我当前的代码,图像没有设置为正确的ImageViews,并且每当我滚动时它们都会改变。我找不到我正在犯的错误。提前谢谢你。

public class BookAdapter extends ArrayAdapter<Book>  {

    public BookAdapter(Context context, ArrayList<Book> objects) {
        super(context, 0, objects);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        View booksView = convertView;

        // inflate a view if its empty
        if (booksView == null) {
            holder = new ViewHolder();

            booksView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);

            holder.bookName = booksView.findViewById(R.id.book_name_textView);
            holder.publishedDate = booksView.findViewById(R.id.dateTextView);
            holder.bookAuthor = booksView.findViewById(R.id.author_textView);
            holder.bookImage = booksView.findViewById(R.id.book_image);


            booksView.setTag(holder);

        } else {
            holder = (ViewHolder) booksView.getTag();

        }

        Book currentBook = getItem(position);

            if (holder.bookImage != null && currentBook.getBookImage()!=null) {
                new LoadImages(holder.bookImage).execute(currentBook.getBookImage());
            }

            holder.bookName.setText(currentBook.getBookName());
            holder.bookAuthor.setText(currentBook.getAuthor());
            holder.publishedDate.setText(currentBook.getDate());

        return booksView;

    }

    private class ViewHolder {
        ImageView bookImage;
        TextView bookName;
        TextView bookAuthor;
        TextView publishedDate;
    }

}

将以下类用于异步任务

    public class LoadImages extends AsyncTask<String,Void,Bitmap>{
        private WeakReference<ImageView> imageview;
        public LoadImages(ImageView imv){
            imageview=new WeakReference<ImageView>(imv);
        }
        /** Background process
         * input:url
         * output: Bitmap image
         * It passed into onPostExecute method
         **/
        @Override
        protected Bitmap doInBackground(String... urls) {

        return QueryUtils.fetchBookImages(urls[0]);
        }

        /** This method called after the doINputBackground method
         * input:Bitmap image
         * output: image set into the image view
         * Image view  passed from RecyclerViewOperation to ShowImage class through constructor
         **/
        @Override
        protected void onPostExecute(Bitmap result) {
            if((imageview!=null)&&(result!=null)){
                ImageView imgview=imageview.get();


                if(imgview!=null){

                    imgview.setImageBitmap(result);
                }
            }
        }
    }

注意: fetchBookImages 类加载返回位图。

【问题讨论】:

    标签: java android bitmap android-asynctask


    【解决方案1】:

    请有一些众所周知的库已经解决了您面临的问题。

    还有很多其他你可以找到here

    【讨论】:

    • 谢谢 Maxime,不过我正在从头开始学习 android,我不喜欢使用 3rd 方库。
    • 不客气。您的问题与 android 中 listview 的工作方式(视图机制的回收)有关。请阅读此post,它详细解释了我在说什么。
    • 如果不使用任何第三方,我无法做到这一点。在分配图像等方面总是遇到奇怪的问题。Glide 非常棒。再次感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-20
    • 2023-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-14
    相关资源
    最近更新 更多