Use view to inflate multiple times

前端 未结 4 1009
Happy的楠姐
Happy的楠姐 2021-01-11 19:57

I have a some problem regarding inflating and re-using the same TextView.
Its like its trying to overwrite the same textview over and over again or something and it can

4条回答
  •  悲哀的现实
    2021-01-11 20:11

    In my case, setTag(i) didn't work, what worked was changing the id after inflating the layout but before adding it to the parent view like this:

    LayoutInflater inflater = LayoutInflater.from(this);
    View mainlayout = inflater.inflate(R.layout.days_monday_inflate, null);
    View layout_number = inflater.inflate(R.layout.inflate_number, null);
    
    for (int i = 0; i < 10; i++) {
        row = new TableRow(this);
        number = (TextView) layout_number.findViewById(R.id.Number);
        number.setId(i); //THIS IS THE KEY STEP
        number.setText(Integer.toString(i));
        row.addView(number);
    }
    setContentView(mainlayout);
    

提交回复
热议问题