gpt4 book ai didi

java - gridview 中加载的图像在滚动时改变其位置

转载 作者:行者123 更新时间:2023-12-01 04:14:09 25 4
gpt4 key购买 nike

嗨,当滚动 ImageView 更改其位置时,我遇到了此类问题。我在此网站上看到了有关此主题的其他答案,但没有一个对我有帮助。

这是我的类(class):

public class MainActivity extends Activity {
private GridView grid;
private GridAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

grid = (GridView) findViewById(R.id.gridView1);

Vector<HashMap<String, String>> vData = new Vector<HashMap<String,String>>();

for (int i=0; i<10; i++) {
String name = "Name "+ (i+1);
String desc = "Description "+ (i+1);
HashMap<String, String> hData = new HashMap<String, String>();
hData.put("name", name);
hData.put("desc", desc);

vData.addElement(hData);
}

adapter = new GridAdapter(vData);
grid.setAdapter(adapter);
grid.setNumColumns(2);

File cacheDir = StorageUtils.getCacheDirectory(this);
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this)
.memoryCacheExtraOptions(480, 800) // default = device screen dimensions
.discCacheExtraOptions(480, 800, CompressFormat.JPEG, 75, null)
.threadPoolSize(10) // default
.threadPriority(Thread.NORM_PRIORITY - 1) // default
.tasksProcessingOrder(QueueProcessingType.FIFO) // default
.denyCacheImageMultipleSizesInMemory()
.memoryCache(new LruMemoryCache(2 * 1024 * 1024))
.memoryCacheSize(10 * 1024 * 1024)
.memoryCacheSizePercentage(30) // default
.discCache(new UnlimitedDiscCache(cacheDir)) // default
.discCacheSize(80 * 1024 * 1024)
.discCacheFileCount(100)
.discCacheFileNameGenerator(new HashCodeFileNameGenerator()) // default
.imageDownloader(new BaseImageDownloader(this)) // default
.imageDecoder(new BaseImageDecoder()) // default
.defaultDisplayImageOptions(DisplayImageOptions.createSimple()) // default
.build();

ImageLoader.getInstance().init(config);
}

//sub class
class GridAdapter extends BaseAdapter {
Vector<HashMap<String,String>> vData;
GridAdapter(Vector<HashMap<String,String>> vData) {
this.vData = vData;
}

@Override
public int getCount() {
// TODO Auto-generated method stub
return vData.size();
}

@Override
public Object getItem(int index) {
// TODO Auto-generated method stub
return vData.elementAt(index);
}

@Override
public long getItemId(int index) {
// TODO Auto-generated method stub
return index;
}

@Override
public View getView(int index, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
HashMap<String, String> hData = vData.elementAt(index);

View v;

if (convertView == null) {
LayoutInflater li = getLayoutInflater();
v = li.inflate(R.layout.grid_element, null);
// v = getLayoutInflater().inflate(R.layout.grid_element, parent, false);
} else {
v = convertView;
}

//TextView txtName = (TextView) v.findViewById(R.id.gridText);
//txtName.setText(hData.get("name"));
ImageView img = (ImageView) v.findViewById(R.id.gridImg);
ImageLoader.getInstance().displayImage("http://www.bourax.com/android/wall"+index+".jpg", img);

return v;
}
}
}

我尝试使用支架但出现错误。有什么帮助吗?

最佳答案

@Override
public View getView(int index, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
HashMap<String, String> hData = vData.elementAt(index);

View v;
ImageView img;
if(convertView == null){

LayoutInflater li = getLayoutInflater();
v = li.inflate(R.layout.grid_element, null);
// v = getLayoutInflater().inflate(R.layout.grid_element, parent, false);
img = (ImageView) v.findViewById(R.id.gridImg);
}else{
v = convertView;
}
//TextView txtName = (TextView) v.findViewById(R.id.gridText);
//txtName.setText(hData.get("name"));

ImageLoader.getInstance().displayImage("http://www.bourax.com/android/wall"+index+".jpg", img);


return v;
}

相应地更改代码...实际上,当convertView为null时,它需要当前初始化的所有 View 才能在 Activity 上呈现...这就是为什么ImageView需要处于if条件...

关于java - gridview 中加载的图像在滚动时改变其位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19612222/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com