How to add fragments programmatically in relativelayout

北慕城南 提交于 2019-12-10 12:47:31

问题


I have designed a RelativeLayout. I am trying to add fragment to it. It overlaps. What do I need to do so that it comes one below other.

    FragmentTransaction t = getSupportFragmentManager().beginTransaction();

    FragmentTest myFragment = new FragmentTest();
    t.add(layout.getId(), myFragment, "myFirstFragment");

    FragmentTest myFragment2 = new FragmentTest();
    t.add(layout.getId(), myFragment2, "mySecondFragment");

    t.commit();

//Views of myFragment and myFragment2 overlaps even though I have set the orientation to vertical.

<RelativeLayout
    android:id="@+id/idll1"
    android:layout_width="fill_parent"
    android:layout_height="50dp"
    android:layout_marginLeft="10sp"
    android:layout_marginRight="0sp"
    android:layout_marginTop="10sp"
    android:layout_weight="1"
    android:paddingBottom="5dp"
    android:paddingLeft="10dp"
    android:paddingRight="5dp"
    android:paddingTop="5dp" 
    android:orientation="vertical"/>

回答1:


We can do it as follows :

Activity class in which relative layout is present. Let that relative layout (mParentLayout) be root element of activity's view hierarchy. And the fragment to be added to the relative layout will be mGridFragment at a position below the Button (mGridBtn).

public class DroidOpsActivity extends FragmentActivity implements
    OnClickListener {

    private RelativeLayout mParentLayout = null;
    private Button mGridBtn = null;
    private FragmentManager mFragmentMgr = null;
    private FragmentTransaction mFragmentTransc = null;
    private Fragment mGridFragment = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initialize();
    }

    private void initialize() {
        setContentView(R.layout.activity_droid_ops);
        mParentLayout = (RelativeLayout) findViewById(R.id.parent_layout);
        mGridBtn = (Button) findViewById(R.id.grid_button);
        mFragmentMgr = getSupportFragmentManager();
        registerListener();
    }

    private void registerListener() {
        mGridBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.grid_button:
                    loadFragment();
                    mGridBtn.setEnabled(false);
        break;
        }
    }

    private void loadFragment() {
        mGridFragment = new DroidOpsFragments();
        mFragmentTransc = mFragmentMgr.beginTransaction();
        mFragmentTransc.add(mParentLayout.getId(), mGridFragment);
        mFragmentTransc.addToBackStack(null);
        mFragmentTransc.commit();
    }

    public RelativeLayout.LayoutParams fetchLayoutParams() {
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                    LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);

        // We can add any rule available for RelativeLayout and hence can position accordingly
        params.addRule(RelativeLayout.BELOW, mGridBtn.getId());
        return params;
    }
}

Corresponding Fragment class which is to be positioned with in the activity's layout.

public class DroidOpsFragments extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                    Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.view_word_grid, null);

        // Here we are fetching the layoutParams from parent activity and setting it to the fragment's view.

        view.setLayoutParams(((DroidOpsActivity) activity).fetchLayoutParams());
        return view;
    }

}



回答2:


You have to set layout prarams in your Fragment's views.

Are they XML or hardCode?

If they are hardcode, you will have to get the FrameLayout Wrapper that Google puts on all fragment's views using the backwards compatible support library and set its relativelayout params either in your view's onlayout or onMeasure.

Otherwise, if they are XML, just set the relative rules.




回答3:


I think it should be LinearLayout




回答4:


Quick and Dirty seems to be a bit better. If you know your layout already you can save some steps, and I know a giant if (X instanceof Class) set of blocks is frowned on, it is pretty versatile.

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        ViewGroup.LayoutParams p = view.getLayoutParams();
        if (p instanceof CoordinatorLayout.LayoutParams) {
            CoordinatorLayout.LayoutParams params = ((CoordinatorLayout.LayoutParams)p);
            params.gravity = Gravity.END | Gravity.BOTTOM;
            view.setLayoutParams(params);
        }
        else if (p instanceof RelativeLayout.LayoutParams) {
            RelativeLayout.LayoutParams params = ((RelativeLayout.LayoutParams)p);
            params.addRule(RelativeLayout.ALIGN_BOTTOM);
            view.setLayoutParams(params);
        }
}


来源:https://stackoverflow.com/questions/13196682/how-to-add-fragments-programmatically-in-relativelayout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!