2017-01-14 34 views
0

問題:單擊按鈕時,我想在主佈局下添加具有唯一ID的新LinearLayout,以便稍後可以引用子佈局佈局。 這是我的活動課。點擊添加按鈕,新的線性佈局應該創建並添加到主線性佈局。每個新佈局都應該有唯一的ID,因此,這個新ID將在稍後提及。在andriod中創建多個具有唯一ID的新的線性佈局,其中包含唯一的ID:

public class CreateEventFragment extends Fragment{ 
    private ListView listView; 
    private EditText createEventEtEventName,createEventEtQuestion; 
    private AboutUsAdapter listAdapter; 
    private List<MyPojoObj> obj; 
    private String jsonAboutus; 
    private ProgressBar createEventPb; 
    private RadioGroup createEventRadioGroupEventType; 
    private RadioButton createEventRadioType1,createEventRadioType2; 
    private LinearLayout createEventLL; 
    private Button createEventBtnAdd,createEventBtnSubmit; 
    private int id=0; 



    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 

     if(Variable.sop)System.out.println("CreateEventFragment.onCreateView()"); 
     View rootView = inflater.inflate(R.layout.fragment_create_event, container,false); 
     obj=new ArrayList<MyPojoObj>(); 
     createEventBtnAdd = (Button) rootView.findViewById(R.id.createEventBtnAdd); 
     createEventBtnSubmit = (Button) rootView.findViewById(R.id.createEventBtnSubmit); 
     createEventEtEventName=(EditText) rootView.findViewById(R.id.createEventEtEventName); 
     createEventRadioType1=(RadioButton) rootView.findViewById(R.id.createEventRadioType1); 
     createEventRadioType2=(RadioButton) rootView.findViewById(R.id.createEventRadioType2);*/ 
     createEventLL = (LinearLayout) rootView.findViewById(R.id.createEventLL); 

     createEventEtEventName.setText("EventName"); 
     getMinutes()); 
     createEventBtnAdd.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View arg0) { 
       actionAddNewEvent(); 
      } 
     }); 

     return rootView; 
    } 

    @SuppressWarnings("deprecation") 
    void actionAddNewEvent(){ 

     id=id+1; 

     int left=5; 
     int top=1; 
     int right=1; 
     int bottom=0; 

     int leftMargin=5; 
     int topMargin=5; 
     int rightMargin=5; 
     int bottomMargin=5; 

     int childId=0; 

     LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams 
     (LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); 
     final LinearLayout ll = new LinearLayout(getActivity()); 
     ll.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)); 
     ll.setOrientation(LinearLayout.VERTICAL); 
     ll.setBackgroundColor(getActivity().getResources().getColor(R.color.feed_bg)); 
     ll.setLayoutParams(lp); 
     ll.setId(id); 
     ll.setTag(id);  
     createEventLL.addView(ll); 
     Drawable img = getActivity().getResources().getDrawable(R.drawable.questions); 
     img.setBounds(0, 0, 55, 55); 

     //question edit text 
     EditText createEventEtQuestion = new EditText(getActivity()); 
     createEventEtQuestion.setLayoutParams(lp); 
     createEventEtQuestion.setPadding(left, top, right, bottom); 
     createEventEtQuestion.setFocusable(true); 
     createEventEtQuestion.setCompoundDrawables(img, null, null, null); 
     createEventEtQuestion.setBackground(getActivity().getResources().getDrawable(R.drawable.border_1)); 
     createEventEtQuestion.setBackgroundColor(getActivity().getResources().getColor(R.color.feed_item_bg)); 

     RadioGroup rg = new RadioGroup (getActivity()); 
     rg.setId(500+id); 

     RadioButton radioButton1 = new RadioButton(getActivity()); 
     radioButton1.setText("True/False"); 
     radioButton1.setId(100000+id);//set radiobutton id and store it somewhere 
     radioButton1.setTextSize(12); 
     RadioGroup.LayoutParams params = new RadioGroup.LayoutParams(RadioGroup.LayoutParams.MATCH_PARENT, RadioGroup.LayoutParams.WRAP_CONTENT); 

     RadioButton radioButton2 = new RadioButton(getActivity()); 
     radioButton2.setText("Yes/No"); 
     radioButton2.setTextSize(12); 
     radioButton2.setId(100001+id);//set radiobutton id and store it somewhere 

     rg.addView(radioButton1, params); 
     rg.addView(radioButton2, params); 

     // Add Type Title 
     TextView tv = new TextView(getActivity()); 
     //tv.setLayoutParams(lp); 
     tv.setText("Answer Type"); 
     tv.setLayoutParams(lp); 
     tv.setTextSize(12); 
     tv.setPadding(left, top, right, bottom); 

     LinearLayout.LayoutParams rmlp = new LinearLayout.LayoutParams 
     (200, 50); 

     Button removeBtn = new Button(getActivity()); 
     //tv.setLayoutParams(lp); 
     removeBtn.setText("Delete Question:"+id); 
     removeBtn.setPadding(left, top, right, bottom); 
     removeBtn.setLayoutParams(rmlp); 
     //removeBtn.setId(id); 
     removeBtn.setTextSize(9); 
     removeBtn.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View arg0) { 
       actionRemoveQuestion(arg0); 
      } 
     }); 

     // draw line 
     View v = new View(getActivity()); 
     v.setLayoutParams(new LinearLayout.LayoutParams(
     LayoutParams.MATCH_PARENT, 
     2 
    )); 
     v.setBackgroundColor(Color.parseColor("#000000")); 


     ll.addView(createEventEtQuestion); 
     ll.addView(tv); 
     ll.addView(rg); 
     ll.addView(removeBtn); 
     ll.addView(v); 

    } 

    void actionRemoveQuestion(View view){ 
     //Toast.makeText(getActivity(), view.getId()+" | "+view.getParent(), Toast.LENGTH_LONG).show(); 
     //View namebar = view.findViewById(view.getId()); 
     //ViewGroup parent = (ViewGroup) namebar.getParent(); 

     if (true) { 
      final LinearLayout child = (LinearLayout) createEventLL.findViewById(id); 
      Toast.makeText(getActivity(), "cCount:"+child.getChildCount()+" | cID:"+child.getId()+" |" + 
      " pCount:"+createEventLL.getChildCount()+" | cTAG:"+child.getTag()+" | pID:"+createEventLL.getId(), Toast.LENGTH_LONG).show(); 
     } 
    } 
} 
+0

你不需要一個唯一的ID,你已經有了這些佈局的參考,不是嗎? – pskink

+0

每個佈局保持刪除按鈕。點擊刪除按鈕,我希望該佈局應該被刪除 – sahil

+0

好,然後刪除該佈局(請參閱'ViewGroup'文檔) – pskink

回答

0

您可以使用onClickListener來生成一個新的LinearlyLayout。但是當你創建另一個子佈局來編輯或刪除它時,你會遇到你的問題,你將無法引用它。

更好的方法是使用LinearLayout數組來動態生成LinearLayout,以便您可以引用它們並稍後使用它們。

的代碼可能類似於:

LinearLayout [ ] linearLayout; 

在聲明 而在onClickListener補充:

linearLayout[ ] = new LinearLayout[ ]; 

這可以更好的進行for循環,因爲它增加一個計數器每次的按鈕被點擊。

我認爲這種方法應該可行。

+0

我現在要ListView .. – sahil