LoginSignup
1
3

More than 5 years have passed since last update.

BottomSheetDialogFragment で FragmentTransition#replaceを行う

Last updated at Posted at 2017-06-07

はじめに

  • BottomSheetDialog内でFragmentTransaction#replace 行いたい人が対象です。
  • designサポートライブラリへの依存設定が必須です。(23.2以降)
  • 利用するケースとしてはBottomSheetDialogFragment内にリストを表示して、リストの項目タップ時にさらにリストを表示する、など再帰的に表示したいパターンなどが考えられます。(それ外に無い気もします...)

先に結論

  • よくあるサンプルのようにsetupDialogをOverrideするのではなく、onCreateView, onViewCreateをOverrideしましょう。

コード

layout_bottom_sheet.xml
<?xml version="1.0" encoding="utf-8"?>
<layout>

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:background="@android:color/transparent"
                  android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="ぼとむしーと"/>

        <FrameLayout
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </LinearLayout>
</layout>

MyBottomSheetDialogFragment.java

public class MyBottomSheetDialogFragment extends BottomSheetDialogFragment implements MyBottomSheetListener {

    public static MyBottomSheetDialogFragment newInstance() {
        return new MyBottomSheetDialogFragment();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        LayoutBottomSheetBinding mBinding = DataBindingUtil.inflate(inflater, R.layout.layout_bottom_sheet, container, false);
        return mBinding.getRoot();
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        showStubFragment();
    }

    @Override
    public void onClick() {
        showStubFragment();
    }

    private void showStubFragment() {
        Fragment fragment = new StubFragment();
        getChildFragmentManager().beginTransaction()
                .replace(R.id.container, fragment)
                .commit();
    }

    public static class StubFragment extends Fragment {

        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            Button button = new Button(getActivity());
            // replace確認用
            button.setText("next" + DateFormat.format("yyyy/MM/dd kk:mm:ss", System.currentTimeMillis()));
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Fragment fragment = getParentFragment();
                    if (fragment instanceof MyBottomSheetListener) {
                        ((MyBottomSheetListener) fragment).onClick();
                    }
                }
            });

            return button;
        }
    }
}

MyBottomSheetListener.java
public interface MyBottomSheetListener {
    void onClick();
}
1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3