I am implementing a grid view of images which is opened using a button. The issue I am facing is regarding lagging/time delay for opening the gridview. Here is some of code snippets
import android.os.Bundle;
import android.app.ActionBar;
import android.app.Activity;
import android.app.FragmentManager;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
public class Gallery extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gallery);
ActionBar ab=getActionBar();
Resources r=getResources();
Drawable d=r.getDrawable(R.color.quotescolor);
ab.setBackgroundDrawable(d);
GridView gv=(GridView)findViewById(R.id.gridview);
gv.setAdapter(new ImageAdapter2(this));
final FragmentManager fm=getFragmentManager();
gv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
// TODO Auto-generated method stub
if(position==0)
{
ImageDial1 id1=new ImageDial1();
id1.show(fm,"image_title");
//Intent i=new Intent(Gallery.this,ImageFrag1.class);
//startActivity(i);
}
and for the images i am using an adapter like this
package com.example.sachinapp;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
public class ImageAdapter2 extends BaseAdapter {
private Context ctx;
public ImageAdapter2(Context c)
{
ctx=c;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return pics.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView iv;
if (convertView == null) { // if it's not recycled, initialize some attributes
iv = new ImageView(ctx);
iv.setLayoutParams(new GridView.LayoutParams(150,150));
iv.setScaleType(ImageView.ScaleType.CENTER_CROP);
iv.setPadding(8, 8, 8, 8);
} else {
iv = (ImageView) convertView;
}
iv.setImageResource(pics[position]);
return iv;
}
private Integer[] pics={
R.drawable.s1,R.drawable.s2,
R.drawable.s3,R.drawable.s4,
R.drawable.s6,R.drawable.s7,
R.drawable.s8,R.drawable.s9,
R.drawable.s10,R.drawable.s11,
R.drawable.s2,R.drawable.s13,
R.drawable.s4,R.drawable.s15,
R.drawable.s6,R.drawable.s7,
R.drawable.s18,R.drawable.s19,
};
}
I am also facing lagging when scrolling through the gridview. How can i improve the performance and make the gridview load faster. I have read that we can use async task for that, but I am not very familiar about how to use it in this case . Is there any other solution as well? Thanks