2016-09-21 64 views
0

我在從相對視圖中刪除視圖時遇到問題。下面的代碼以編程方式添加電話字段,然後在下面添加開關。當添加字段時,這一切都很好,但是我無法找到輕鬆刪除它們的方法,即有三個字段,我想刪除下面的第二個或第三個字段,而不是將它們錨定到的視圖。如何刪除相對對齊的視圖並保持其他視圖不變?

enter image description here

代碼:

private int lastCreatedView; 
    private int tokensIteration; 
    private int addButtonId, switch1Id, switch2Id; 
    private ArrayList<Integer> editTextsIds = new ArrayList<>(); 
    private Contact contact; 

    private void setPhoneField(String number){ 
     EditText editText = new EditText(new ContextThemeWrapper(this, R.style.ContactDetailsEditText)); 
     editText.setId(View.generateViewId()); 
     editText.setText(number); 
     editText.setHint(R.string.contact_details_activity_hint_phone_number); 
     editText.setTextColor(getResources().getColor(android.R.color.white)); 
     editText.setEms(10); 
     editText.setInputType(InputType.TYPE_CLASS_PHONE); 
     editText.setImeOptions(tokenizer != null && tokenizer.hasMoreTokens() 
       ? EditorInfo.IME_ACTION_NEXT : EditorInfo.IME_ACTION_DONE); 
     LinearLayout.LayoutParams editTextParams = new LinearLayout.LayoutParams(
       LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); 
     editText.setLayoutParams(editTextParams); 

     editTextsIds.add(editText.getId()); 

     TextInputLayout til = new TextInputLayout(new ContextThemeWrapper(this, R.style.ContactDetailsEditText)); 
     til.setId(View.generateViewId()); 
     RelativeLayout.LayoutParams textInputLayoutParams = new RelativeLayout.LayoutParams(
       RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
     int viewToAttach = lastCreatedView == 0 ? R.id.input_layout_company : lastCreatedView; 
     textInputLayoutParams.addRule(RelativeLayout.BELOW, viewToAttach); 
     textInputLayoutParams.addRule(RelativeLayout.END_OF, R.id.contact_photo); 
     til.setLayoutParams(textInputLayoutParams); 
     til.setTag(editTextsIds.size()); 

     lastCreatedView = til.getId(); 

     ImageView phoneButton = new ImageView(this); 
     phoneButton.setImageResource(R.drawable.ic_phone_black_48dp); 
     phoneButton.setPadding(5,5,5,5); 
     phoneButton.setScaleType(ImageView.ScaleType.FIT_END); 
     phoneButton.setColorFilter(Color.argb(255, 255, 255, 255)); 
     RelativeLayout.LayoutParams phoneButtonParams = new RelativeLayout.LayoutParams(
       RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
     phoneButtonParams.addRule(RelativeLayout.START_OF, til.getId()); 
     phoneButtonParams.addRule(RelativeLayout.ALIGN_TOP, til.getId()); 
     phoneButtonParams.addRule(RelativeLayout.ALIGN_BOTTOM, til.getId()); 
     phoneButton.setLayoutParams(phoneButtonParams); 
     phoneButton.setTag(editTextsIds.size()); 
     phoneButton.setOnClickListener(view -> { 
      Intent call = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + number)); 
      startActivity(call); 
     }); 

     til.addView(editText); 

     contactContainer.addView(til); 
     contactContainer.addView(phoneButton); 

     ImageView removeNumber = new ImageView(this); 
     removeNumber.setId(View.generateViewId()); 
     removeNumber.setVisibility(tokensIteration > 0 ? View.VISIBLE : View.GONE); 
     removeNumber.setImageResource(R.drawable.minus); 
     RelativeLayout.LayoutParams removeNumberParams = new RelativeLayout.LayoutParams(
       RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
     removeNumberParams.addRule(RelativeLayout.END_OF, til.getId()); 
     removeNumberParams.addRule(RelativeLayout.ALIGN_TOP, til.getId()); 
     removeNumberParams.addRule(RelativeLayout.ALIGN_BOTTOM, til.getId()); 
     removeNumber.setLayoutParams(removeNumberParams); 
     removeNumber.setTag(editTextsIds.size()); 
     removeNumber.setOnClickListener(view -> { 

      for(View v : getViewsByTag(contactContainer, view.getTag())) 
       contactContainer.removeView(v); 

     }); 

     if(tokensIteration > 0) contactContainer.removeView(findViewById(addButtonId)); 
     ImageView addNumber = new ImageView(this); 
     addNumber.setId(View.generateViewId()); 
     addNumber.setImageResource(R.drawable.plus); 
     addNumber.setPadding(tokensIteration > 0 ? 5 : 0, 0, 0, 0); 
     RelativeLayout.LayoutParams addNumberParams = new RelativeLayout.LayoutParams(
       RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
     addNumberParams.addRule(RelativeLayout.END_OF, removeNumber.getId()); 
     addNumberParams.addRule(RelativeLayout.ALIGN_TOP, removeNumber.getId()); 
     addNumberParams.addRule(RelativeLayout.ALIGN_BOTTOM, removeNumber.getId()); 
     addNumber.setLayoutParams(addNumberParams); 
     addNumber.setTag(editTextsIds.size()); 
     addNumber.setOnClickListener(view -> { 
      //Toast.makeText(this, "Function not available", Toast.LENGTH_SHORT).show(); 

       setPhoneField(""); 
       setSwitches(); 
      } 
     ); 

     addButtonId = addNumber.getId(); 

     contactContainer.addView(removeNumber); 
     contactContainer.addView(addNumber); 

     setSwitches(); 
    } 

    void setSwitches(){ 

     RelativeLayout.LayoutParams recordCallsParams = new RelativeLayout.LayoutParams(
       RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
     recordCallsParams.addRule(RelativeLayout.BELOW, lastCreatedView); 
     recordCallsParams.addRule(RelativeLayout.ALIGN_START, lastCreatedView); 
     recordCallsParams.addRule(RelativeLayout.ALIGN_END, lastCreatedView); 

     if(switch1Id != 0 && switch2Id != 0){ 
      contactContainer.updateViewLayout(findViewById(switch1Id), recordCallsParams); 
      return; 
     } 

     Switch recordCalls = new Switch(this); 
     recordCalls.setText(R.string.contact_details_activity_record_calls); 
     recordCalls.setTextColor(getResources().getColor(R.color.white)); 
     recordCalls.setSwitchPadding(5); 
     recordCalls.setId(View.generateViewId()); 
     recordCalls.setLayoutParams(recordCallsParams); 
     recordCalls.setChecked(contact != null && contact.getIsRecordCalls()); 
     switch1Id = recordCalls.getId(); 

     Switch switchPrivate = new Switch(this); 
     switchPrivate.setText(R.string.contact_details_activity_private_contact); 
     switchPrivate.setTextColor(getResources().getColor(R.color.white)); 
     switchPrivate.setSwitchPadding(5); 
     switchPrivate.setId(View.generateViewId()); 
     RelativeLayout.LayoutParams switchPrivateParams = new RelativeLayout.LayoutParams(
       RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
     switchPrivateParams.addRule(RelativeLayout.BELOW, recordCalls.getId()); 
     switchPrivateParams.addRule(RelativeLayout.ALIGN_START, recordCalls.getId()); 
     switchPrivateParams.addRule(RelativeLayout.ALIGN_END, recordCalls.getId()); 
     switchPrivate.setLayoutParams(switchPrivateParams); 
     switchPrivate.setChecked(contact != null && contact.getIsPrivateContact()); 
     switch2Id = switchPrivate.getId(); 

     contactContainer.addView(recordCalls); 
     contactContainer.addView(switchPrivate); 
    } 

    private static ArrayList<View> getViewsByTag(ViewGroup root, Object tag){ 
     ArrayList<View> views = new ArrayList<>(); 
     final int childCount = root.getChildCount(); 
     for (int i = 0; i < childCount; i++) { 
      final View child = root.getChildAt(i); 
      if (child instanceof ViewGroup) { 
       views.addAll(getViewsByTag((ViewGroup) child, tag)); 
      } 

      final Object tagObj = child.getTag(); 
      if (tagObj != null && tagObj.equals(tag)) { 
       views.add(child); 
      } 
     } 
     return views; 
+1

我同意下面的donfuxx的回覆,但如果不是選項爲你也許你可以組織在一個列表視圖中的字段。這樣它可以更加動態。 –

+0

請您詳細說明一下嗎?你的意思是整個'phoneField'作爲放入列表中的對象/視圖? –

+0

您可以在ListView中使用包含edittext的佈局來創建佈局。因此,ListView的每一行都將是一個EditText。無論何時需要添加/刪除字段,都可以更新ListView。 –

回答

1

你真的需要徹底刪除的看法?如果你只想隱藏部分視圖,而不與佈局搞亂你可以將其設置爲INVISIBLE

phoneButton.setVisibility(View.INVISIBLE); 

BTW:我建議你使用佈局資源文件。特別是當您的佈局變得更加複雜,並且您的想法使您的應用與多種屏幕尺寸,屏幕方向等兼容時,處理起來要容易得多。

+0

我很想做一個手機領域的佈局資源視圖。目前,我只是不知道如何正確實施它,特別是在對齊方面 –