0

I have created a gallery. I am populating a gridview from a simple cursor adapter (custom implementation).

First issue is that gridview stucks in the middle while scrolling. I think its because images are binded to a view continuously on user interaction. How can I achieve a smooth scroll?

Secondly I do not get all the images from the gallery through the Cursor. What do I need to do in order to get all the images. I also observed that this code does not return images on different devices, such as Samsung Galaxy s2. Works fine on Samsung Note 2 and Nexus 4 though. URI is same though: content://media/external/images/thumbnails

Code for an adapter:

/**  
 * @author Syed Ahmed Hussain
 */
public class CustomGalleryAdapter extends SimpleCursorAdapter {

    private ContentResolver mContentResolver;
    private LayoutInflater mLayoutInflater;
    private int mImageIdColumnIndex;
    private ImageView mImageView;
    private Context mContext;
    private int mLayout;

    public CustomGalleryAdapter(Context pContext, int pLayout, Cursor pCursor, String[] pFrom, int[] pTo, int pFlags) {
        super(pContext, pLayout, pCursor, pFrom, pTo, pFlags);
        mLayout             = pLayout;
        mContext            = pContext;
        mLayoutInflater     = LayoutInflater.from(mContext);
        mContentResolver    = mContext.getContentResolver();
        mImageIdColumnIndex = pCursor.getColumnIndex(MediaStore.Images.Thumbnails.IMAGE_ID);
    }

    @Override
    public View newView(Context pContext, Cursor pCursor, ViewGroup pParent) {
        return mLayoutInflater.inflate(mLayout, null);
    }

    @Override
    public void bindView(View pView, Context pContext, Cursor pCursor) {
        long id = pCursor.getLong(mImageIdColumnIndex);
        mImageView = (ImageView) pView;
        mImageView.setScaleType(ScaleType.CENTER_CROP);
        mImageView.setImageBitmap(MediaStore.Images.Thumbnails.getThumbnail(mContentResolver, id, MediaStore.Images.Thumbnails.MINI_KIND, new Options()));
    }

}

Code for an activity:

public class GalleryActivity extends FragmentActivity implements LoaderManager.LoaderCallbacks<Cursor> {

    private String[] projection = { MediaStore.Images.Thumbnails.IMAGE_ID };
    private CustomGalleryAdapter mCustomGalleryAdapter;
    private String[] selectionArgs = null;
    private String selection = "";
    private GridView mGridView;

    @Override
    protected void onCreate(Bundle pSavedInstanceState) {
        super.onCreate(pSavedInstanceState);
        setContentView(R.layout.view_gallery);
        mGridView = (GridView) findViewById(R.id.gridView);
        getSupportLoaderManager().initLoader(0, null, this);
    }

    @Override
    public Loader<Cursor> onCreateLoader(int pId, Bundle pArgs) {

        return new android.support.v4.content.CursorLoader(getApplicationContext(), MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, null, null, null, null);
    }

    @Override
    public void onLoadFinished(Loader<Cursor> pLoader, Cursor pData) {
        mCustomGalleryAdapter = new CustomGalleryAdapter(getApplicationContext(), R.layout.gallery_item, pData, projection, new int[] { R.id.galleryImageView }, 0);
        mGridView.setAdapter(mCustomGalleryAdapter);
    }

    @Override
    public void onLoaderReset(Loader<Cursor> pLoader) {

    }

}

View_Galler.xml:

<?xml version="1.0" encoding="utf-8"?>
<GridView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:gravity="center"
    android:numColumns="auto_fit"
    android:columnWidth="100dp"
    android:paddingLeft="2dp"
    android:paddingRight="2dp"
    android:layoutMode="opticalBounds"
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp"
    android:listSelector="#00000000"
    android:id="@+id/gridView" />

gallery_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<ImageView
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/galleryImageView"
  android:layout_width="match_parent"
  android:layout_height="match_parent" />

Can anyone please guide me on what am I doing wrong? and how can I solve these problems?

4

2 回答 2

3

以下是您可以尝试的改进:

  1. 使用 ArrayAdapter。
  2. 使用 ViewHolder 模式在网格单元中重用视图并在 getView 中重用 convertview 是至关重要的 (**)
  3. 避免在适配器 getView 中实例化不必要的对象。
  4. 如果您使用 LazyLoading 技术和异步任务从 Internet 下载,则缓存 imgaes。
  5. 当您使用本地资源的图像时,在这两种情况下都使用压缩图像。
  6. 安卓:fastScrollEnabled="真"。
于 2013-12-07T23:15:17.713 回答
1

I would suggest you to use Universal Image Loader

https://github.com/nostra13/Android-Universal-Image-Loader

Also use: android:largeHeap="true" in your Manifest.

Declare this attribute in your GridView (XML) android:fastScrollEnabled="true"

于 2015-02-06T18:56:00.253 回答