简体   繁体   English

重新使用DialogFragment作为活动布局中的片段

[英]Re-Use DialogFragment as a Fragment in Activity Layout

Was trying to reuse a complex DialogFragment as a Fragment in an activity layout. 试图将复杂的DialogFragment作为活动布局中的片段重用。 I don't want to have to re-write this whole DialogFragment class as it is quite complex. 我不想重写整个DialogFragment类,因为它很复杂。 In one spot only does the designers want this layout not as a popup but in a page. 在一个地方,设计人员只希望此布局不是弹出窗口,而是页面中的布局。 Is there a way to get around the DialogFragment from throwing this (From DialogFragment.java): 有没有一种方法可以解决此问题(从DialogFragment.java):

    if (view != null) {
        if (view.getParent() != null) {
            throw new IllegalStateException("DialogFragment can not be attached to a container view");
        }
        mDialog.setContentView(view);
    }

I went so far as to null out the creation of Dialog in the OnCreateDialog() overridden method and added the overridden method onCreateView(). 我什至在OnCreateDialog()重写方法中取消了Dialog的创建,并添加了onCreateView()重写的方法。 But still view is not null and throws the IllegalStateException. 但是,视图仍然不为null,并抛出IllegalStateException。 I have the fragment embedded in the activity layout 我在活动布局中嵌入了片段

<fragment
     android:id="@+id/fragment_mine"
     android:name="com.test.MyDialogFragment"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:visibility="visible" />

So question is, is there anyway to re-use a DialogFragment as a Fragment in an Activity's layout? 所以问题是,是否有必要将DialogFragment作为Activity布局中的Fragment重用?

Cant you just use your DialogFragment as a regular fragment ?. 不能只将DialogFragment用作常规片段?。

Like so: 像这样:

public class MainActivity extends FragmentActivity {

  @Override
  protected void onCreate(Bundle state) {
    super.onCreate(state);
    final int FRAGMENT_ID = 100;

    LinearLayout contentView = new LinearLayout(this);
    contentView.setOrientation(LinearLayout.VERTICAL);
    Button showButton = new Button(this);
    showButton.setText("Show Dialog");
    showButton.setOnClickListener(new View.OnClickListener() {

      @Override
      public void onClick(View v) {
        //using TestDialogFragment as a dialog
        new TestDialogFragment().show(getSupportFragmentManager(), "dialog");
      }
    });

    contentView.addView(showButton);

    final LinearLayout fragmentContainer = new LinearLayout(this);
    fragmentContainer.setId(FRAGMENT_ID);
    contentView.addView(fragmentContainer);

    setContentView(contentView);

    //using TestDialogFragment as a Fragment
    getSupportFragmentManager().beginTransaction()
        .replace(FRAGMENT_ID, new TestDialogFragment()).commit();

  }

  public static class TestDialogFragment extends DialogFragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
      TextView view = new TextView(getActivity());
      view.setText("Test Fragment");
      return view;
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM