Android. Is there way to invert order of adding Fragments to LinearLayout?

56 Views Asked by At

I have a fragment containing LinearLayout, where programmaticaly added fragments using:

getFragmentManager().beginTransaction().add(R.id.linear_layout, CheckTemplateFragment.newInstance(*params*).commit();

And it filling linear layout from Top to Bottom, but I need it in reverse, Bottom to Top, aka adding new Fragment to first position, on top if linear layout.

I know about .addView(View, index), and with index=0 it will work, BUT it works with Views only, not Fragment.newInstance() I also tried to change in layout xml:

android:gravity="bottom"

doesn't do trick I also heared that 'wraping' fragment with View can do trick using .addView instead of .add. But is there more clean way to do this?

Here xml of fragments that are being added:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/check_template"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:animateLayoutChanges="true"
    tools:context=".CheckTemplateFragment">

    <TextView
        android:id="@+id/btn_expand"
        android:layout_width="40dp"

        android:layout_height="40dp"
        android:layout_alignParentEnd="true"
        android:foreground="@drawable/button_expand" />

    <TextView
        android:id="@+id/check_date"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_alignParentStart="true"
        android:layout_toStartOf="@+id/btn_expand" />

    <WebView
        android:id="@+id/check_extra"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/btn_expand"
        android:background="@color/teal_200"/>
</RelativeLayout>

I'm using fragment cause i need handle button onClick:

btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                isExpanded = !isExpanded;
                if(isExpanded){
                    extra.setVisibility(View.VISIBLE);
                    btn.startAnimation(anim_rot_cw);
                }
                else {
                    extra.setVisibility(View.GONE);
                    btn.startAnimation(anim_rot_ccw);
                }
            }
        });
0

There are 0 best solutions below