Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Contains multiple {@link ScreenView} in variety of layouts.
- */
- public class ScreensLayout extends FrameLayout {
- private static final int DEFAULT_LAYOUT = R.layout.layout_grid_screens;
- /**
- * Actual layout which contains multiple {@link ScreenView}.
- */
- private ViewGroup layout;
- public ScreenView selectedScreen;
- //</editor-fold>
- //<editor-fold desc="Constructors">
- public ScreensLayout(Context context) {
- super(context);
- initialize(context, null, 0);
- }
- public ScreensLayout(Context context, AttributeSet attrs) {
- super(context, attrs);
- initialize(context, attrs, 0);
- }
- public ScreensLayout(Context context, AttributeSet attrs, int defStyle) {
- super(context, attrs, defStyle);
- initialize(context, attrs, defStyle);
- }
- //</editor-fold>
- private void setViewSelected(ScreenView view) {
- removeViewSelected();
- this.selectedScreen = view;
- Log.i("ScreensLayout", "setViewSelected: " + "Screen (" + ScreensLayout.this.getResources().getResourceEntryName(ScreensLayout.this.selectedScreen.getId()) + ") selected.");
- if (this.selectedScreen != null) {
- // change to a selected background for example
- }
- }
- private ScreenView getViewSelected() {
- if (this.selectedScreen != null) {
- return this.selectedScreen;
- }
- return null;
- }
- private void removeViewSelected() {
- if (this.selectedScreen != null) {
- Log.i("ScreensLayout", "setViewSelected: " + "Screen (" + ScreensLayout.this.getResources().getResourceEntryName(ScreensLayout.this.selectedScreen.getId()) + ") deselected.");
- // remove selected background for example
- this.selectedScreen = null;
- }
- // clear and reset the focus on the parent
- this.layout.clearFocus();
- this.layout.requestFocus();
- }
- private void initialize(final Context context, AttributeSet attributeSet, int defStyleAttr) {
- if (this.isInEditMode()) {
- return;
- }
- this.selectedScreen = null;
- if (attributeSet != null) {
- final TypedArray array = this.getContext().obtainStyledAttributes(
- attributeSet, R.styleable.ScreenView, defStyleAttr, defStyleAttr);
- changeLayout(context);
- changeSplit(ScreenSplit.valueOf(ScreenSplit.DEFAULT_SPLIT.name()));
- this.layout.setOnFocusChangeListener(new OnFocusChangeListener() {
- @Override
- public void onFocusChange(View v, boolean hasFocus) {
- ScreenView viewSelected = ScreensLayout.this.getViewSelected();
- if (viewSelected != null && viewSelected.hasFocus() == false) {
- ScreensLayout.this.removeViewSelected();
- }
- //Log.i("ScreensLayout", "onFocusChange: " + " (" + ScreensLayout.this.getResources().getResourceEntryName(v.getId()) + ") focus = " + hasFocus);
- }
- });
- this.layout.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- ScreensLayout.this.layout.clearFocus();
- ScreensLayout.this.layout.requestFocus();
- }
- });
- ScreenView child;
- for (int index = 0; index < ScreenSplit.MAX_SPLIT.getScreens(); index++) {
- child = (ScreenView) this.layout.getChildAt(index);
- child.setOnLongClickListener(new OnLongClickListener() {
- @Override
- public boolean onLongClick(View v) {
- ScreenView selectedScreen = ScreensLayout.this.getViewSelected();
- if (selectedScreen == null || selectedScreen.equals(v) == false) {
- // whether selected child wasn't long clicked again
- ScreensLayout.this.setViewSelected((ScreenView) v);
- }
- return true;
- }
- });
- child.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- ScreensLayout.this.removeViewSelected();
- }
- });
- child.setOnFocusChangeListener(new OnFocusChangeListener() {
- @Override
- public void onFocusChange(View v, boolean hasFocus) {
- //Log.i("ScreensLayout", "onFocusChange child: " + "Screen (" + ScreensLayout.this.getResources().getResourceEntryName(v.getId()) + ") focus = " + hasFocus);
- }
- });
- }
- array.recycle();
- }
- }
- /**
- * Changes visible screens number in current layout and save it.
- *
- * @param split Requested split.
- */
- public void changeSplit(ScreenSplit split) {
- ScreenView child;
- int index = 0;
- for (; index < split.getScreens(); index++) {
- child = (ScreenView) this.layout.getChildAt(index);
- child.setVisibility(VISIBLE);
- }
- for (; index < this.layout.getChildCount(); index++) {
- child = (ScreenView) this.layout.getChildAt(index);
- child.setVisibility(GONE);
- }
- }
- private void changeLayout(Context context) {
- ViewGroup container = (ViewGroup) LayoutInflater.from(context).inflate(DEFAULT_LAYOUT, this, true);
- this.layout = (ViewGroup) container.getChildAt(0);
- }
- }
Add Comment
Please, Sign In to add comment