ListView项目使用baseadapter重新排序和重复

时间:2014-02-27 05:31:24

标签: java android listview gridview android-listview

我正在尝试实现一个显示目录列表的列表视图。在每个目录下是一个带有相关适配器的gridview(如下所示),显示了一个图像缩略图列表(见下图)。我有它工作得很好,除非列表项目在屏幕外,然后带回屏幕,图像被重新加载。我使用asynctask下载缩略图并替换每个imageview的占位符图像,因此每次项目在屏幕外时都不能接受,所有缩略图都会再次下载。有没有人有这种类型的实现(listview适配器中的gridview适配器)存储imageview(或图像)的示例?这样做的正确方法是什么? 在此先感谢您的帮助。

Screenshot of Activity

图库适配器

public class GalleryAdapter extends BaseAdapter {
private Context mContext;
ArrayList<GalleryItem> GalleryList;
//MediaAdapter adapter;


public GalleryAdapter(Context c,ArrayList<GalleryItem> l) {
    mContext = c;
    GalleryList = l;
}

public int getCount() {
    return GalleryList.size();
}

public Object getItem(int position) {
    return GalleryList.get(position);
}

public long getItemId(int position) {
    return 0;
}

public View getView(int position, View convertView, ViewGroup parent) {
    GalleryViewHolder viewHolder = null;


    if(convertView==null){
        // inflate the layout
        LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
        convertView = inflater.inflate(R.layout.gallery_item, parent, false);
        viewHolder = new GalleryViewHolder();
        viewHolder.title =  (TextView) convertView.findViewById(R.id.title);
        viewHolder.folder_settings = (ImageView) convertView.findViewById(R.id.folder_settings);
        viewHolder.mediaGrid = (GridView) convertView.findViewById(R.id.imagegrid);
        viewHolder.gridHolder = (LinearLayout) convertView.findViewById(R.id.gridholder);
        convertView.setTag(viewHolder);
    }
    else{
        viewHolder = (GalleryViewHolder) convertView.getTag();
    }



    viewHolder.title.setText(GalleryList.get(position).getTitle());
//Formatting the gridView to fit the screen dim.
    ImageTools mWidth = new ImageTools(mContext);
    viewHolder.mediaGrid.setColumnWidth(mWidth.imageSize());
    int rows = (int) Math.ceil((GalleryList.get(position).getMedia().size() / mWidth.columnNumber)+1);
    LinearLayout.LayoutParams labelLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, mWidth.imageSize()*rows);
    viewHolder.gridHolder.setLayoutParams(labelLayoutParams);
    viewHolder.mediaGrid.setLayoutParams(labelLayoutParams);
    viewHolder.mediaGrid.setMinimumHeight(mWidth.imageSize()*rows);
//Set Adapter for image views
    viewHolder.mediaGrid.setAdapter(new MediaAdapter(convertView.getContext(),GalleryList.get(position).getMedia()));

    viewHolder.folder_settings.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Dialogs.createListDialog(mContext,"Folder Actions", R.array.gallery_action_array).show();
        }
    });

    viewHolder.mediaGrid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
        }
    });

    return convertView;
}

private class GalleryViewHolder {
    private TextView title;
    private ArrayList<ImageView> imageList;
    private GridView mediaGrid;
    private ImageView folder_settings;
    private LinearLayout gridHolder;
    private int position;
}
}

媒体适配器

public class MediaAdapter extends BaseAdapter {
private Context mContext;
ArrayList<MediaItem> mediaitems;

public MediaAdapter(Context c,ArrayList<MediaItem> l) {
    mContext = c;
    mediaitems = l;
}

public int getCount() {
    return mediaitems.size();
}

public Object getItem(int position) {
    return mediaitems.get(position);
}

public long getItemId(int position) {
    return 0;
}

public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView;
    if (convertView == null) {  // if it's not recycled, initialize some attributes
        imageView = new ImageView(mContext);
        imageView.setImageResource(R.drawable.loading);

        imageView.setTag(R.integer.path,mediaitems.get(position).getPath().toString());
        imageView.setTag(R.integer.fullsize,"false");
        imageView.setTag(R.integer.parentpath,mediaitems.get(position).getParentPath().toString());
        imageView.setTag(R.integer.index , String.valueOf(position));
        try {
            new thumbDownload(mContext).execute(imageView);
        } catch (DbxException e) {
            e.printStackTrace();
        }
        ImageTools mWidth = new ImageTools(mContext);
        imageView.setLayoutParams(new GridView.LayoutParams(mWidth.imageSize(), mWidth.imageSize()));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
    } else {
        imageView = (ImageView) convertView;
    }

    //imageView.setImageBitmap(mediaitems.get(position).getBitmap());
    return imageView;
}

}

3 个答案:

答案 0 :(得分:0)

试试这个..可能是它的愚蠢方式,但它对我有用。只需在方法中添加一行代码,如图库适用:

public View getView(int position, View convertView, ViewGroup parent) {
GalleryViewHolder viewHolder = null;  
// Add this line.
convertView = null;

if(convertView==null){
    // inflate the layout
    LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
    convertView = inflater.inflate(R.layout.gallery_item, parent, false);
    viewHolder = new GalleryViewHolder();
    viewHolder.title =  (TextView) convertView.findViewById(R.id.title);
    viewHolder.folder_settings = (ImageView) convertView.findViewById(R.id.folder_settings);
    viewHolder.mediaGrid = (GridView) convertView.findViewById(R.id.imagegrid);
    viewHolder.gridHolder = (LinearLayout) convertView.findViewById(R.id.gridholder);
    convertView.setTag(viewHolder);
}
else{
    viewHolder = (GalleryViewHolder) convertView.getTag();
}

// rest of your code

}

答案 1 :(得分:0)

您可以使用StickyGridHeaders来实现您的用户界面和Android-Universal-Image-Loader,以实现灵活的异步图像加载。

  

StickyGridHeaders是一个Android库,它提供了一个GridView,它在带有标题的部分中显示项目。默认情况下,部分标题会像Android 4.x中的People应用程序一样粘在顶部,但可以关闭它。

     

Android-Universal-Image-Loader旨在为异步图像加载,缓存和显示提供可重复使用的工具。

答案 2 :(得分:0)

我最终使用HashMap<String,Bitmap>存储下载后的图像。我在mediaAdapter中创建了hashMap静态,所以我可以在下载时从我的asynctask中添加位图。然后在我的媒体适配器getView()中,我添加了一个if语句来检查图像是否已经下载。如果有,我使用了setImageBitmap(myHash.get(key))