Link to home
Start Free TrialLog in
Avatar of jbajaj
jbajaj

asked on

Scroll of Grid View in android

Hello friend,

I am working on grid view for displaying an Media store images, In grid view images will be displayed but the scroll is not as smooth as device gallery grid . So please explain me how I  increase the performance of grid in my code, here I attach my code.

Thank You.
import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class GridViewDemo extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        GridView gridView=(GridView)findViewById(R.id.gridView);
        String selection = MediaStore.Images.Thumbnails.KIND + "="  + // Select only mini's
        MediaStore.Images.Thumbnails.MINI_KIND;

        Cursor cursor=managedQuery(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, null, selection, null, null);
       
        cursor.moveToFirst();
        gridAdapter gAdapter=new gridAdapter(getApplicationContext(), cursor);
        gridView.setAdapter(gAdapter);
    }
    
    class gridAdapter extends BaseAdapter{
    	Context context;
    	Cursor cursor;
    	LayoutInflater inflater;
    	public gridAdapter(Context context,Cursor cursor){
    		this.context=context;
    		this.cursor=cursor;
    		inflater=LayoutInflater.from(context);
    	}
    	
		@Override
		public int getCount() {
			return cursor.getCount();
		}

		@Override
		public Object getItem(int arg0) {
			return cursor.getString(cursor.getColumnIndex(MediaStore.Images.Thumbnails.DATA));
		}

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

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ImageView img;
			cursor.moveToPosition(position);
			String path=cursor.getString(cursor.getColumnIndex(MediaStore.Images.Thumbnails.DATA));
			if(convertView==null){
				img=new ImageView(context);
				img.setLayoutParams(new GridView.LayoutParams(90, 90));

				convertView=img;
				String pathStr=cursor.getString(cursor.getColumnIndex(MediaStore.Images.Thumbnails.DATA));
				((ImageView)convertView).setImageURI(Uri.parse(pathStr));
			}else{
				String pathStr=cursor.getString(cursor.getColumnIndex(MediaStore.Images.Thumbnails.DATA));
				((ImageView)convertView).setImageURI(Uri.parse(pathStr));
			}
			return convertView;
		}
    	
    }
}


       main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<GridView  
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/gridView"
    android:numColumns="auto_fit"
	android:columnWidth="90dip"
	android:horizontalSpacing="0dip"></GridView>
</LinearLayout>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Dejan Pažin
Dejan Pažin
Flag of Austria image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jbajaj
jbajaj

ASKER

Thank you dejanpazin.

I immediately check time to execute  each line in code by adding some output.
But is there any other faster way to see all the images from gallery. Because I also test it on device but the scroll is not as smooth as default gallery in device.
If any other way to do same thing then please help me.

Thank You.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jbajaj

ASKER

Thank you dejanpazin,

I will try to find  bottleneck of my code and then improve it, and also try to caching images if necessary.

Thank you for help.

I guess this question is requested to be closed by a mistake - the author stated the reason as 'excellent'.
All,
 
Following an 'Objection' by dejanpazin (at https://www.experts-exchange.com/questions/26868664/Automated-Request-for-Review-Objection-to-Accept-Q-26860708.html) to the intended closure of this question, it has been reviewed by at least one Moderator and is being closed as recommended by the Expert.
 
At this point I am going to re-start the auto-close procedure.
 
Thank you,
 
_alias99
Community Support Moderator