0

Hey I will shortly describe what I do in the code below: I create an ViewStub (Vs) and add it to the RelativeLayout (Rl). Then I want to add a new Vs to the same RL, so I create the stub2 and add it to rl. Then I reference to the Rl (which is inflatet) of the first and of the second Vs (rlInf1 ,rlInf2) And then I set the Rl.Layoutparams so that the rlInf2 should be below the rlInf1 but they are overlapping. How can I set the LayoutParams so that the new ViewStub is below the old one? Any Ideas?

(I did not add the ViewStub in XML, but in Java-Code)

//Löschen des Standard-Tv
            TextView tv = (TextView) findViewById(R.id.tvNoContentInfo);
            RelativeLayout rl = (RelativeLayout) findViewById(R.id.Layout_ContentInfo);
            rl.removeView(tv);
            //Erstellen eines ViewStubs
            ViewStub stub1 = new ViewStub(this);
            //zum richtigen Layout hinzufügen
            rl.addView(stub1);
            stub1.setLayoutResource(R.layout.routen_navi_infocontent);
            //Referenz zum eingefügten Layout herstellen
            View inflated = stub1.inflate();
            //Zugriff auf Inhalt des zufor referenzierten Layouts
            TextView myTextView = (TextView) inflated.findViewById(R.id.tvHeaderInfocontent);
            myTextView.setText("hey");
            stub1.setId(View.generateViewId());
            stub1.setInflatedId(stub1.getId());
            RelativeLayout rlInf1 = (RelativeLayout) inflated.findViewById(R.id.Layout_infocontent);

            ViewStub stub2 = new ViewStub(this);
            //zum richtigen Layout hinzufügen
            rl.addView(stub2);
            stub2.setLayoutResource(R.layout.routen_navi_infocontent);
            //Referenz zum eingefügten Layout herstellen
            View inflated2 = stub2.inflate();
            RelativeLayout rlInf2 = (RelativeLayout) inflated2.findViewById(R.id.Layout_infocontent);
            RelativeLayout.LayoutParams p= new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT);
            p.addRule(RelativeLayout.BELOW,rlInf1.getId());
            rlInf2.setLayoutParams(p);
Goetti
  • 3
  • 5
  • what do you need those `ViewStub`s for, if you are inflating them right away? – pskink Aug 11 '16 at 12:20
  • I just test it...I want a dynamic side -> Maybe I need the first time 2Vs and at the second time I need it 3 times... And before I am getting started with the real programm I want to test if I can position a Vs below another @pskink – Goetti Aug 11 '16 at 12:33
  • BTW `LinearLayout`s are not good enough? or why not using `RecyclerView` instead? – pskink Aug 11 '16 at 12:34
  • What is the RecyclerView? Maybe there is another possibility to do what I want...I have an Rl which I have to "include" on runtime and sometimes I have to include the same Rl two or more times below the previous one – Goetti Aug 11 '16 at 12:38
  • So first read about ListView, then about RecyclerView – pskink Aug 11 '16 at 12:48
  • can i add a Layout without ViewStubs? @pskink – Goetti Aug 11 '16 at 12:49
  • you can add any `View` without `ViewStub`, just call `ViewGroup#addView`, but really read about the list containers like `ListViw` and / or `RecyclerView` – pskink Aug 11 '16 at 13:22
  • thank you, I read about ListView and found sth that helped @pskink – Goetti Aug 11 '16 at 13:40

1 Answers1

1

Once you inflate the view into the ViewStub, the ViewStub is gone, so it is useless to change the id on it. You need to change it on the inflated view. So change

stub1.setId(View.generateViewId());

to

inflated.setId(View.generateViewId());

And change the last two lines to:

    p.addRule(RelativeLayout.BELOW,inflated.getId());
    inflated2.setLayoutParams(p);

Also, you might want to grab the existing LayoutParams from the inflated view and not create new ones, otherwise you will end up overwriting all the existing ones.

BTW: I have to assume that the addition of the ViewStubs and immediate inflation is just a test for something and not the real flow, right? :-)

ADDITION: Yes, you can add layouts without using ViewStub

    View inflated = LayoutInflater.from(this).inflate(R.layout.routen_navi_infocontent, rl, false);
    inflated.setId(View.generateViewId());
    rl.addView(inflated);

    View inflated2 = LayoutInflater.from(this).inflate(R.layout.routen_navi_infocontent, rl, false);
    RelativeLayout.LayoutParams p= new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT);
    p.addRule(RelativeLayout.BELOW,inflated.getId());
    inflated2.setLayoutParams(p);
    rl.addView(inflated2);

    TextView myTextView = (TextView) inflated.findViewById(R.id.tvHeaderInfocontent);
    myTextView.setText("hey");
N.T.
  • 2,601
  • 1
  • 14
  • 20
  • thank you, I found another whay to do it (with lists) but my problem is that the list only shows the first Item and not the second...so if I am not able to do it with lists i will test your code – Goetti Aug 11 '16 at 14:05
  • Ohhh it was just a fail:D It shows both elements but I have to scroll...I thought that the relative layout will expand if the list is too long...@N.T. – Goetti Aug 11 '16 at 14:08