
我的问题是我的自定义ArrayAdapter正确填充了我的列表(下面的代码).据我所知,我的适配器只是在实例化时填充textvIEwResourceID,因为我使用的是构造函数Adapter(context,rowLayout,textVIEwResourceID,ArrayList< Items>),但只有当不可见的行成为getVIEw时才调用getVIEw方法可见.
这导致了一个问题,因为当我的列表首次显示时,只显示我文章的标题,我必须一直向下滚动列表,然后向上滚动,以便每行中的所有视图都能正确填充(因为任务在getVIEw中完成.
谁能指出我正确的方向?我怎么能重构这个,以便每个可见行中的所有视图立即填充?
代码到我的自定义适配器:
import java.util.ArrayList;import androID.app.Activity;import androID.content.Context;import androID.graphics.drawable.Drawable;import androID.vIEw.LayoutInflater;import androID.vIEw.VIEw;import androID.vIEw.VIEwGroup;import androID.Widget.ArrayAdapter;import androID.Widget.ImageVIEw;import androID.Widget.TextVIEw;public class ArticleArrayAdapter extends ArrayAdapter<Article> { private final Context context; private final ArrayList<Article> articles; @SuppressWarnings("unused") private final int rowLayout; public ArticleArrayAdapter(Context context, int rowLayout, int textVIEwResourceID, ArrayList<Article> articles) { super(context, rowLayout, textVIEwResourceID, articles); this.rowLayout=rowLayout; this.context = context; this.articles = articles; } @OverrIDe public VIEw getVIEw(int position, VIEw convertVIEw, VIEwGroup parent) { VIEw row = convertVIEw; if (row == null) { LayoutInflater inflater = ((Activity)context).getLayoutInflater(); row = inflater.inflate(R.layout.affichageitem, null); } else { TextVIEw vIEwTitre = (TextVIEw)row.findVIEwByID(R.ID.titre); TextVIEw vIEwAuteur = (TextVIEw)row.findVIEwByID(R.ID.auteur); TextVIEw vIEwDate = (TextVIEw)row.findVIEwByID(R.ID.date); ImageVIEw vIEwlogo = (ImageVIEw)row.findVIEwByID(R.ID.category_logo); vIEwTitre.setText(articles.get(position).getTitle()); vIEwAuteur.setText(articles.get(position).getCreator()); vIEwDate.setText(articles.get(position).getDate()); Drawable drawlogo = context.getResources().getDrawable(R.drawable.logocat); vIEwlogo.setimageDrawable(drawlogo); } return super.getVIEw(position, convertVIEw, parent); }}编辑版:
public class ArticleArrayAdapter extends ArrayAdapter<Article> { private final Context context; @SuppressWarnings("unused") private final int rowLayout; public ArticleArrayAdapter(Context context, int rowLayout,int textVIEwResourceID) { super(context, rowLayout, textVIEwResourceID); this.rowLayout=rowLayout; this.context = context; } @OverrIDe public VIEw getVIEw(int position, VIEw convertVIEw, VIEwGroup parent) { VIEw row = convertVIEw; if (row == null) { LayoutInflater inflater = ((Activity)context).getLayoutInflater(); row = inflater.inflate(R.layout.affichageitem, null); } else { TextVIEw vIEwTitre = (TextVIEw)row.findVIEwByID(R.ID.titre); TextVIEw vIEwAuteur = (TextVIEw)row.findVIEwByID(R.ID.auteur); TextVIEw vIEwDate = (TextVIEw)row.findVIEwByID(R.ID.date); ImageVIEw vIEwlogo = (ImageVIEw)row.findVIEwByID(R.ID.category_logo); vIEwTitre.setText(getItem(position).getTitle()); vIEwAuteur.setText(getItem(position).getCreator()); vIEwDate.setText(getItem(position).getDate()); Drawable drawlogo = context.getResources().getDrawable(R.drawable.logocat); vIEwlogo.setimageDrawable(drawlogo); } return super.getVIEw(position, convertVIEw, parent); // <<- ONLY TitleS //return row; <<- EMPTY }}的RowLayout:
<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="fill_parent" androID:layout_height="wrap_content" androID:orIEntation="horizontal" > <ImageVIEw androID:ID="@+ID/category_logo" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_gravity="center_vertical" androID:contentDescription="@string/logo_desc" androID:padding="20dp" /> <linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="0dp" androID:layout_height="wrap_content" androID:layout_weight="1" androID:orIEntation="vertical" androID:paddingleft="5dp" > <TextVIEw androID:ID="@+ID/titre" androID:layout_wIDth="fill_parent" androID:layout_height="wrap_content" androID:textSize="18dp" androID:textStyle="bold" /> <linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="fill_parent" androID:layout_height="wrap_content" androID:gravity="" androID:orIEntation="horizontal" > <TextVIEw androID:ID="@+ID/auteur" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:adjustVIEwBounds="true" androID:textSize="12dp" /> <TextVIEw androID:ID="@+ID/espace" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="@string/espace" androID:adjustVIEwBounds="true" androID:textSize="12dp" /> <TextVIEw androID:ID="@+ID/date" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:adjustVIEwBounds="true" androID:textSize="12dp" /> </linearLayout> </linearLayout></linearLayout>解决方法:
问题是您没有填充新视图.会发生什么是AndroID可能会保留固定数量的视图,这些视图将用于列表视图.这些视图被回收,这就是为什么在所有视图变得可见之前不可能“填充”它们的原因.这条线
if (vIEw == null) { LayoutInflater inflater = ((Activity)context).getLayoutInflater(); row = inflater.inflate(R.layout.affichageitem, null);}检查视图是否被回收. null表示不是,所以如果你得到null,你需要膨胀一个新的视图.你的代码很好.但是,您需要填充视图,看它是否是新增的视图.所以你不应该有else语句,只有
VIEw row = convertVIEw;if (row == null) { LayoutInflater inflater = ((Activity)context).getLayoutInflater(); row = inflater.inflate(R.layout.affichageitem, null);}TextVIEw vIEwTitre = (TextVIEw)row.findVIEwByID(R.ID.titre);TextVIEw vIEwAuteur = (TextVIEw)row.findVIEwByID(R.ID.auteur);TextVIEw vIEwDate = (TextVIEw)row.findVIEwByID(R.ID.date);ImageVIEw vIEwlogo = (ImageVIEw)row.findVIEwByID(R.ID.category_logo);vIEwTitre.setText(getItem(position).getTitle());vIEwAuteur.setText(getItem(position).getCreator());vIEwDate.setText(getItem(position).getDate());Drawable drawlogo = context.getResources().getDrawable(R.drawable.logocat);vIEwlogo.setimageDrawable(drawlogo);return row;它一直向下滚动的原因是,在你回来的路上,getVIEw正在接收回收的视图,它直接跳到了你拥有的else子句中.
总结以上是内存溢出为你收集整理的Android自定义适配器:仅在滚动时填充的列表项全部内容,希望文章能够帮你解决Android自定义适配器:仅在滚动时填充的列表项所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)