1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
| public class StatusLayout extends FrameLayout{ private static final String TAG = "StatusLayout.FrameLayout";
private static final int DEFAULT = 1; private static final int EMPTY = 2; private static final int NET_ERROR = 3;
private String mInitMessage; private Drawable mInitImage; private String mInitStrInBtn;
private Map<Integer, View> mMapMessageViews; private List<View> mNormalViews;
private LayoutInflater mLayoutInflater;
private LinearLayout mDefaultView; private LinearLayout mDefaultEmptyMessageView; private LinearLayout mDefaultNetErrorView;
private Context mContext;
public StatusLayout(Context context){ this(context, null); }
public StatusLayout(Context context, AttributeSet attrs){ this(context, attrs, 0); }
public StatusLayout(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); mContext = context; init(attrs); }
private void init(AttributeSet attrs){ if (null == mNormalViews) mNormalViews = new ArrayList<>();
if (null == mMapMessageViews) mMapMessageViews = new HashMap<>();
if (null == mLayoutInflater){ mLayoutInflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); }
TypedArray mValueArray = mContext.obtainStyledAttributes(attrs, R.styleable.StatusLayoutValue);
mInitMessage = mValueArray.getString(R.styleable.StatusLayoutValue_attr_message); mInitImage = mValueArray.getDrawable(R.styleable.StatusLayoutValue_attr_image_src); mInitStrInBtn = mValueArray.getString(R.styleable.StatusLayoutValue_attr_str_btn);
setEmptyMessageView(); setNetErrorMessageView(); setDefaultView(mInitMessage, mInitImage, mInitStrInBtn); mValueArray.recycle();
}
@Override protected void onFinishInflate(){ super.onFinishInflate(); showDefaultView(); }
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); measureChildren(widthMeasureSpec, heightMeasureSpec); }
@Override public void addView(View child, ViewGroup.LayoutParams params) { super.addView(child, params); for(int i = 0; i < getChildCount(); i ++){ mNormalViews.add(getChildAt(i)); } }
public void showDefaultView(){ showStatusView(DEFAULT); }
public void showEmptyMessageView(){ showStatusView(EMPTY); }
public void showNetErrorView(){ showStatusView(NET_ERROR); }
public void showNormalView(){ hiddenStatusViews(); setContentView(true); }
private void setContentView(boolean isShown){ if (isShown){ for (View v : mNormalViews){ v.setVisibility(VISIBLE); } }else { for (View v : mNormalViews){ v.setVisibility(GONE); } } }
private void setNetErrorMessageView(){ if (null == mDefaultNetErrorView){ mDefaultNetErrorView = (LinearLayout)mLayoutInflater.inflate(R.layout.status_layout_net_error_message, null); } mMapMessageViews.put(NET_ERROR, mDefaultNetErrorView); }
public void setNetErrorMessageView(String message, Drawable image){ if(null == mDefaultNetErrorView){ mDefaultNetErrorView = (LinearLayout)mLayoutInflater.inflate(R.layout.status_layout_net_error_message, null); }else { mDefaultNetErrorView = (LinearLayout)mMapMessageViews.get(NET_ERROR); }
TextView mTvNetError = (TextView)mDefaultNetErrorView.findViewById(R.id.tv_net_error_view); ImageView mIvNetError = (ImageView)mDefaultNetErrorView.findViewById(R.id.iv_net_error_view); if (null != message){ mTvNetError.setText(message); mTvNetError.setVisibility(VISIBLE); }else { mTvNetError.setVisibility(GONE); } if (null != image){ mIvNetError.setImageDrawable(image); mIvNetError.setVisibility(VISIBLE); }else { mIvNetError.setVisibility(GONE); }
mMapMessageViews.put(NET_ERROR, mDefaultNetErrorView); }
private void setEmptyMessageView(){ if (null == mDefaultEmptyMessageView) { mDefaultEmptyMessageView = (LinearLayout)mLayoutInflater.inflate(R.layout.status_layout_empty_message, null); } mMapMessageViews.put(EMPTY, mDefaultEmptyMessageView); }
public void setEmptyMessageView(int messageId, int imageId, int messageInBtnId){ String messageInBtn = mContext.getString(messageInBtnId); setEmptyMessageView(messageId, imageId, messageInBtn); }
public void setEmptyMessageView(int messageId, int imageId, String messageInBtn){ Drawable image = ContextCompat.getDrawable(mContext, imageId); setEmptyMessageView(messageId, image, messageInBtn); }
public void setEmptyMessageView(int messageId, Drawable image, String messageInBtn){ String message = mContext.getString(messageId); setEmptyMessageView(message, image, messageInBtn); }
public void setEmptyMessageView(String message, Drawable image, String messageInBtn){ if(null == mDefaultEmptyMessageView){ mDefaultEmptyMessageView = (LinearLayout)mLayoutInflater.inflate(R.layout.status_layout_empty_message, null); }else { mDefaultEmptyMessageView = (LinearLayout)mMapMessageViews.get(EMPTY); }
TextView mTvEmpty = (TextView)mDefaultEmptyMessageView.findViewById(R.id.tv_empty_view); ImageView mIvEmpty = (ImageView)mDefaultEmptyMessageView.findViewById(R.id.iv_empty_view); Button mBtnEmpty = (Button)mDefaultEmptyMessageView.findViewById(R.id.btn_empty_view); if (null != message){ mTvEmpty.setText(message); mTvEmpty.setVisibility(VISIBLE); }else { mTvEmpty.setVisibility(GONE); } if (null != image){ mIvEmpty.setImageDrawable(image); mIvEmpty.setVisibility(VISIBLE); }else { mIvEmpty.setVisibility(GONE); } if (null != messageInBtn) { mBtnEmpty.setText(message); mBtnEmpty.setVisibility(VISIBLE); }else { mBtnEmpty.setVisibility(GONE); } mMapMessageViews.put(EMPTY, mDefaultEmptyMessageView); }
private void setDefaultView(String message, Drawable image, String messageInBtn){ if(null == mDefaultView){ mDefaultView = (LinearLayout)mLayoutInflater.inflate(R.layout.status_layout_default_message, null); }else { mDefaultView = (LinearLayout)mMapMessageViews.get(DEFAULT); }
TextView mTvDefault = (TextView)mDefaultView.findViewById(R.id.tv_default_view); ImageView mIvDefault = (ImageView)mDefaultView.findViewById(R.id.iv_default_view); Button mBtnDefault = (Button)mDefaultView.findViewById(R.id.btn_default_view); if (null != message){ mTvDefault.setText(message); mTvDefault.setVisibility(VISIBLE); }else { mTvDefault.setVisibility(GONE); } if (null != image){ mIvDefault.setImageDrawable(image); mIvDefault.setVisibility(VISIBLE); }else { mIvDefault.setVisibility(GONE); } if (null != messageInBtn) { mBtnDefault.setText(message); mBtnDefault.setVisibility(VISIBLE); }else { mBtnDefault.setVisibility(GONE); } mMapMessageViews.put(DEFAULT, mDefaultView); }
public void addStatus(int key, View view) throws IllegalNumException { if(1 == key||2 == key||3 == key) { throw new IllegalNumException(); } mMapMessageViews.put(key, view); }
public void showStatusView(int key){ setContentView(false); View mMessageView = mMapMessageViews.get(key); hiddenStatusViews(); addView(mMessageView); mMessageView.setVisibility(VISIBLE); }
private void hiddenStatusViews(){ for (View v : mMapMessageViews.values()){ removeView(v); } }
}
|