這是我的解決方案:
我的子類的GridView與構造(上下文,AttributeSet中):
(這對我來說必須在一個單獨的文件類完成)
和overrided onSizeChanged方法
MyGrid.java
public class MyGrid extends GridView {
public void MyGrid(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
// TODO Auto-generated method stub
// Here I call my custom functions to prepare layout of my custom listview
super.onSizeChanged(w, h, oldw, oldh);
}
}
在類活動比使用GridView,
我已override onStart方法
(在OnCreate和onRestart之後調用 [當你來自另一個活動])
MyActivity.java
public class MyActivity extends Activity {
onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(...)
...
}
....
protected void onStart() {
// TODO Auto-generated method stub
// call db and create cursor
// 'mygridview' = findbyid MyGrid
// (declared on xml like '<packagepath>'.mygrid
// setadapter with my costom adapeter
// **HERE THE CALLED ONSIZECHANGED**
// I make test proper for my app
// (there is a simple test)
if ('mygridview'.getMeasuredHeight() > 0) {
// I use the same width and height for new and old measures
// because for me that is right
'mygridview'.onSizeChanged(gw.getWidth(), gw.getHeight(),
gw.getWidth(), gw.getHeight());
}
super.onStart();
}
}
使用這種方法,我可以隨時調整網格大小。 我希望這對你有所幫助。
如果您已將高度設置爲FILL_PARENT,則這不起作用。如果你這樣做,然後改變方向,你將有不正確的高度。 – 2011-09-17 00:42:28
這是一個破解快速滾動工作,當列表是動態的,並得到更新。 – PravinCG 2011-09-17 07:11:42
感謝這解決了我長期堅持的問題 – reiley 2012-07-25 12:26:39