2

編集:この問題は、BaseAdapterが原因で、スクロール時にListViewの順序が狂うことに関連している可能性があります


私のメインアプリケーション画面は、GridViewを使用してアイコンのグリッドを表示します。各アイコンには、画像、名前、およびインテントがあります。背景を変更できるように、方向の変更を処理するようにアクティビティを設定しています。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のアイコンの順序が逆になります。そして、画面を元の方向に回転させると、すべてが通常の状態に戻ります。何が起きてる?

4

2 に答える 2

1

実際、カスタムアダプタに問題がありました。問題と解決策は両方ともここにありますBaseAdapterは、スクロールしたときにListViewの順序が狂う原因になります

于 2010-07-15T19:39:59.790 に答える
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();
}
于 2010-06-19T20:12:00.610 に答える