/virtualview/src/main/java/com/tmall/wireless/vaf/virtualview/view/grid/Grid.java

https://github.com/alibaba/Virtualview-Android · Java · 296 lines · 224 code · 46 blank · 26 comment · 28 complexity · 683c7d0192ddea966889baa2d2566b1d MD5 · raw file

  1. /*
  2. * MIT License
  3. *
  4. * Copyright (c) 2018 Alibaba Group
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in all
  14. * copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. * SOFTWARE.
  23. */
  24. package com.tmall.wireless.vaf.virtualview.view.grid;
  25. import android.text.TextUtils;
  26. import android.util.Log;
  27. import android.view.View;
  28. import com.libra.Utils;
  29. import com.libra.virtualview.common.StringBase;
  30. import com.tmall.wireless.vaf.framework.VafContext;
  31. import com.tmall.wireless.vaf.framework.cm.ContainerService;
  32. import com.tmall.wireless.vaf.virtualview.core.IContainer;
  33. import com.tmall.wireless.vaf.virtualview.core.NativeViewBase;
  34. import com.tmall.wireless.vaf.virtualview.core.ViewBase;
  35. import com.tmall.wireless.vaf.virtualview.core.ViewCache;
  36. import com.tmall.wireless.vaf.virtualview.core.ViewCache.Item;
  37. import com.tmall.wireless.vaf.virtualview.event.EventData;
  38. import com.tmall.wireless.vaf.virtualview.event.EventManager;
  39. import org.json.JSONArray;
  40. import org.json.JSONException;
  41. import org.json.JSONObject;
  42. /**
  43. * Created by gujicheng on 16/10/27.
  44. */
  45. public class Grid extends NativeViewBase {
  46. private final static String TAG = "Grid_TMTEST";
  47. private GridImp mNative;
  48. public Grid(VafContext context, ViewCache viewCache) {
  49. super(context, viewCache);
  50. mNative = new GridImp(context.forViewConstruction());
  51. mNative.setVirtualView(this);
  52. __mNative = mNative;
  53. }
  54. @Override
  55. public void reset() {
  56. super.reset();
  57. recycleViews();
  58. }
  59. @Override
  60. public boolean isContainer() {
  61. return true;
  62. }
  63. private void recycleViews() {
  64. ContainerService cm = mContext.getContainerService();
  65. int childCount = mNative.getChildCount();
  66. for (int i = 0; i < childCount; ++i) {
  67. IContainer c = (IContainer) mNative.getChildAt(i);
  68. cm.recycle(c);
  69. }
  70. mNative.removeAllViews();
  71. }
  72. @Override
  73. public void setData(Object data) {
  74. super.setData(data);
  75. if (data instanceof JSONObject) {
  76. JSONObject obj = (JSONObject) data;
  77. data = obj.optJSONArray(this.getDataTag());
  78. } else if (data instanceof com.alibaba.fastjson.JSONObject) {
  79. com.alibaba.fastjson.JSONObject obj = (com.alibaba.fastjson.JSONObject) data;
  80. data = obj.getJSONArray(this.getDataTag());
  81. }
  82. recycleViews();
  83. if (data instanceof JSONArray) {
  84. JSONArray arr = (JSONArray) data;
  85. ContainerService cm = mContext.getContainerService();
  86. int len = arr.length();
  87. for (int i = 0; i < len; ++i) {
  88. try {
  89. JSONObject obj = arr.getJSONObject(i);
  90. String type = obj.optString(TYPE);
  91. if (!TextUtils.isEmpty(type)) {
  92. View v = cm.getContainer(type);
  93. if (null != v) {
  94. ViewBase vb = ((IContainer) v).getVirtualView();
  95. vb.setVData(obj);
  96. mNative.addView(v);
  97. if (vb.supportExposure()) {
  98. mContext.getEventManager().emitEvent(EventManager.TYPE_Exposure, EventData.obtainData(mContext, vb));
  99. }
  100. vb.ready();
  101. } else {
  102. Log.e(TAG, "create view failed");
  103. }
  104. } else {
  105. Log.e(TAG, "get type failed");
  106. }
  107. } catch (JSONException e) {
  108. Log.e(TAG, "get json object failed:" + e);
  109. }
  110. }
  111. } else if (data instanceof com.alibaba.fastjson.JSONArray) {
  112. com.alibaba.fastjson.JSONArray arr = (com.alibaba.fastjson.JSONArray) data;
  113. ContainerService cm = mContext.getContainerService();
  114. int len = arr.size();
  115. for (int i = 0; i < len; ++i) {
  116. com.alibaba.fastjson.JSONObject obj = arr.getJSONObject(i);
  117. String type = obj.getString(TYPE);
  118. if (!TextUtils.isEmpty(type)) {
  119. View v = cm.getContainer(type);
  120. if (null != v) {
  121. ViewBase vb = ((IContainer) v).getVirtualView();
  122. vb.setVData(obj);
  123. mNative.addView(v);
  124. if (vb.supportExposure()) {
  125. mContext.getEventManager().emitEvent(EventManager.TYPE_Exposure, EventData.obtainData(mContext, vb));
  126. }
  127. vb.ready();
  128. } else {
  129. Log.e(TAG, "create view failed");
  130. }
  131. } else {
  132. Log.e(TAG, "get type failed");
  133. }
  134. }
  135. } else {
  136. Log.e(TAG, "setData not array");
  137. }
  138. }
  139. @Override
  140. public void onParseValueFinished() {
  141. super.onParseValueFinished();
  142. mNative.setAutoDimDirection(mAutoDimDirection);
  143. mNative.setAutoDimX(mAutoDimX);
  144. mNative.setAutoDimY(mAutoDimY);
  145. }
  146. @Override
  147. protected boolean setAttribute(int key, float value) {
  148. boolean ret = super.setAttribute(key, value);
  149. if (!ret) {
  150. ret = true;
  151. switch (key) {
  152. case StringBase.STR_ID_itemHeight:
  153. mNative.setItemHeight(Utils.dp2px(value));
  154. break;
  155. case StringBase.STR_ID_itemHorizontalMargin:
  156. mNative.setItemHorizontalMargin(Utils.dp2px(value));
  157. break;
  158. case StringBase.STR_ID_itemVerticalMargin:
  159. mNative.setItemVerticalMargin(Utils.dp2px(value));
  160. break;
  161. default:
  162. ret = false;
  163. break;
  164. }
  165. }
  166. return ret;
  167. }
  168. @Override
  169. protected boolean setAttribute(int key, int value) {
  170. boolean ret = true;
  171. switch (key) {
  172. case StringBase.STR_ID_colCount:
  173. mNative.setColumnCount(value);
  174. break;
  175. case StringBase.STR_ID_itemHeight:
  176. mNative.setItemHeight(Utils.dp2px(value));
  177. break;
  178. case StringBase.STR_ID_itemHorizontalMargin:
  179. mNative.setItemHorizontalMargin(Utils.dp2px(value));
  180. break;
  181. case StringBase.STR_ID_itemVerticalMargin:
  182. mNative.setItemVerticalMargin(Utils.dp2px(value));
  183. break;
  184. default:
  185. ret = super.setAttribute(key, value);
  186. break;
  187. }
  188. return ret;
  189. }
  190. @Override
  191. protected boolean setAttribute(int key, String value) {
  192. boolean ret = true;
  193. switch (key) {
  194. case StringBase.STR_ID_itemHorizontalMargin:
  195. mViewCache.put(this, StringBase.STR_ID_itemHorizontalMargin, value, Item.TYPE_FLOAT);
  196. break;
  197. case StringBase.STR_ID_itemVerticalMargin:
  198. mViewCache.put(this, StringBase.STR_ID_itemVerticalMargin, value, Item.TYPE_FLOAT);
  199. break;
  200. default:
  201. ret = super.setAttribute(key, value);
  202. break;
  203. }
  204. return ret;
  205. }
  206. @Override
  207. protected boolean setRPAttribute(int key, float value) {
  208. boolean ret = true;
  209. switch (key) {
  210. case StringBase.STR_ID_itemHeight:
  211. mNative.setItemHeight(Utils.rp2px(value));
  212. break;
  213. case StringBase.STR_ID_itemHorizontalMargin:
  214. mNative.setItemHorizontalMargin(Utils.rp2px(value));
  215. break;
  216. case StringBase.STR_ID_itemVerticalMargin:
  217. mNative.setItemVerticalMargin(Utils.rp2px(value));
  218. break;
  219. default:
  220. ret = super.setRPAttribute(key, value);
  221. break;
  222. }
  223. return ret;
  224. }
  225. @Override
  226. protected boolean setRPAttribute(int key, int value) {
  227. boolean ret = true;
  228. switch (key) {
  229. case StringBase.STR_ID_itemHeight:
  230. mNative.setItemHeight(Utils.rp2px(value));
  231. break;
  232. case StringBase.STR_ID_itemHorizontalMargin:
  233. mNative.setItemHorizontalMargin(Utils.rp2px(value));
  234. break;
  235. case StringBase.STR_ID_itemVerticalMargin:
  236. mNative.setItemVerticalMargin(Utils.rp2px(value));
  237. break;
  238. default:
  239. ret = super.setRPAttribute(key, value);
  240. break;
  241. }
  242. return ret;
  243. }
  244. public static class Builder implements IBuilder {
  245. @Override
  246. public ViewBase build(VafContext context, ViewCache viewCache) {
  247. return new Grid(context, viewCache);
  248. }
  249. }
  250. }