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

后端 未结 4 1020
小蘑菇
小蘑菇 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 11:02

    Inflating multiple times cannot be done the same way doing it in single shot. Hope this works

    LayoutInflater inflator = (LayoutInflater).getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    
        LinearLayout parentPanel = findViewById(R.id.parent_pannel);
    
        ArrayList myList = getData();
        for(String data : myList) {
            // inflate child
            View item = inflator.inflate(R.layout.list_item, null);
            // initialize review UI
            TextView dataText = (TextView) item.findViewById(R.id.data);
            // set data
            dataText.setText(data);
            // add child
            parentPanel.addView(item);
        }
    

    This will work, at the least worked me

提交回复
热议问题