Bad data interpretation when scrolling of Listview

时间:2015-06-26 10:13:45

标签: android android-listview android-arrayadapter textview android-recyclerview

I wrote code with custom ListView. In ListView I have a textView that is hidden by certain circumstances. Everything works well when I scroll view down. However, when I scroll view up all textVie are hidden. I know it has something to do with setting tags for elements. I tried examples from different sides, however, the textView's always disappear :( My custom ArrayAdapter code:

class WallpapersListAdapter extends ArrayAdapter<Wallpaper> {
    private List<Wallpaper> wallpapersContainer;
    private ImageLoader imageLoader;
    private DisplayImageOptions imageLoaderOptions;

public WallpapersListAdapter(Context context, List<Wallpaper> wallpapersContainer) {
    super(context, R.layout.wallpaper_view, wallpapersContainer);
    this.wallpapersContainer = wallpapersContainer;
    setImageLoaderOptions();
}

private void setImageLoaderOptions() {
    imageLoaderOptions = new DisplayImageOptions.Builder()
            //.showImageOnLoading(R.drawable.place_holder)
            //.showImageForEmptyUri(R.drawable.question)
            //.showImageOnFail(R.drawable.big_problem)
            .imageScaleType(ImageScaleType.EXACTLY)
            .cacheInMemory(true)
            .cacheOnDisk(true)
            .build();
    imageLoader = ImageLoader.getInstance();
}

@Override
public int getCount() {
    return wallpapersContainer.size();
}

@Override
public Wallpaper getItem(int position) {
    Wallpaper wallpaper = wallpapersContainer.get(position);
    return wallpaper;
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View itemView = convertView;
    WallpaperViewHolder currentWallpaperViewHolder;
    //            do we have a view
    if (convertView == null) {
 //                we don't have a view so create one by inflating the layout
        itemView = LayoutInflater.from(getContext()).inflate(R.layout.wallpaper_view, parent, false);
        itemView.getLayoutParams().height = parent.findViewById(R.id.wallpapers_list_view).getMeasuredHeight();
        currentWallpaperViewHolder = getWallpaperViewHolder(itemView);
//                set the tag for this view to the current person view holder
        itemView.setTag(currentWallpaperViewHolder);
        itemView.setTag(R.id.wallpaper_author, currentWallpaperViewHolder.wallpaperAuthor);
        itemView.setTag(R.id.wallpaper_site, currentWallpaperViewHolder.wallpaperSite);
    } else {
//             we have a view so get the tagged view
        currentWallpaperViewHolder = (WallpaperViewHolder) itemView.getTag();
    }
    currentWallpaperViewHolder.wallpaperAuthor.setTag(position);
    currentWallpaperViewHolder.wallpaperSite.setTag(position);
    setWallpaperViewContent(wallpapersContainer.get(position), currentWallpaperViewHolder);
    return itemView;
}

private void setWallpaperViewContent(Wallpaper wallpaper, WallpaperViewHolder wallpaperViewHolder) {
    String authorName = wallpaper.getAuthorName();
    String authorSite = wallpaper.getAuthorSite();
    if (authorName == null) {
        wallpaperViewHolder.wallpaperAuthor.setVisibility(View.GONE);
    } else {
        wallpaperViewHolder.wallpaperAuthor.setText(getContext().getString(R.string.wallpaper_author_name) + " " + authorName);
    }
    if (authorSite == null) {
        wallpaperViewHolder.wallpaperSite.setVisibility(View.GONE);
    } else {
        wallpaperViewHolder.wallpaperSite.setText(getContext().getString(R.string.wallpaper_author_site) + " " + authorSite);
    }
    wallpaperViewHolder.wallpaperDateForUse.setText(wallpaper.getDateForUse());
    imageLoader.displayImage(
            wallpaper.getHDUrl()//the drawable
            , wallpaperViewHolder.imageViewWallpaper//the image view
            , imageLoaderOptions);//the display options
}

private WallpaperViewHolder getWallpaperViewHolder(View itemView) {
    WallpaperViewHolder wallpaperViewHolder = new WallpaperViewHolder();
    wallpaperViewHolder.wallpaperDateForUse
            = (TextView) itemView.findViewById(R.id.wallpaper_date_for_use);
    wallpaperViewHolder.imageViewWallpaper
            = (ImageView) itemView.findViewById(R.id.wallpaper_image);
    wallpaperViewHolder.wallpaperAuthor
            = (TextView) itemView.findViewById(R.id.wallpaper_author);
    wallpaperViewHolder.wallpaperSite
            = (TextView) itemView.findViewById(R.id.wallpaper_site);
    wallpaperViewHolder.setWallpaperBtn
            = (Button) itemView.findViewById(R.id.wallpaper_set);
    return wallpaperViewHolder;
}

private static class WallpaperViewHolder {
    ImageView imageViewWallpaper;
    TextView wallpaperDateForUse;
    TextView wallpaperAuthor;
    TextView wallpaperSite;
    Button setWallpaperBtn;
}
}

1 个答案:

答案 0 :(得分:2)

In else cases you have to change visibility for textviews other wise they don't visible.

if (authorName == null) {
    wallpaperViewHolder.wallpaperAuthor.setVisibility(View.GONE);
} else {
     wallpaperViewHolder.wallpaperSite.setVisibility(View.VISIBLE);
     wallpaperViewHolder.wallpaperAuthor.setText(getContext().getString(R.string.wa llpaper_author_name) + " " + authorName);
}

Hope this will helps you.