GridAdapter在屏幕方向改变时显示view0而不是view4

时间:2013-02-26 07:26:12

标签: android orientation android-gridview baseadapter onconfigurationchanged

我有GridView BaseAdapter,显示6个这样的按钮:

  • 人像:

B11 B12

B21 B22

B31 B32

  • 风景:

B11 B12 B21

B22 B31 B32

一切都很好,直到我从风景转回肖像。然后显示:

  • 人像:

B11 B12

B21 B22

B11 B32

所以它显示 B11 两次。当我点击它时,它仍然会得到正确的位置(4)并崩溃,因为它无法找到正确的按钮ID。

以下是适配器的代码:

public class RightSlideMenuGridAdapter extends BaseAdapter {
private Context mContext;
private String TAG = "RightSlideMenuGridAdapter";

public RightSlideMenuGridAdapter(Context c) {
    mContext = c;
}

public int getCount() {
    return 6;
}

public Object getItem(int position) {
    Log.i(TAG, "getItem" + Integer.toString(position));
    return null;
}

public long getItemId(int position) {
    Log.i(TAG, "getItemId" + Integer.toString(position));
    long retId = 0;
    // Nothing worked here. Maybe I should return something?
    return retId;
}

// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
    View child = convertView;
    if (child == null) {
        Context c = CobraApplication.getAppContext();
        Log.i(TAG, "getView" + Integer.toString(position));
        switch (position) {
        case 0:
            child = (View) ((LayoutInflater) CobraApplication
                    .getAppContext().getSystemService(
                            Context.LAYOUT_INFLATER_SERVICE)).inflate(
                    R.layout.toolscity, null);
            Button btnToolscityhwy = (Button) child
                    .findViewById(R.id.toolscityhwy);
            if (PersistentStoreHelper
                    .getDetectorSetting(DetectorSettings.DIR_MOB_IRAD_CITY_MODE
                            .name()) == CommunicationProtocol.DIR_MOB_IRAD_SETTING_HIGHWAY) { // VK:
                                                                                              // The
                                                                                              // setting
                                                                                              // is
                                                                                              // highway
                // VK: Show the highway icon
                btnToolscityhwy.setBackgroundDrawable(CobraApplication
                        .getAppContext().getResources()
                        .getDrawable(R.drawable.toolshwy));
                btnToolscityhwy.setText(c.getResources().getString(
                        R.string.city));
            } else {
                btnToolscityhwy.setBackgroundDrawable(CobraApplication
                        .getAppContext().getResources()
                        .getDrawable(R.drawable.toolscity));
                btnToolscityhwy.setText(c.getResources().getString(
                        R.string.highway));
            }
            break;

        case 1:
            child = (View) ((LayoutInflater) CobraApplication
                    .getAppContext().getSystemService(
                            Context.LAYOUT_INFLATER_SERVICE)).inflate(
                    R.layout.toolscarfinderbutton, null);
            ToggleButton btnToolsCarFinder = (ToggleButton) child
                    .findViewById(R.id.myToggleButtoncarfinder);
            // VK: Here we need to check the current setting of car finder
            // and set button state appropriately
            if (PersistentStoreHelper
                    .getNonDetectorSetting(NonDetectorSettings.CAR_FINDER_SETTING
                            .name()) == CommunicationProtocol.DIR_MOB_IRAD_SETTING_ON) {
                btnToolsCarFinder.setChecked(true);
            } else {
                btnToolsCarFinder.setChecked(false);
            }
            break;

        case 2:
            child = (View) ((LayoutInflater) CobraApplication
                    .getAppContext().getSystemService(
                            Context.LAYOUT_INFLATER_SERVICE)).inflate(
                    R.layout.toolstrafficbutton, null);
            ToggleButton btnToolsTraffic = (ToggleButton) child
                    .findViewById(R.id.myToggleButtonTraffic);
            btnToolsTraffic.setChecked(PersistentStoreHelper
                    .getTrafficDisplayState());
            break;

        case 3:
            child = (View) ((LayoutInflater) CobraApplication
                    .getAppContext().getSystemService(
                            Context.LAYOUT_INFLATER_SERVICE)).inflate(
                    R.layout.toolsmenulegend, null);
            break;

        case 4:
            child = (View) ((LayoutInflater) CobraApplication
                    .getAppContext().getSystemService(
                            Context.LAYOUT_INFLATER_SERVICE)).inflate(
                    R.layout.toolsmusic, null);
            ToggleButton btnToolsMusic = (ToggleButton) child
                    .findViewById(R.id.myToggleButtonMusicControl);
            btnToolsMusic.setChecked(PersistentStoreHelper
                    .getMusicControlDisplayState());
            break;

        case 5:
            child = (View) ((LayoutInflater) CobraApplication
                    .getAppContext().getSystemService(
                            Context.LAYOUT_INFLATER_SERVICE)).inflate(
                    R.layout.toolsdirectionsmenu, null);

            break;
        }
    }
    child.setPadding(0, 0, 0, 0);
    child.setFocusable(false);
    child.setClickable(false);

    return child;
}

以下是GridInit肖像的痕迹 - 好的。然后将方向改为横向 - 仍然可以。然后回到肖像。它会两次获得View。

02-26 09:17:14.960: I/RightSlideMenuGridAdapter(12207): getView0
02-26 09:17:14.970: I/RightSlideMenuGridAdapter(12207): getView1
02-26 09:17:14.985: I/RightSlideMenuGridAdapter(12207): getView2
02-26 09:17:15.005: I/RightSlideMenuGridAdapter(12207): getView3
02-26 09:17:15.015: I/RightSlideMenuGridAdapter(12207): getView4
02-26 09:17:15.030: I/RightSlideMenuGridAdapter(12207): getView5
02-26 09:17:20.660: I/RightSlideMenuGridView(12207): MenuSize:600
02-26 09:17:20.665: I/RightSlideMenuGridView(12207): MenuSize:600
02-26 09:17:20.680: I/RightSlideMenuGridAdapter(12207): getView0
02-26 09:17:20.685: I/RightSlideMenuGridAdapter(12207): getView1
02-26 09:17:20.690: I/RightSlideMenuGridAdapter(12207): getView2
02-26 09:17:20.695: I/RightSlideMenuGridAdapter(12207): getView3
02-26 09:17:20.700: I/RightSlideMenuGridAdapter(12207): getView4
02-26 09:17:20.705: I/RightSlideMenuGridAdapter(12207): getView5
02-26 09:17:20.825: I/RightSlideMenuGridAdapter(12207): getView0
02-26 09:17:20.835: I/RightSlideMenuGridAdapter(12207): getItemId0
02-26 09:17:28.680: I/RightSlideMenuGridView(12207): MenuSize:375
02-26 09:17:28.680: I/RightSlideMenuGridView(12207): MenuSize:375
02-26 09:17:28.700: I/RightSlideMenuGridAdapter(12207): getView0
02-26 09:17:28.705: I/RightSlideMenuGridAdapter(12207): getView1
02-26 09:17:28.710: I/RightSlideMenuGridAdapter(12207): getView2
02-26 09:17:28.715: I/RightSlideMenuGridAdapter(12207): getView3
02-26 09:17:28.905: I/RightSlideMenuGridAdapter(12207): getView0
02-26 09:17:28.935: I/RightSlideMenuGridAdapter(12207): getItemId0
02-26 09:17:28.940: I/RightSlideMenuGridAdapter(12207): getView5

0 个答案:

没有答案