如何在MainActivity的框架布局中加载默认片段?

时间:2017-03-17 10:47:57

标签: android android-fragments

我在mainActivity中有 2片段 frameLayout 。 每当我运行我的应用程序时,我希望 Fragment1 加载到我的frameLayout 默认中,该文件位于mainActivity中。

activity_main.xml中

<?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/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.prank.fragmentbackstack.MainActivity">

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/frame"
    >

</FrameLayout>
</RelativeLayout>

Frag.class

import android.widget.Button;
    import android.widget.TextView;
    public class Frag extends Fragment {
        String Tag1="frag1";

        Button b;

        public View onCreateView(LayoutInflater inflater, ViewGroup viewGroup, Bundle saveInstanceState)
        {View view = inflater.inflate(R.layout.fragment, viewGroup, false);
            b=(Button)view.findViewById(R.id.button);

            return view;
        }
    }

7 个答案:

答案 0 :(得分:3)

使用此代码动态添加片段

// Create new fragment and transaction
Fragment newFragment = new ExampleFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();

// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack if needed
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);

// Commit the transaction
transaction.commit();

答案 1 :(得分:1)

试试这个:

undefined

答案 2 :(得分:0)

设置:

 viewPager.setCurrentItem(index);
要显示的index = index

fragment

答案 3 :(得分:0)

使用此代码只需将frameLayout容器替换为onCreate:

中的Fragment1
getFragmentManager().beginTransaction()
                .replace(R.id. frameLayout, new Fragment1())
                .commit();

答案 4 :(得分:0)

在主要活动中使用此功能:

frag_name frag_name = new frag_name();
                    FragmentManager manager = this.getSupportFragmentManager();
                    manager.beginTransaction().replace(R.id.youtframelayout, frag_name, frag_name.getTag()).commit();

它会fragment使用FrameLayout并将其替换为{{1}}

希望它有效! :)

答案 5 :(得分:0)

使用sigleton设计模式解决。

fragment内:

public static HomeFragment newInstance() {

    Bundle args = new Bundle();

    HomeFragment fragment = new HomeFragment();
    fragment.setArguments(args);
    return fragment;
}

fragmentactivity方法中添加onCreate

HomeFragment homeFragment = HomeFragment.newInstance();
    getSupportFragmentManager()
            .beginTransaction()
            .replace(R.id.yourFrameLayoutId, homeFragment)
            .commit();

答案 6 :(得分:0)

制作pushfragment类,并在需要添加或更新时使用。

pushFragments(R.id.frame, this, YourFragment, false, false, YourFragment.class.getSimpleName());

public static void pushFragments(int container, AppCompatActivity activity, Fragment fragment, boolean animate, boolean shouldAdd, String tag) {
        FragmentManager manager = activity.getSupportFragmentManager();
        FragmentTransaction ft = manager.beginTransaction();
        if (animate) { //For animation
            ft.setCustomAnimations(R.anim.enter, R.anim.exit, R.anim.pop_enter, R.anim.pop_exit);
        }

        if (shouldAdd) { //for add in stack
            ft.replace(container, fragment, tag);
            ft.addToBackStack(tag);
            ft.commitAllowingStateLoss();
        } else {
            manager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
            ft.replace(container, fragment, tag);
            ft.commit();
        }
    }

将Frag传递给pushFragments并在onCreate of Mainactivity中添加call。