How to add same view to parent multiple times by inflating it only once

后端 未结 4 1005
小蘑菇
小蘑菇 2020-12-10 10:30

I have a LinearLayout with vertical orientation as parent, I want to add some view programmatically multiple times to this parent. Right now I am inflating the child every t

4条回答
  •  無奈伤痛
    2020-12-10 10:52

    I'm not sure what your view is but have you creating it manually over inflating the XML:

       ArrayList myList = getData();
        for(String data : myList) {
    
            LinearLayout layout = new LinearLayout(this);
            layout.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,             LayoutParams.WRAP_CONTENT));
            TextView textView = new TextView(this);
            textView.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
    
            textView.setText(data);
    
            layout.addChild(textView);         
    
            parentPanel.addView(layout);
        }
    

    But yeah your clearly attempting something that has been done for you with Simple ListView & API

提交回复
热议问题