具有多种类型的行布局的ListView-BaseAdapter在Android中无法正常工作 [英] ListView with multiple types of row layout - BaseAdapter is not working properly in Android
问题描述
您好,以下是我的baseadapter班,但无法正常工作:
Hi below is my class for baseadapter, but it is not working properly:
private static class MyBaseAdapter extends BaseAdapter {
private Context context;
private LayoutInflater inflater;
private MyBaseAdapter(Context context, FlipViewController controller) {
inflater = LayoutInflater.from(context);
this.context = context;
}
@Override
public int getCount() {
return Globals.list_album.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View layout = convertView;
if (convertView == null )
{
if (Globals.list_album.get(position).no_of_images == 1) {
layout = inflater.inflate(R.layout.single_image_adapter, null);
}
else if(Globals.list_album.get(position).no_of_images == 2) {
layout = inflater.inflate(R.layout.two_image_adapter, null);
}
else if(Globals.list_album.get(position).no_of_images == 3) {
layout = inflater.inflate(R.layout.three_image_adapter, null);
}
else if(Globals.list_album.get(position).no_of_images == 4) {
layout = inflater.inflate(R.layout.four_image_adapter, null);
}
else if(Globals.list_album.get(position).no_of_images == 5) {
layout = inflater.inflate(R.layout.five_image_adapter, null);
}
}
return layout;
}
}
我想根据Globals.list_album中的图像编号将布局加载到每个位置.但是它不能正常工作.它不适用于no_of_images = 5和2,因为我在列表中有这样的值.当前,我的no_of_images值为4,3,1,2和5.因此,它应该显示布局four_image_adapter,Three_image_adapter,one_image_adapter,two_image_adapter和Five_image_adapter.但是它显示four_image_adapter,Three_image_adapter,one_image_adapter,four_image_adapter和three_image_adapter. 所有布局都有根据图像数量显示的图像视图. 有人可以告诉我我该怎么做吗?
I want to load the layout according to the no of images in Globals.list_album to each position. But it is not working properly. It does not work for no_of_images = 5 and 2 as I have such values in list. Currently my values for no_of_images are 4,3,1,2 and 5. So it should display the layouts four_image_adapter, three_image_adapter, one_image_adapter, two_image_adapter and five_image_adapter. But it displays four_image_adapter, three_image_adapter, one_image_adapter, four_image_adapter and three_image_adapter. All layouts have imageviews according to number of images. Will anybody please tell me what I have to do?
推荐答案
由于您需要为不同的行使用不同的布局,因此请使用以下方法
Since you need different layout for different rows, use below methods
getViewTypeCount()-返回信息多少行类型.
getViewTypeCount() - returns information how many types of rows.
getItemViewType(int position)-返回信息,您应该根据位置使用哪种布局类型
getItemViewType(int position) - returns information which layout type you should use based on position
使用getItemViewType可以定义需要使用的布局.
using getItemViewType you can define the layout you need to use.
赞
public static final int TYPE_1 = 1;
public static final int TYPE_2 = 2;
public int getItemTypeCount(){
return 5;
}
public int getItemType(int position){
// Your if else code and return type ( TYPE_1 to TYPE_5 )
}
public View getView(int position, View convertView, ViewGroup parent){
// Return the right kind of view based on getItemType(position)
int type = getItemType(position);
if(type == TYPE_1){
// create (or reuse) TYPE_1 view
} else if() {
}......
return myView;
}
样品
http://android.amberfog.com/?p=296
http://www.survivingwithandroid.com/2014/08/android -listview-with-multiple-row.html
这篇关于具有多种类型的行布局的ListView-BaseAdapter在Android中无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!