处理方向更改后Android GridView反转

时间:2010-06-19 15:17:43

标签: android gridview screen-orientation

编辑:此问题可能与BaseAdapter causing ListView to go out of order when scrolled

有关

我的主应用程序屏幕使用GridView显示图标网格。每个图标都有一个图像,名称和意图。我将Activity设置为处理方向更改,以便我可以更改背景。 onCreate方法使用3个不执行任何操作的测试图标和一个打开PreferencesActivity的首选项图标来设置GridView:

super.onCreate(savedInstanceState);
setContentView(R.layout.main);

GridView gv = (GridView) findViewById(R.id.main_grid);
if (getResources().getConfiguration().orientation ==
        Configuration.ORIENTATION_LANDSCAPE)
    gv.setBackgroundResource(R.drawable.main_bg_horiz);
else
    gv.setBackgroundResource(R.drawable.main_bg);

ArrayList<Icon> icons = new ArrayList<Icon>();
icons.add(new Icon("Test 1", R.drawable.test1, null));
icons.add(new Icon("Test 2", R.drawable.test2, null));
icons.add(new Icon("Test 3", R.drawable.test3, null));
icons.add(new Icon("Preferences", R.drawable.perferences, new Intent(this,
        AlertPreferences.class)));
IconAdapter ia = new IconAdapter(icons);
gv.setAdapter(ia);
gv.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3){
        Intent i = ((IconAdapter)arg0.getAdapter()).getItem(arg2).getIntent();
        if (i != null)
            HomeActivity.this.startActivity(i);
    }
});

onConfigurationChanged非常简单:

super.onConfigurationChanged(newConfig);    
GridView gv = (GridView) findViewById(R.id.main_grid);

if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
    gv.setBackgroundResource(R.drawable.main_bg_horiz);
else
    gv.setBackgroundResource(R.drawable.main_bg);

后台变化很好,但代码引入了GridView的新问题。无论我在哪个方向启动应用程序都可以正常工作。但是,当我更改方向时,GridView中图标的顺序会反转。当我将屏幕旋转回它开始的方向时,一切都恢复正常。这是怎么回事?

2 个答案:

答案 0 :(得分:1)

实际上,自定义适配器存在问题。问题和解决方案都在这里BaseAdapter causing ListView to go out of order when scrolled

答案 1 :(得分:0)

尝试分离设置用户界面的逻辑:

GridView gv;

public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    configureYourUIHere();    
}

public void configureYourUIHere(){
    gv = (GridView) findViewById(R.id.main_grid);
    if (getResources().getConfiguration().orientation ==
            Configuration.ORIENTATION_LANDSCAPE)
        gv.setBackgroundResource(R.drawable.main_bg_horiz);
    else
        gv.setBackgroundResource(R.drawable.main_bg);

    ArrayList<Icon> icons = new ArrayList<Icon>();
    icons.add(new Icon("Test 1", R.drawable.test1, null));
    icons.add(new Icon("Test 2", R.drawable.test2, null));
    icons.add(new Icon("Test 3", R.drawable.test3, null));
    icons.add(new Icon("Preferences", R.drawable.perferences, new Intent(this,
            AlertPreferences.class)));
    IconAdapter ia = new IconAdapter(icons);
    gv.setAdapter(ia);
    gv.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3){
            Intent i = ((IconAdapter)arg0.getAdapter()).getItem(arg2).getIntent();
            if (i != null)
                HomeActivity.this.startActivity(i);
        }
    });
}

public void onConfigurationChanged(Configuration newConfig){
    super.onConfigurationChanged(newConfig);    
    configureYourUIHere();
}