【问题标题】:Re-Use DialogFragment as a Fragment in Activity Layout重用 DialogFragment 作为 Activity 布局中的 Fragment
【发布时间】:2014-03-07 01:51:09
【问题描述】:

试图将复杂的 DialogFragment 重用为 Activity 布局中的 Fragment。我不想重写整个 DialogFragment 类,因为它非常复杂。在一个地方,设计师只希望这种布局不是作为弹出窗口而是在页面中。有没有办法绕过 DialogFragment 抛出这个(来自 DialogFragment.java):

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

我什至在 OnCreateDialog() 重写方法中取消了 Dialog 的创建,并添加了 onCreateView() 重写方法。但仍然视图不为空并抛出 IllegalStateException。我在活动布局中嵌入了片段

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

所以问题是,有没有办法在 Activity 的布局中重新使用 DialogFragment 作为 Fragment?

【问题讨论】:

    标签: android android-fragments android-dialogfragment


    【解决方案1】:

    你不能把你的 DialogFragment 当作一个普通的片段来使用吗?

    像这样:

    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;
        }
      }
    }
    

    【讨论】:

    • 是的,我想我可以,但我的 DialogFragment 没有使用 onCreateView,它使用了 onCreateDialog()。我必须同时拥有这两种方法来创建我们的对话框更加复杂,但是是的,这就是我需要的要点谢谢!
    • @JPM 嗨,你能解决这个问题吗,因为我有同样的问题,我想抽象一下,以避免为活动和对话框重新创建相同的视图和点击
    • 我现在要做的一件事是在 onCreateView() 中使用 getShowsDialog() ... if (!getShowsDialog()) { do view stuff } else return null;并在 onCreateDialog ... if (getShowsDialog()) { do dialog stuff } else return null;
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-16
    • 1970-01-01
    相关资源
    最近更新 更多