Gridview 适配器在横向上的不同行为

Gridview adapter different behavior on landscape

我的 gridview 在 4x2 网格中包含 8 个项目。 这是我的适配器代码:

public View getView(int position, View convertView, ViewGroup parent) {
    CustomCell v;
    Log.d("position: ", " " + position + " " + convertView);

    if(convertView == null)
    {
        switch (position){
            case 3:
                v = new CustomCellExercise(mContext);
                break;
            case 5:
                v = new CustomCellDrink(mContext);
                break;
            case 7:
                v = new CustomCellSleep(mContext);
                break;
            default:
                v = new CustomCell(mContext);
                break;
        }

    }
    else{
        v = (CustomCell)convertView;
    } 

    //Some text and color settings

    return v;
}

当我在纵向屏幕上使用它时,它工作正常,最后一项有 class "CustomCellSleep"。但是,当我将屏幕切换到横向模式时,适配器永远不会进入 "case 7" 并且最后一项有 "CustomCell" class。我使用此解决方案是因为 Exercise、Drink 和 Sleep 单元格具有与 CustomCell 不同的功能和覆盖方法。

尝试每次都初始化convertView, 这意味着不要使用

if(convertView == null){

}