我正在实现使用按钮打开的图像网格视图。我面临的问题是关于打开网格视图的滞后/时间延迟。这是一些代码片段
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);
}
对于图像,我正在使用这样的适配器
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,
};
}
滚动浏览gridview时,我也面临滞后。如何提高性能并使 gridview 加载更快。我读过我们可以为此使用异步任务,但我对在这种情况下如何使用它不是很熟悉。还有其他解决方案吗?谢谢