Skip to content

Instantly share code, notes, and snippets.

@layerlre
Created April 29, 2015 11:15
Show Gist options
  • Save layerlre/91871aeefa02292cd7e9 to your computer and use it in GitHub Desktop.
Save layerlre/91871aeefa02292cd7e9 to your computer and use it in GitHub Desktop.
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
v = inflater.inflate(R.layout.xxxx, container, false);
gridView = (GridView)v.findViewById(R.id.gridview);
gridView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
if (gridView.getColumnWidth()>0) {
seteGridviewHeight(gridView,จำนวนitem,gridView.getNumColumns());
gridView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
}
});
return v;
}
private void seteGridviewHeight(GridView gridView, float itemCount,float numColumn) {
int row = (int) Math.ceil((itemCount + 1f) / numColumn);
Log.d("debug", "-------row = " + row);
int height = (imageHeight * row); // imageHeight = ความสูงของแต่ละแถวหรือความสูงของรูป
height += (row - 1) * verticalSpacing;
gridView.getLayoutParams().height = height;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment