如何使用AsyncTask异步加载GridView中的缩略图或任何其他数据

时间:2014-03-27 20:13:59

标签: android gridview android-asynctask

我在活动中有一个内部类名ThumbnailCreater(此活动接收来自调用活动的外部存储上的视频路径)类,它用作网格视图构建器。

但问题是在

时加载活动布局需要很长时间

1 启动它,

2 返回它(缩略图可点击并调用新活动来播放点击的缩略图视频,按下视频播放活动停止但缩略图活动不会很快重新出现)

时间方向改变时

3

class ThumbnailCreater extends BaseAdapter {

    ArrayList<String> pathList;
    Context context;

    public ThumbnailCreater(ArrayList<String> p, Context context) {

        this.context = context;

        pathList = new ArrayList<String>(p);

    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return pathList.size();
    }

    @Override
    public Object getItem(int i) {
        // TODO Auto-generated method stub
        return pathList.get(i);
    }

    @Override
    public long getItemId(int i) {
        // TODO Auto-generated method stub
        return i;
    }


    class ViewHolder {
        ImageView myVid;
        CheckBox picker;

        public ViewHolder(View v) {
            myVid = (ImageView) v.findViewById(R.id.vid);
            picker = (CheckBox) v.findViewById(R.id.checkIt);
        }
    }


    @Override
    public View getView(int i, View v, ViewGroup vGroup) {
        // TODO Auto-generated method stub

        View row = v;
        ViewHolder holder = null;


        if (row == null) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate(R.layout.video_thumbnail, vGroup, false);
            holder = new ViewHolder(row);
            row.setTag(holder);
        } else {
            holder = (ViewHolder) row.getTag();
        }



        Bitmap thumb = ThumbnailUtils.createVideoThumbnail(pathList.get(i),
                MediaStore.Video.Thumbnails.MICRO_KIND);


        holder.myVid.setImageBitmap(thumb);

        holder.picker.setText(pathList.get(i));

        if(vidSelected.contains(holder.picker.getText().toString()))
        {
            holder.picker.setChecked(true);
        }
        else
        {
            holder.picker.setChecked(false);
        }

        holder.myVid.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                ViewGroup thumbNail = (ViewGroup) v.getParent();
                CheckBox checker = (CheckBox) thumbNail.getChildAt(1);
                String playIt = checker.getText().toString();

                Intent play = new Intent(context, VideoPlay.class);
                play.putExtra("playIt", playIt);
                context.startActivity(play);
            }
        });



        final CheckBox picker2 = holder.picker;
        picker2.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                if (picker2.isChecked()) {
                    vidSelected.add(picker2.getText().toString());

                } else {
                    for (String s : vidSelected) {
                        if (s.equals(picker2.getText().toString())) {
                            vidSelected
                                    .remove(picker2.getText().toString());

                            break;
                        }
                    }
                }
            }
        });



        return row;


}


}

我尝试将grid.setAdapter(new ThumbnailCreater(pathList, this));作为AsyncTask执行但未能实现所需的重新组合。

是否有任何方法可以加载缩略图而不会延迟,例如在网页中加载图像(随机或按顺序),因为它们是从服务器接收的,而不会在网页滚动或导航中产生障碍。

0 个答案:

没有答案