我在Android项目中使用 GridView ,其中为numColumns设置了auto_fit属性(将columnWidth设置为48dp)。
我想在设置适配器之前知道 numColumns 值,因此我可以设置值大小。
但如果我在onCreateView中调用它,则为-1
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
int numColumns = gv.getNumColumns();
if(numColumns==-1) numColumns = 3; //currently I've put a workaround as hardcode
String [] values = new String[numColumns*10];
int ctr = 0;
for (int i = numColumns * (sectionNumber)+1; i <= (numColumns +1) * (sectionNumber); i++) {
for (int j = 1; j <= 10; j++) {
values[ctr++] = j * i + "";
}
}
ArrayAdapter<String> adapter = new MyAdapter(getActivity(),values);
gv.setAdapter(adapter);
return rootView;
}