每當我改變手機的方向時,我都會看到網格視圖中的項目順序顛倒,即現在第一個位置的東西到了末尾,依此類推。Android網格視圖按相反順序加載
我該如何擺脫這個問題?根據點擊的位置,我正在調用不同的活動。但是,由於順序改變,當屏幕方向改變時錯誤的活動被調用。
我在清單文件中添加了android:configChanges="orientation|keyboardHidden"
。
任何幫助表示讚賞。
適配器類別:
class ImageAdapter extends BaseAdapter {
Context mContext;
private String[] mHome_icon_text = { "A", "B",
"C", "D", "E","F" };
private Integer[] mHome_icon_image = { R.drawable.icon,
R.drawable.icon, R.drawable.icon, R.drawable.icon,
R.drawable.icon, R.drawable.icon, };
public ImageAdapter(Context c) {
mContext = c;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mHome_icon_text.length;
}
@Override
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View v;
if (convertView == null) {
LayoutInflater li = getLayoutInflater();
v = li.inflate(R.layout.home_grid_view_item, null);
TextView tv = (TextView) v.findViewById(R.id.home_icon_text);
tv.setText(mHome_icon_text[position]);
ImageView iv = (ImageView) v.findViewById(R.id.home_icon_image);
iv.setImageResource(mHome_icon_image[position]);
} else {
v = convertView;
}
return v;
}
}
在主類:
grid_main.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
Intent i;
switch (position) {
case 0:
// start A
i = new Intent(Home.this,
com.da.A.class);
startActivity(i);
break;
case 3: //Cactivity
i = new Intent(Home.this,
com.da.C.class);
startActivity(i);
break;
default:
Toast
.makeText(Home.this, "" + position,
Toast.LENGTH_SHORT).show();
}
}
});
我認爲我們需要更多的努力。你如何填充網格?你可以發佈一些代碼嗎? – 2011-06-08 13:55:53
ImageAdapter class code:private String [] mHome_icon_text = {「A」,「B」,「C」,「D」,「E」,「F」}; LayoutInflater li = getLayoutInflater(); \t \t \t \t v = li.inflate(R.layout.home_grid_view_item,null); \t \t \t \t TextView tv =(TextView)v.findViewById(R.id.home_icon_text); \t \t \t \t tv.setText(mHome_icon_text [position]); \t \t \t \t ImageView iv =(ImageView)v.findViewById(R.id.home_icon_image); \t \t \t \t \t \t \t \t iv.setImageResource(mHome_icon_image [位置]);在itemclicklistener函數中:switch(position){case 0: i = new Intent(Home.this,com.da.AHome.class); \t \t startActivity(i); \t \t \t break; – Sapan 2011-06-08 14:05:16
請編輯您的代碼到問題中。閱讀評論是不可能的。 – 2011-06-08 14:13:42