public void setSelected(int newState) { RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) mToggleSelector.getLayoutParams(); lp.topMargin = (mTableLayout.getMeasuredHeight() - heightItem) / 2; lp.leftMargin = newState * widthItem + mTableLayout.getPaddingLeft(); mToggleSelector.setLayoutParams(lp); if (Build.VERSION.SDK_INT < 11) { AlphaAnimation animation = new AlphaAnimation((float) 0.2, (float) 0.2); animation.setDuration(0); animation.setFillAfter(true); for (ImageView view : buttonList) { view.startAnimation(animation); } currentState = newState; animation = new AlphaAnimation(1, 1); animation.setDuration(0); animation.setFillAfter(true); buttonList.get(currentState).startAnimation(animation); } else { for (ImageView view : buttonList) { view.setAlpha((float) 0.2); } currentState = newState; buttonList.get(currentState).setAlpha((float) 1); } }
@Override protected void onLayout(boolean changed, int l, int t, int r, int b) { super.onLayout(changed, l, t, r, b); if (changed) { widthTableRow = mTableLayout.getMeasuredWidth() - mTableLayout.getPaddingLeft() - mTableLayout.getPaddingRight(); widthItem = widthTableRow / buttonList.size(); heightItem = mTableLayout.getMeasuredHeight() - mTableLayout.getPaddingTop() - mTableLayout.getPaddingBottom(); RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(widthItem, heightItem); mToggleSelector.setLayoutParams(lp); setSelected(currentState); } }