如何在滚动时保持ListView项的状态?

时间:2015-04-15 05:59:22

标签: android android-listview

我正在使用listview作为列表项

  1. 的TextView
  2. ImageView(显示附件图标)
  3. 我正在使用扩展ArrayAdapter并使用ViewHolder的自定义适配器,并且在自定义适配器中,如果存在附件,我会检查条件。如果为true,我会ImageView可见。一切正常,但滚动时ImageView消失了。

    public class NoticeListAdapter  extends ArrayAdapter<String>{
    
        private final Activity context;
        private  List<String> img_url = new ArrayList<String>();
        private  List<String>  heading = new ArrayList<String>();
        private  List<Long>  createdDate = new ArrayList<Long>();
        private  List<String>  documentCategoryId = new ArrayList<String>();
        Date date = new Date();
    
        public NoticeListAdapter(Activity context, List<String> img_url, List<String> heading, List<Long> createdDate, List<String> documentCategoryId) {
            super(context, R.layout.attendance_list_item, heading);
            // TODO Auto-generated constructor stub
            this.context=context;
            this.img_url = img_url;
            this.heading = heading;
            this.createdDate = createdDate;
            this.documentCategoryId = documentCategoryId;
        }
    
        public static class ViewHolder {
            ImageView profilePicture;
            ImageView attachmentIcon;
            TextView txtemployeeName;
            TextView txtbulletinDate;
        }
    
        @Override
        public View getView(final int position, View view, ViewGroup parent){
    
            ViewHolder viewHolder;
            if(view == null){
                viewHolder = new ViewHolder();
                LayoutInflater inflater = LayoutInflater.from(getContext());
                view = inflater.inflate(R.layout.notice_list_item, parent, false);
                viewHolder.profilePicture = (ImageView) view.findViewById(R.id.profilePic);
                viewHolder.txtemployeeName = (TextView) view.findViewById(R.id.employeeName);
                viewHolder.txtbulletinDate = (TextView) view.findViewById(R.id.bulletinDate);
                viewHolder.attachmentIcon = (ImageView) view.findViewById(R.id.attachmentIcon);
                view.setTag(viewHolder);
            }else{
                viewHolder = (ViewHolder)view.getTag();
            }
    
    
            String description = heading.get(position);
            description = description.length()>40?description.substring(0,40)+"...":description;
    
            SharedPreferences pref = getContext().getSharedPreferences("authDetails", Context.MODE_PRIVATE);
            ImageLoader imageLoader = new ImageLoader(getContext());
            imageLoader.DisplayImage(pref.getString("base_url", "")+img_url.get(position), viewHolder.profilePicture);
    
            viewHolder.txtemployeeName.setText(description);
            viewHolder.txtbulletinDate.setText(DateUtils.getRelativeTimeSpanString(createdDate.get(position),  date.getTime(),  DateUtils.MINUTE_IN_MILLIS, DateUtils.FORMAT_ABBREV_RELATIVE));
    
            if(documentCategoryId.get(position).equalsIgnoreCase("0"))
                viewHolder.attachmentIcon.setVisibility(View.GONE);
            return view;
        }
    
    }
    

3 个答案:

答案 0 :(得分:2)

只需更改您的密码:

if(documentCategoryId.get(position).equalsIgnoreCase("0"))
        viewHolder.attachmentIcon.setVisibility(View.GONE);

是:

if(documentCategoryId.get(position).equalsIgnoreCase("0"))
        viewHolder.attachmentIcon.setVisibility(View.GONE);
else
        viewHolder.attachmentIcon.setVisibility(View.VISIBLE);

答案 1 :(得分:0)

此问题来自您的自定义ImageLoader实施。

粘贴你的代码,有一些错误没有显示图像。

或者,您可以从URL下载图片,并将其设置为profilePicture ImageView的来源,如下所示:

URL url = new URL(img_url.get(position));
Bitmap bmp = BitmapFactory.decodeStream(url.openConnection().getInputStream());
viewHolder.profilePicture.setImageBitmap(bmp);

答案 2 :(得分:0)

您的代码中存在ViewHolder实现问题。您不将持有者结果设置为非空视图。而且,不要检索最近创建的持有者。

尝试改变:

 view.setTag(viewHolder);
 }else{
 viewHolder = (ViewHolder)view.getTag();
 }

到这个

 view.setTag(viewHolder);
 }
 viewHolder = (ViewHolder)view.getTag();

return view;之前:

view.setTag(viewHolder);

试试这个,固定代码:

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

    ViewHolder viewHolder;
    if(view == null){
        viewHolder = new ViewHolder();
        LayoutInflater inflater = LayoutInflater.from(getContext());
        view = inflater.inflate(R.layout.notice_list_item, parent, false);
        viewHolder.profilePicture = (ImageView) view.findViewById(R.id.profilePic);
        viewHolder.txtemployeeName = (TextView) view.findViewById(R.id.employeeName);
        viewHolder.txtbulletinDate = (TextView) view.findViewById(R.id.bulletinDate);
        viewHolder.attachmentIcon = (ImageView) view.findViewById(R.id.attachmentIcon);
        view.setTag(viewHolder);
    }
    viewHolder = (ViewHolder)view.getTag();

    String description = heading.get(position);
    description = description.length()>40?description.substring(0,40)+"...":description;

    SharedPreferences pref = getContext().getSharedPreferences("authDetails", Context.MODE_PRIVATE);
    ImageLoader imageLoader = new ImageLoader(getContext());
    imageLoader.DisplayImage(pref.getString("base_url", "")+img_url.get(position), viewHolder.profilePicture);

    viewHolder.txtemployeeName.setText(description);
    viewHolder.txtbulletinDate.setText(DateUtils.getRelativeTimeSpanString(createdDate.get(position),  date.getTime(),  DateUtils.MINUTE_IN_MILLIS, DateUtils.FORMAT_ABBREV_RELATIVE));

    if(documentCategoryId.get(position).equalsIgnoreCase("0"))
        viewHolder.attachmentIcon.setVisibility(View.GONE);
    view.setTag(viewHolder);
    return view;
}