codeido

ScreensLayout.java

Dec 30th, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.71 KB | None | 0 0
  1. /**
  2.  * Contains multiple {@link ScreenView} in variety of layouts.
  3.  */
  4. public class ScreensLayout extends FrameLayout {
  5.     private static final int DEFAULT_LAYOUT = R.layout.layout_grid_screens;
  6.     /**
  7.      * Actual layout which contains multiple {@link ScreenView}.
  8.      */
  9.     private ViewGroup layout;
  10.     public ScreenView selectedScreen;
  11.     //</editor-fold>
  12.  
  13.     //<editor-fold desc="Constructors">
  14.     public ScreensLayout(Context context) {
  15.         super(context);
  16.         initialize(context, null, 0);
  17.     }
  18.  
  19.     public ScreensLayout(Context context, AttributeSet attrs) {
  20.         super(context, attrs);
  21.         initialize(context, attrs, 0);
  22.     }
  23.  
  24.     public ScreensLayout(Context context, AttributeSet attrs, int defStyle) {
  25.         super(context, attrs, defStyle);
  26.         initialize(context, attrs, defStyle);
  27.     }
  28.     //</editor-fold>
  29.  
  30.     private void setViewSelected(ScreenView view) {
  31.         removeViewSelected();
  32.  
  33.         this.selectedScreen = view;
  34.         Log.i("ScreensLayout", "setViewSelected: " + "Screen (" + ScreensLayout.this.getResources().getResourceEntryName(ScreensLayout.this.selectedScreen.getId()) + ") selected.");
  35.         if (this.selectedScreen != null) {
  36.             // change to a selected background for example
  37.         }
  38.     }
  39.  
  40.     private ScreenView getViewSelected() {
  41.         if (this.selectedScreen != null) {
  42.             return this.selectedScreen;
  43.         }
  44.         return null;
  45.     }
  46.  
  47.     private void removeViewSelected() {
  48.         if (this.selectedScreen != null) {
  49.             Log.i("ScreensLayout", "setViewSelected: " + "Screen (" + ScreensLayout.this.getResources().getResourceEntryName(ScreensLayout.this.selectedScreen.getId()) + ") deselected.");
  50.             // remove selected background for example
  51.             this.selectedScreen = null;
  52.         }
  53.         // clear and reset the focus on the parent
  54.         this.layout.clearFocus();
  55.         this.layout.requestFocus();
  56.     }
  57.  
  58.     private void initialize(final Context context, AttributeSet attributeSet, int defStyleAttr) {
  59.         if (this.isInEditMode()) {
  60.             return;
  61.         }
  62.  
  63.         this.selectedScreen = null;
  64.  
  65.         if (attributeSet != null) {
  66.             final TypedArray array = this.getContext().obtainStyledAttributes(
  67.                     attributeSet, R.styleable.ScreenView, defStyleAttr, defStyleAttr);
  68.  
  69.             changeLayout(context);
  70.  
  71.             changeSplit(ScreenSplit.valueOf(ScreenSplit.DEFAULT_SPLIT.name()));
  72.  
  73.             this.layout.setOnFocusChangeListener(new OnFocusChangeListener() {
  74.                 @Override
  75.                 public void onFocusChange(View v, boolean hasFocus) {
  76.                     ScreenView viewSelected = ScreensLayout.this.getViewSelected();
  77.                     if (viewSelected != null && viewSelected.hasFocus() == false) {
  78.                         ScreensLayout.this.removeViewSelected();
  79.                     }
  80.                     //Log.i("ScreensLayout", "onFocusChange: " + " (" + ScreensLayout.this.getResources().getResourceEntryName(v.getId()) + ") focus = " + hasFocus);
  81.                 }
  82.             });
  83.  
  84.             this.layout.setOnClickListener(new OnClickListener() {
  85.                 @Override
  86.                 public void onClick(View v) {
  87.                     ScreensLayout.this.layout.clearFocus();
  88.                     ScreensLayout.this.layout.requestFocus();
  89.                 }
  90.             });
  91.  
  92.             ScreenView child;
  93.             for (int index = 0; index < ScreenSplit.MAX_SPLIT.getScreens(); index++) {
  94.                 child = (ScreenView) this.layout.getChildAt(index);
  95.                 child.setOnLongClickListener(new OnLongClickListener() {
  96.                     @Override
  97.                     public boolean onLongClick(View v) {
  98.                         ScreenView selectedScreen = ScreensLayout.this.getViewSelected();
  99.                         if (selectedScreen == null || selectedScreen.equals(v) == false) {
  100.                             // whether selected child wasn't long clicked again
  101.                             ScreensLayout.this.setViewSelected((ScreenView) v);
  102.                         }
  103.                         return true;
  104.                     }
  105.                 });
  106.                 child.setOnClickListener(new OnClickListener() {
  107.                     @Override
  108.                     public void onClick(View v) {
  109.                         ScreensLayout.this.removeViewSelected();
  110.                     }
  111.                 });
  112.                 child.setOnFocusChangeListener(new OnFocusChangeListener() {
  113.                     @Override
  114.                     public void onFocusChange(View v, boolean hasFocus) {
  115.                         //Log.i("ScreensLayout", "onFocusChange child: " + "Screen (" + ScreensLayout.this.getResources().getResourceEntryName(v.getId()) + ") focus = " + hasFocus);
  116.                     }
  117.                 });
  118.             }
  119.             array.recycle();
  120.         }
  121.     }
  122.     /**
  123.      * Changes visible screens number in current layout and save it.
  124.      *
  125.      * @param split Requested split.
  126.      */
  127.     public void changeSplit(ScreenSplit split) {
  128.         ScreenView child;
  129.  
  130.         int index = 0;
  131.         for (; index < split.getScreens(); index++) {
  132.             child = (ScreenView) this.layout.getChildAt(index);
  133.             child.setVisibility(VISIBLE);
  134.         }
  135.         for (; index < this.layout.getChildCount(); index++) {
  136.             child = (ScreenView) this.layout.getChildAt(index);
  137.             child.setVisibility(GONE);
  138.         }
  139.     }
  140.  
  141.     private void changeLayout(Context context) {
  142.         ViewGroup container = (ViewGroup) LayoutInflater.from(context).inflate(DEFAULT_LAYOUT, this, true);
  143.         this.layout = (ViewGroup) container.getChildAt(0);
  144.     }
  145. }
Add Comment
Please, Sign In to add comment