为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

繁华开满天机 2019-07-25 19:32:46
如何使用以编程方式创建的内容视图向活动添加片段我想将一个片段添加到以编程方式实现其布局的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()的错误。这样做的正确方法是什么?
查看完整描述

3 回答

?
繁星点点滴滴

TA贡献1803条经验 获得超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扩展程序,或者只编写您自己的扩展程序


查看完整回答
反对 回复 2019-07-25
?
ibeautiful

TA贡献1993条经验 获得超5个赞

public class Example1 extends FragmentActivity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
          DemoFragment fragmentDemo = (DemoFragment) 
          getSupportFragmentManager().findFragmentById(R.id.frame_container);
          //above part is to determine which fragment is in your frame_container
          setFragment(fragmentDemo);
                       (OR)
          setFragment(new TestFragment1());
        }

        // This could be moved into an abstract BaseActivity 
        // class for being re-used by several instances
        protected void setFragment(Fragment fragment) {
            FragmentManager fragmentManager = getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = 
                fragmentManager.beginTransaction();
            fragmentTransaction.replace(android.R.id.content, fragment);
            fragmentTransaction.commit();
        }
    }

要将片段添加到Activity或FramentActivity中,它需要一个Container。该容器应该是一个“ Framelayout”,可以包含在xml中,否则你可以使用默认容器android.R.id.content来删除或替换Activity中的片段。

main.xml中

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
 <!-- Framelayout to display Fragments -->
   <FrameLayout
        android:id="@+id/frame_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <ImageView
        android:id="@+id/imagenext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_margin="16dp"
        android:src="@drawable/next" /></RelativeLayout>


查看完整回答
反对 回复 2019-07-25
  • 3 回答
  • 0 关注
  • 347 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信