7

嗨,下面是我的 baseadapter 课程,但它不能正常工作:

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。所有布局都有根据图像数量的图像视图。有人能告诉我我必须做什么吗?

4

1 回答 1

9

由于您需要为不同的行使用不同的布局,请使用以下方法

getViewTypeCount() - 返回有多少行类型的信息。

getItemViewType(int position) - 根据位置返回您应该使用哪种布局类型的信息

使用 getItemViewType 你可以定义你需要使用的布局。

像这样

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

于 2014-09-09T10:35:06.650 回答