Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically Add Fragment with LayoutParams

Is it possible to add dynamically add Fragments with LayoutParams? I have 2 fragments that I want to put in a RelativeLayout: one should lock to the left. The other should be locked to the right.

like image 486
user123321 Avatar asked Jan 16 '23 14:01

user123321


1 Answers

I used the following approach for a ListFragment in a LinearLayout:

class CustomListFragment
    extends ListFragment
{
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState)
    {
        ViewGroup v = (ViewGroup) super.onCreateView(inflater, container,
                savedInstanceState);
        v.setLayoutParams(new LinearLayout.LayoutParams(0,
                LinearLayout.LayoutParams.MATCH_PARENT, 1));
        return v;
    }
}

Now that I read your comments on the other answer, I see it is similar to what you considered.

But note: the framework may instantiate your fragments. Therefore, the class must be public and must have a constructor without any further arguments.

Any setter-methods to customize your fragments won't work therefore, as they won't be called for any framework-created instances.

like image 77
sstn Avatar answered Jan 31 '23 00:01

sstn