滚动时StaggeredGridView卡在中间

时间:2015-06-19 11:48:42

标签: android gridview scroll staggered-gridview

我在我的应用程序中显示设备存储中的所有图像。我遇到的问题是,当我滚动它不平滑时,它会卡在中间。我正在使用etsy/AndroidStaggeredGrid并使用下面的代码。我尝试过把任务放在异步后台任务中,但我仍然遇到这个问题。为什么它会介于两者之间?我该如何解决这个问题?

GalleryFragment

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    final View rootView = inflater.inflate(R.layout.fragment_gallery, container, false);
    galleries=new ArrayList<Gallery>();
            ((ActionBarActivity) getActivity()).getSupportActionBar().show();
    gridview = (StaggeredGridView)rootView.findViewById(R.id.gridcamera);
    progressBar=(ProgressBar)rootView.findViewById(R.id.progressBar);
        gallerylist=new CameraGalleryAdapter(getActivity().getApplicationContext(), R.layout.gallery_grid,galleries);
    gridview.setAdapter(gallerylist);

    AsyncTaskRunner runner = new AsyncTaskRunner();

    runner.execute();

    return rootView;
}

private class AsyncTaskRunner extends AsyncTask<String, String, Void> {

    private String resp;

    @Override
    protected Void doInBackground(String... params)
    {

        int count,count2,count3,count4;

        ((CameraActivity)getActivity()).setOnBackPressedListener(new BaseBackPressedListener(getActivity(),true));

        final String[] columns = { MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID,MediaStore.Video.Media.DATA,
        };
        final String orderBy = MediaStore.Images.Media._ID;
        Cursor cursor = getActivity().getContentResolver().query(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null,
                null, orderBy);

        count = cursor.getCount();
        Cursor cursor2 = getActivity().getContentResolver().query(
                MediaStore.Images.Media.INTERNAL_CONTENT_URI, columns, null,
                null, orderBy);
        count2 = cursor2.getCount();

        Cursor cursor3 = getActivity().getContentResolver().query(
                MediaStore.Video.Media.EXTERNAL_CONTENT_URI, columns, null,
                null, orderBy);
        count3 = cursor3.getCount();
        Cursor cursor4 = getActivity().getContentResolver().query(
                MediaStore.Video.Media.INTERNAL_CONTENT_URI, columns, null,
                null, orderBy);
        count4 = cursor4.getCount();


        for (int i = 0; i < count; i++)
        {
            cursor.moveToPosition(i);
            int dataColumnIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATA);

            Gallery ga=new Gallery();
            ga.setImageUrl(cursor.getString(dataColumnIndex));
            ga.setIsImage(true);
            galleries.add(ga);
        }
        for (int i = 0; i < count2; i++)
        {
            cursor2.moveToPosition(i);
            int dataColumnIndex = cursor2.getColumnIndex(MediaStore.Images.Media.DATA);
            Gallery ga=new Gallery();;
            ga.setImageUrl(cursor2.getString(dataColumnIndex));
            ga.setIsImage(true);
            galleries.add(ga);

        }

        for (int i = 0; i < count3; i++)
        {


            cursor3.moveToPosition(i);

            int dataColumnIndex = cursor3.getColumnIndex(MediaStore.Video.Media.DATA);

            Bitmap thumb = ThumbnailUtils.createVideoThumbnail(cursor3.getString(dataColumnIndex),
                    MediaStore.Images.Thumbnails.MINI_KIND);

            Gallery ga=new Gallery();

            ga.setVideoThumbnail(thumb);

            ga.setVideoUrl(cursor3.getString(dataColumnIndex));

            ga.setIsImage(false);
            galleries.add(ga);


        }
        for (int i = 0; i < count4; i++)
        {


            cursor4.moveToPosition(i);

            int dataColumnIndex = cursor4.getColumnIndex(MediaStore.Video.Media.DATA);

            Bitmap thumb = ThumbnailUtils.createVideoThumbnail(cursor4.getString(dataColumnIndex),MediaStore.Images.Thumbnails.MINI_KIND);

            Gallery ga = new Gallery();

            ga.setVideoThumbnail(thumb);

            ga.setVideoUrl(cursor4.getString(dataColumnIndex));

            ga.setIsImage(false);
            galleries.add(ga);


        }
        return null;
    }



    @Override
    protected void onPreExecute() {

    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
        gallerylist.notifyDataSetChanged();
        progressBar.setVisibility(View.GONE);
    }

    @Override
    protected void onProgressUpdate(String... text) {

    }
}

CameraGalleryAdapter

public class CameraGalleryAdapter extends BaseAdapter {
    ArrayList<Gallery> itemList;
    LayoutInflater vi;
    int Resource;
    ViewHolder holder;
    Bitmap bm;
    public CameraGalleryAdapter(Context context, int resource, ArrayList<Gallery> objects) {

        vi = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        Resource = resource;
        itemList = objects;

    }


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

    @Override
    public Object getItem(int i) {
        return null;
    }

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

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

        View v = convertView;
        if (v == null) {
            holder = new ViewHolder();
            v = vi.inflate(Resource, null);
            holder.imageview = (ImageView) v.findViewById(R.id.ivImageg);
            holder.playicon= (ImageView) v.findViewById(R.id.galleryplay);

            v.setTag(holder);
        } else {
            holder = (ViewHolder) v.getTag();
        }

 if(itemList.get(position).getIsImage())
 {

            holder.playicon.setVisibility(View.INVISIBLE);

     AsyncTaskRunner runner = new AsyncTaskRunner();

     runner.execute(itemList.get(position).getImageUrl());
     bm= null ;
     try {
         bm = runner.get();
     } catch (InterruptedException e) {
         e.printStackTrace();
     } catch (ExecutionException e) {
         e.printStackTrace();
     }
     holder.imageview.setImageBitmap(bm);
 }
 else
 {
            holder.playicon.setVisibility(View.VISIBLE);
            holder.imageview.setImageBitmap(itemList.get(position).getVideoThumbnail());
 }
        return v;

    }

    static class ViewHolder {
        public ImageView imageview;
        public ImageView playicon;

    }
    private class AsyncTaskRunner extends AsyncTask<String, String,Bitmap>
    {

        private String resp;

        @Override
        protected Bitmap doInBackground(String... params)
        {
            resp=params[0];
            Bitmap bm = decodeSampledBitmapFromUri(resp, 220, 220);
            return bm;
        }



        @Override
        protected void onPreExecute() {

        }

        @Override
        protected void onPostExecute(Bitmap aVoid) {
            super.onPostExecute(aVoid);


        }

        @Override
        protected void onProgressUpdate(String... text) {

        }
    }
    public Bitmap decodeSampledBitmapFromUri(String path, int reqWidth, int reqHeight) {

        Bitmap bm = null;

        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(path, options);


        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);


        options.inJustDecodeBounds = false;
        bm = BitmapFactory.decodeFile(path, options);

        return bm;
    }

    public int calculateInSampleSize(

            BitmapFactory.Options options, int reqWidth, int reqHeight)
    {

        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {
            if (width > height) {
                inSampleSize = Math.round((float)height / (float)reqHeight);
            } else {
                inSampleSize = Math.round((float)width / (float)reqWidth);
            }
        }

        return inSampleSize;
    }

}

1 个答案:

答案 0 :(得分:1)

它是一个常见的问题bcoz的大图像大小在每次滚动时都在视图中加载....来过这个问题你可以尝试延迟加载imgae技术.. http://symfony.com/doc/current/reference/forms/types/date.html#html5