我的问题是关于列表。假设我的列表中有3种不同的项目,
1-没有图像的列表项-带有封面图像的列表项3-以2x平方图像替换封面图像的列表项。
让我说我可以像这样构造一个列表项目
<LinearLayout>
<ImageView visibility gone /><ImageView with visibility gone/>
</LinearLayout>因此,当我为这个列表视图编写适配器时,对这些视图使用不同的布局,还是使用上面这样的布局,并根据图像计数隐藏/显示图像视图,会更方便吗?还有更好的吗?谢谢
发布于 2015-02-06 07:46:02
我会覆盖getViewTypeCount()和getItemViewType(int position),并根据它在我的getView()中隐藏/显示ImageViews。您的3种类型的项目没有明显的不同,最终的结果可以很容易地通过隐藏/显示。同时,当您使用getItemViewType(int position)时,Android仍将重用列表中的许多视图。
发布于 2015-02-06 10:10:28
我就这样解决了那个案子。希望对你有帮助。首先,您必须为不同类型创建模型。您应该为不同的模型创建自定义布局。例如,我有两种类型: OneObject和TwoObject
public class MainObject { String type;
public MainObject(String t){ this.type = t;}
}
public class OneObject extends MainObject { String image;
public OneObject(String t, String image){
super(t);
this.image = image;
}
}
public class TwoObject extends MainObject { String place;
public TwoObject(String t, String p) {
super(t);
this.place = p;
}
}然后。当您将数据添加到数组列表中时,您可以这样添加。
ArrayList<MainObject> objlist = new ArrayList<>();
objlist.add(new MainObject("main"));
objlist.add(new OneObject("one", "image"));
objlist.add(new TwoObject("two","place"));在适配器中,您将得到那个ArrayList.And,然后您可以检查您应该转换哪个模型,以及应该使用哪个布局。例如..。
ArrayList<MainObject> objlist = new ArrayList<>();
//IN ADAPTER
..............
MainObject obj = objlist.get(position);
//u can check in other way
//if(obj.type.equals("one"))
if(obj instanceof OneObject){
//this object is "OneObject"
}
//else if(obj.type.equals("two"))
else if(obj instanceof TwoObject){
//this object is "TwoObject"
}
//else if(obj.type.equals("main"))
else if(obj instanceof MainObject){
//this object is "MainObject"
}https://stackoverflow.com/questions/28360769
复制相似问题