有人能告诉我如何在不创建新对象的情况下重用嵌入式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); 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,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.toupperCase(); Bitmap bm = Cache.getCachefile("thumb",pathtophoto); if (bm == null) { ImageDownloader downloader = new ImageDownloader(holder.Imagethumbnail); downloader.execute("thumb","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会给你更好的体验
总结以上是内存溢出为你收集整理的Android:在gridview中重用嵌入式视图[解决方案已发布]全部内容,希望文章能够帮你解决Android:在gridview中重用嵌入式视图[解决方案已发布]所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)