Image shuffles in list view. when scrolling

前端 未结 2 903
感情败类
感情败类 2021-01-24 04:27

What happens is that when scrolling the list, the images that were loaded on each item are shuffled, and the avatar of each item going to the next item ends. I do not know if I\

相关标签:
2条回答
  • 2021-01-24 05:24

    try this. change your getView Like this.

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
         view=convertView;
        if (convertView == null) {
            view = layoutInflater.inflate(R.layout.list_row_layout, null);
            holder = new ViewHolder();
            holder.headlineView = (TextView) view.findViewById(R.id.title);
            holder.reporterNameView = (TextView) view.findViewById(R.id.reporter);
            holder.reportedDateView = (TextView) view.findViewById(R.id.date);
            holder.imageView = (ImageView) view.findViewById(R.id.thumbImage);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
    
        NewsItem newsItem = (NewsItem) listData.get(position);
        holder.headlineView.setText(newsItem.getHeadline());
        holder.reporterNameView.setText("By, " + newsItem.getReporterName());
        holder.reportedDateView.setText(newsItem.getDate());
        if (holder.imageView != null) {
            new ImageDownloaderTask(holder.imageView).execute(newsItem.getUrl());
        }
        return view;
    }
    

    and declare View view as a class level variable hope it helps thanks. Refer Here https://androidruler.wordpress.com/2016/02/21/android-custom-listview-example/

    0 讨论(0)
  • 2021-01-24 05:27
    import android.content.Context;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.ImageView;
    import android.widget.TextView;
    
    import java.util.ArrayList;
    
    public class CustomListAdapter extends BaseAdapter {
        private ArrayList listData;
        private LayoutInflater layoutInflater;
    
        public CustomListAdapter(Context context, ArrayList listData) {
            this.listData = listData;
            layoutInflater = LayoutInflater.from(context);
        }
    
        @Override
        public int getCount() {
            return listData.size();
        }
    
        @Override
        public Object getItem(int position) {
            return listData.get(position);
        }
    
        @Override
        public long getItemId(int position) {
            return position;
        }
    
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = layoutInflater.inflate(R.layout.list_row_layout, null);
                holder = new ViewHolder();
                holder.headlineView = (TextView) convertView.findViewById(R.id.title);
                holder.reporterNameView = (TextView) convertView.findViewById(R.id.reporter);
                holder.reportedDateView = (TextView) convertView.findViewById(R.id.date);
                holder.imageView = (ImageView) convertView.findViewById(R.id.thumbImage);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
    
            NewsItem newsItem = (NewsItem) listData.get(position);
            holder.headlineView.setText(newsItem.getHeadline());
            holder.reporterNameView.setText("By, " + newsItem.getReporterName());
            holder.reportedDateView.setText(newsItem.getDate());
    
            Drawable placeholder = holder.imageView.getContext().getResources().getDrawable(R.drawable.placeholder;
                    holder.imageView.setImageDrawable(placeholder);       
    
                new ImageDownloaderTask(holder.imageView).execute(newsItem.getUrl());
    
            return convertView;
        }
    
        static class ViewHolder {
            TextView headlineView;
            TextView reporterNameView;
            TextView reportedDateView;
            ImageView imageView;
        }
    
    0 讨论(0)
提交回复
热议问题