private void initView(Context context) { mActivity = (DynamicAddViewActivity) context; mRedColorRes = mActivity.getResources().getColor(R.color.red); mGrayColorRes = mActivity.getResources().getColor(R.color.darker_gray); mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); mInflater.inflate(R.layout.dynamic_add_view_v_main, this); mDynamicContainer = (LinearLayout) findViewById(R.id.dynamic_addView_layout_container); mBtnAddView = findViewById(R.id.dynamic_addView_btn_add); mBtnAddView.setOnClickListener(mClickListener); addItemView(); }
@Override public void onClick(View view) { switch (view.getId()) { case R.id.dynamic_addView_btn_add: if (mItemViewIndex == 1) { // 在此处获取ItemView的高度 mItemViewHeight = mDynamicContainer.getMeasuredHeight(); Log.i(TAG, "mItemViewHeight = " + mItemViewHeight); } addItemView(); mDynamicContainerHeight = mDynamicContainer.getMeasuredHeight(); Log.i(TAG, "mDynamicContainerHeight = " + mDynamicContainerHeight); mActivity.setScrollViewScroll(mDynamicContainerHeight); break; case R.id.dynamic_addView_delete_view: view = (View) view.getTag(); Animation animation = AnimationUtils.loadAnimation(mActivity, R.anim.dynamic_delete_item_view); view.startAnimation(animation); Message msg = mHandler.obtainMessage(); msg.what = Constant.HANDLER_DYNAMIC_DELETE_ITEM_VIEW; msg.obj = view; mHandler.sendMessageDelayed(msg, 600); break; case R.id.begin_time_layout: case R.id.end_time_layout: showDateTimeDialog(view); break; default: break; } }
private void setItemViewTitleIndex(int itemViewTotal) { for (int index = 0; index < itemViewTotal; index++) { View itemView = mDynamicContainer.getChildAt(index); TextView textTitleIndex = (TextView) itemView.findViewById(R.id.dynamic_addView_title_index); textTitleIndex.setText(mActivity.getString(R.string.table_item_index, (index + 1))); } }
@Override public void handleMessage(Message msg) { super.handleMessage(msg); switch (msg.what) { case Constant.HANDLER_CALC_TIEM_INTERVAL: mTextTimeLen.setText(mTimeIntervalHours + mActivity.getString(R.string.hours)); mTextTimeLen.setVisibility(View.VISIBLE); mLayoutCalcTimeWait.setVisibility(View.GONE); break; case Constant.HANDLER_DYNAMIC_DELETE_ITEM_VIEW: deleteItemView((View) msg.obj); break; default: break; } }
private void addItemView() { /* * 在addItemView()方法中调用日志输出方法:Log.i(String, String, * Throwable),可以在让程序在日志控制台中以栈的形式列出程序是怎么执行到该方法 */ Log.i(TAG, "How Go To Here", new Exception("Stack Way")); View itemView = mInflater.inflate(R.layout.dynamic_add_view_v_item, null); itemView.setId(mItemViewIndex); TextView textTitleIndex = (TextView) itemView.findViewById(R.id.dynamic_addView_title_index); textTitleIndex.setText(mActivity.getString(R.string.table_item_index, (mItemViewIndex + 1))); TextView btnDeleteView = (TextView) itemView.findViewById(R.id.dynamic_addView_delete_view); btnDeleteView.setTag(itemView); btnDeleteView.setOnClickListener(mClickListener); if (mItemViewIndex == 0) { // 第一个条目不显示删除按钮 btnDeleteView.setVisibility(View.GONE); } LinearLayout beginTimeLayout = (LinearLayout) itemView.findViewById(R.id.begin_time_layout); beginTimeLayout.setOnClickListener(mClickListener); LinearLayout endTimeLayout = (LinearLayout) itemView.findViewById(R.id.end_time_layout); endTimeLayout.setOnClickListener(mClickListener); // 开始时间和结束时间相互设置Tag用于当初始化其中任何一个时,另一个跟着变化 beginTimeLayout.setTag(endTimeLayout); endTimeLayout.setTag(beginTimeLayout); mDynamicContainer.addView( itemView, mItemViewIndex++, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); // 第一次添加ItemView后,若马上在此处获取ItemView的高度,则mItemViewHeight为零 // mItemViewHeight = mDynamicContainer.getMeasuredHeight(); // Log.i(TAG, "mItemViewHeight = " + mItemViewHeight); }