次のような 6 つのボタンを表示GridView
するがあります。BaseAdapter
- ポートレート:
B11 B12
B21 B22
B31 B32
- 風景:
B11 B12 B21
B22 B31 B32
横向きから縦向きに切り替えるまで、すべてがうまく機能します。次に、次のように表示されます。
- ポートレート:
B11 B12
B21 B22
B11 B32
したがって、B112回表示されます。クリックすると、正しい位置 (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
- OK。次に、向きを横向きに変更します - それでも問題ありません。その後、ポートレートに戻ります。そして、View を 2 回取得します。
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