List of usage examples for android.widget ScrollView getDrawingRect
public void getDrawingRect(Rect outRect)
From source file:com.bq.robotic.robopad_plusplus.fragments.ScheduleRobotMovementsFragment.java
/** * Set the listeners to the views that need them. It must be done here in the fragment in order * to get the callback here and not in the FragmentActivity, that would be a mess with all the * callbacks of all the possible fragments *//* w w w . j ava 2 s. com*/ private void setUiListeners() { gridView.setOnRearrangeListener(new OnRearrangeListener() { public void onRearrange(int oldIndex, int newIndex) { if (scheduledControls.isEmpty()) { return; } String scheduledControl = scheduledControls.remove(oldIndex); if (oldIndex < newIndex) scheduledControls.add(newIndex, scheduledControl); else scheduledControls.add(newIndex, scheduledControl); } public void onRearrange(boolean isDraggedDeleted, int draggedDeletedIndex) { if (scheduledControls.isEmpty()) { return; } if (isDraggedDeleted) { scheduledControls.remove(draggedDeletedIndex); } } }); GridLayout movementsViews = (GridLayout) getActivity().findViewById(R.id.type_of_movements_container); for (int i = 0; i < movementsViews.getChildCount(); i++) { movementsViews.getChildAt(i).setOnClickListener(onMovementsButtonClick); } LinearLayout optionsLayout = (LinearLayout) getActivity().findViewById(R.id.menu_options_container); for (int i = 0; i < optionsLayout.getChildCount(); i++) { optionsLayout.getChildAt(i).setOnClickListener(onOptionsButtonClick); } // Resize the GridLayout depending on the number of icons (depending on the current robot // selected). In order not to get too much number of columns in little screen, it is limited // to two and scroll the screen if there are more icons. If scroll is not needed the icons // are centered in the layout final GridLayout typeOfMovementsContainer = (GridLayout) getActivity() .findViewById(R.id.type_of_movements_container); final ViewTreeObserver vto = typeOfMovementsContainer.getViewTreeObserver(); final ViewTreeObserver.OnPreDrawListener preDrawListener = new ViewTreeObserver.OnPreDrawListener() { public boolean onPreDraw() { Rect scrollBounds = new Rect(); ScrollView scroll = (ScrollView) getActivity().findViewById(R.id.scroll_container); scroll.getDrawingRect(scrollBounds); // int finalHeight = scrollBounds.bottom - getActivity().getResources().getDimensionPixelSize(R.dimen.scheduler_grid_margin); int finalHeight = scrollBounds.bottom; int childCount = typeOfMovementsContainer.getChildCount(); if (childCount > 1) { for (int i = 0; i < childCount; i++) { if (typeOfMovementsContainer.getChildAt(i).getBottom() > finalHeight) { typeOfMovementsContainer.setColumnCount(2); break; } } } scroll.invalidate(); final ViewTreeObserver vto = typeOfMovementsContainer.getViewTreeObserver(); vto.removeOnPreDrawListener(this); return true; } }; vto.addOnPreDrawListener(preDrawListener); }