如何使用以编程方式创建的内容视图向活动添加片段

如何使用以编程方式创建的内容视图向活动添加片段

我想将一个片段添加到以编程方式实现其布局的Activity。我查看了Fragment文档,但没有很多示例描述我需要的内容。这是我尝试编写的代码类型:

public class DebugExampleTwo extends Activity {

    private ExampleTwoFragment mFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FrameLayout frame = new FrameLayout(this);
        if (savedInstanceState == null) {
            mFragment = new ExampleTwoFragment();
            FragmentTransaction ft = getFragmentManager().beginTransaction();
            ft.add(frame.getId(), mFragment).commit();
        }

        setContentView(frame);
    }}

...

public class ExampleTwoFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, 
                             ViewGroup container, 
                             Bundle savedInstanceState) {
        Button button = new Button(getActivity());
        button.setText("Hello There");
        return button;
    }}

此代码编译但在开始时崩溃,可能是因为我FragmentTransaction.add()的错误。这样做的正确方法是什么?


繁华开满天机
浏览 352回答 3
3回答

繁星点点滴滴

这是我在阅读Tony Wong的评论后想出的:public class DebugExampleTwo extends BaseActivity {     @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         addFragment(android.R.id.content,                     new DebugExampleTwoFragment(),                     DebugExampleTwoFragment.FRAGMENT_TAG);     }}...public abstract class BaseActivity extends Activity {     protected void addFragment(@IdRes int containerViewId,                                @NonNull Fragment fragment,                                @NonNull String fragmentTag) {         getSupportFragmentManager()                 .beginTransaction()                 .add(containerViewId, fragment, fragmentTag)                 .disallowAddToBackStack()                 .commit();     }     protected void replaceFragment(@IdRes int containerViewId,                                    @NonNull Fragment fragment,                                    @NonNull String fragmentTag,                                    @Nullable String backStackStateName) {         getSupportFragmentManager()                 .beginTransaction()                 .replace(containerViewId, fragment, fragmentTag)                 .addToBackStack(backStackStateName)                 .commit();     }}...public class DebugExampleTwoFragment extends Fragment {     public static final String FRAGMENT_TAG =          BuildConfig.APPLICATION_ID + ".DEBUG_EXAMPLE_TWO_FRAGMENT_TAG";     // ...}科特林如果您正在使用Kotlin,请务必查看Google提供的Kotlin扩展程序,或者只编写您自己的扩展程序。

ibeautiful

public&nbsp;class&nbsp;Example1&nbsp;extends&nbsp;FragmentActivity&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@Override &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;protected&nbsp;void&nbsp;onCreate(Bundle&nbsp;savedInstanceState)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;super.onCreate(savedInstanceState); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DemoFragment&nbsp;fragmentDemo&nbsp;=&nbsp;(DemoFragment)&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;getSupportFragmentManager().findFragmentById(R.id.frame_container); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//above&nbsp;part&nbsp;is&nbsp;to&nbsp;determine&nbsp;which&nbsp;fragment&nbsp;is&nbsp;in&nbsp;your&nbsp;frame_container &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;setFragment(fragmentDemo); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(OR) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;setFragment(new&nbsp;TestFragment1()); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;This&nbsp;could&nbsp;be&nbsp;moved&nbsp;into&nbsp;an&nbsp;abstract&nbsp;BaseActivity&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;class&nbsp;for&nbsp;being&nbsp;re-used&nbsp;by&nbsp;several&nbsp;instances &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;protected&nbsp;void&nbsp;setFragment(Fragment&nbsp;fragment)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FragmentManager&nbsp;fragmentManager&nbsp;=&nbsp;getSupportFragmentManager(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FragmentTransaction&nbsp;fragmentTransaction&nbsp;=&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fragmentManager.beginTransaction(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fragmentTransaction.replace(android.R.id.content,&nbsp;fragment); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fragmentTransaction.commit(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;}要将片段添加到Activity或FramentActivity中,它需要一个Container。该容器应该是一个“&nbsp;Framelayout”,可以包含在xml中,否则你可以使用默认容器android.R.id.content来删除或替换Activity中的片段。main.xml中<RelativeLayout &nbsp;&nbsp;&nbsp;&nbsp;android:layout_width="match_parent" &nbsp;&nbsp;&nbsp;&nbsp;android:layout_height="match_parent"&nbsp;> &nbsp;<!--&nbsp;Framelayout&nbsp;to&nbsp;display&nbsp;Fragments&nbsp;--> &nbsp;&nbsp;&nbsp;<FrameLayout &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;android:id="@+id/frame_container" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;android:layout_width="match_parent" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;android:layout_height="match_parent"&nbsp;/> &nbsp;&nbsp;&nbsp;&nbsp;<ImageView &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;android:id="@+id/imagenext" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;android:layout_width="wrap_content" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;android:layout_height="wrap_content" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;android:layout_alignParentBottom="true" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;android:layout_alignParentRight="true" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;android:layout_margin="16dp" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;android:src="@drawable/next"&nbsp;/></RelativeLayout>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Android