我正在尝试重用一个在其中包含图像和textview的framelayout,但我不认为我这样做是正确的.代码工作,显示正确,但性能很差,我相信这是因为每次适配器返回项目位置时我都会创建一个新的
ImageView和TextView.
有人能告诉我如何在不创建新对象的情况下重用嵌入式ImageView(称为i)和TextView(称为t)吗?我是Java新手,这是我尝试构建Android应用程序.
public View getView(int position, View convertView, ViewGroup parent) {
FrameLayout F;
FrameLayout ImageBorder;
FrameLayout TextBG;
ImageView i;
TextView t;
if(convertView == null) {
F = new FrameLayout(mContext);
} else {
F = (FrameLayout) convertView;
}
ImageBorder = new FrameLayout(F.getContext());
FrameLayout.LayoutParams params1 = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT,300,Gravity.BOTTOM);
ImageBorder.setLayoutParams(params1);
i = new ImageView(F.getContext());
TextBG = new FrameLayout(F.getContext());
t = new TextView(F.getContext());
F.setBackgroundColor(Color.BLACK);
ImageBorder.setPadding(2, 2, 2, 2);
ImageBorder.setBackgroundColor(Color.BLACK);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT,40,Gravity.BOTTOM);
TextBG.setLayoutParams(params);
TextBG.setBackgroundColor(Color.BLACK);
TextBG.setAlpha(.6f);
t.setLayoutParams(params);
t.setGravity(Gravity.CENTER_VERTICAL);
String pathToPhoto = FileList.get(position).toString();
String fileDescription = pathToPhoto.replaceAll("/mnt/external1/PaliPhotography/","");
fileDescription = fileDescription.replaceAll(".jpg","");
fileDescription = fileDescription.toUpperCase();
Bitmap bm = Cache.getCacheFile("thumb",pathToPhoto);
if (bm == null) {
ImageDownloader downloader = new ImageDownloader(i);
downloader.execute("thumb", pathToPhoto, "400", "400");
} else {
i.setImageBitmap(bm);
i.setScaleType(ImageView.ScaleType.CENTER_CROP);
t.setTextAppearance(getApplicationContext(), android.R.style.TextAppearance_Large);
t.setText(" " + fileDescription);
}
ImageBorder.addView(i);
ImageBorder.addView(TextBG);
ImageBorder.addView(t);
F.addView(ImageBorder);
return F;
}
}
先感谢您!
[编辑]
—————————解决方案———————- ——————————-
以下是我根据以下反馈实施的解决方案!谢谢!
public View getView(int position, View convertView, ViewGroup parent) {
View ReturnThisView;
ViewHolder holder;
LayoutInflater inflater;
holder = new ViewHolder();
if(convertView == null) {
inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ReturnThisView = inflater.inflate(R.layout.imagecell, null);
ReturnThisView.setTag(holder);
} else {
ReturnThisView = convertView;
}
holder.TextDescription = (TextView) ReturnThisView.findViewById(R.id.PhotoDesc);
holder.ImageThumbnail = (ImageView) ReturnThisView.findViewById(R.id.Thumbnail);
String pathToPhoto = FileList.get(position).toString();
String fileDescription = pathToPhoto.replaceAll("/mnt/external1/PaliPhotography/","");
fileDescription = fileDescription.replaceAll(".jpg","");
fileDescription = fileDescription.toUpperCase();
Bitmap bm = Cache.getCacheFile("thumb",pathToPhoto);
if (bm == null) {
ImageDownloader downloader = new ImageDownloader(holder.ImageThumbnail);
downloader.execute("thumb", pathToPhoto, "400", "400");
} else {
holder.ImageThumbnail.setImageBitmap(bm);
holder.ImageThumbnail.setScaleType(ImageView.ScaleType.CENTER_CROP);
holder.TextDescription.setTextAppearance(getApplicationContext(), android.R.style.TextAppearance_Large);
holder.TextDescription.setText(" " + fileDescription);
}
return ReturnThisView;
}
}
static class ViewHolder {
TextView TextDescription;
ImageView ImageThumbnail;
}
最佳答案 >而不是为每个元素动态创建View,而是创建一个XML布局文件,比如row.xml.
>如果通过使用inflater检测到convertView == null为新行充气
>使用View#findViewById找到TextView和ImageView
>创建一个Holder对象,它有助于保存对新发现的TextView和ImageView的引用
>将持有者保存为标记,以便convertView.setTag(holder)
>对于现有的convertView,通过执行holder = convertView.getTag()查找Holder对象
>为这两个保存的对象设置文本和图像,例如holder.txt.setText( “富”)
>重新使用膨胀行的实例,Android适配器将完成其余工作
可以说甚至对于你的代码你可以查看初始化和布局一次并使用Holder模式来避免重新初始化元素但我认为XML会给你更好的体验