Dynamically add fragment using LayoutParams

Is it possible to dynamically add fragments using LayoutParams? I have 2 fragments that I want to add to RelativeLayout: I need to lock to the left. The other should be locked to the right.

+4
source share
4 answers

You can wrap them each in a layout (e.g. LinearLayout ), and then set its size and position. This answer addresses a similar issue and shows how to use FrameLayout with custom layout options to contain Fragment .

Note that when using the Fragment object in XML, you can use the android:layout_* attributes to control your position. For instance:

 <LinearLayout android:orientation="horizontal" ... > <fragment android:layout_height="wrap_content" android:layout_width="0px" android:layout_weight="1" ... /> <fragment android:layout_height="wrap_content" android:layout_width="0px" android:layout_weight="1" ... /> </LinearLayout> 

However, I don’t think that you can programmatically apply the layout options to Fragment s . After all, they are not ViewGroup s.

+2
source

I used the following approach for ListFragment in 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 have read your comments on another answer, I see that it looks like what you thought.

But note: a structure can instantiate your fragments. Therefore, the class must be publicly available and must have a constructor without any additional arguments.

Any tuning methods for setting up your fragments will not work therefore, since they will not be called for any instances created by the frame.

+5
source

In certain cases, you can move the layer ordering to onStart () override

The fragment is as follows

Public class FragmentOne extends fragment {

 private Client client; public interface Client { RelativeLayout.LayoutParams fetchFragmentOneParams(FragmentOne fragment); } @Override public void onAttach(Context context) { super.onAttach(context); Activity activity = getActivity(); try { client = (Client) getActivity(); } catch (ClassCastException e) { throw new ClassCastException(activity.toString() + " must implement FragmentOne.Client"); } } @Override public void onStart() { super.onStart(); getView().setLayoutParams(client.fetchFragmentOneParams(this)); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { RelativeLayout view = new RelativeLayout(getActivity()); // // code // return view; } 
+1
source

You can set the LayoutParameters of your fragment in the onCreateView method. To return the final representation as the return value of this method, you can set the layout parameters. As an example below, the fragment returns a TextView as its view, which is bound to its parent on the left by setting LayoutParams on it:

  public static class DummySectionFragment extends Fragment { public static final String ARG_SECTION_NUMBER = "placeholder_text"; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { TextView textView = new TextView(getActivity()); textView.setGravity(Gravity.CENTER); textView.setText(Integer.toString(getArguments().getInt( ARG_SECTION_NUMBER))); LayoutParams lp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT); textView.setLayoutParams(lp); return textView; } } 
0
source

Source: https://habr.com/ru/post/1433450/


All Articles