gpt4 book ai didi

Android:在 gridview 中重用嵌入式 View [已发布的解决方案]

转载 作者:太空宇宙 更新时间:2023-11-03 10:28:59 25 4
gpt4 key购买 nike

我正在尝试重用其中包含图像和 TextView 的框架布局,但我认为我这样做不对。代码有效,显示正确,但性能很差,我相信这是因为每次适配器返回到项目位置时我都创建了一个新的 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;
}

最佳答案

  1. 不是为每个元素动态创建 View ,而是创建一个 XML 布局文件,比如 row.xml。
  2. 如果您检测到 convertView == null 使用 inflater 膨胀新行
  3. 使用 View#findViewById 找到您的 TextView 和 ImageView
  4. 创建一个 Holder 对象,这将有助于保存对新找到的 TextView 和 ImageView 的引用
  5. 将 holder 保存为标签,这样 convertView.setTag(holder)
  6. 对于现有的 convertView,通过执行 holder = convertView.getTag() 找到 Holder 对象
  7. 为这两个保存的对象设置文本和图像,例如holder.txt.setText("Foo")
  8. Android 适配器将完成剩下的工作,只要重用膨胀行的实例

可以说,即使对于您的代码,您也可以进行一次 View 初始化和布局,并使用 Holder 模式来避免重新初始化元素,但我认为 XML 会给您带来更好的体验

关于Android:在 gridview 中重用嵌入式 View [已发布的解决方案],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8706322/

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