Is it possible to have Persistent Bottom Sheet as

2019-08-29 17:38发布

问题:

I want to make Persistent Bottom Sheet as a Fragment inside a Fragment but can't find anywhere how to achieve it. Everywhere Persistent Bottom Sheet is added as a layout inside activity/fragment but is it possible to make Persistent Bottom Sheet as a Fragment so that I can reuse that view some where else?

回答1:

There is an amazing tutorial on BottomSheet in AndroidHive

You can create a class extending BottomSheetDialogFragment.

public class BottomSheetFragment extends BottomSheetDialogFragment {
    public BottomSheetFragment() {
    // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_bottom_sheet_dialog, container, 
              false);
    }
}

And toggle it using below code

BottomSheetFragment bottomSheetFragment = new BottomSheetFragment();
bottomSheetFragment.show(getSupportFragmentManager(), bottomSheetFragment.getTag());


回答2:

you can use BottomSheetDialogFragment, 
I imagine you want to do just like this: [https://cdn-images-1.medium.com/max/800/1*rLBf6fPtfWhXqnTOLlj0cQ.png][1]

create the navigationView file

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">


        <android.support.design.widget.NavigationView
            android:id="@+id/navigation_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:itemBackground="@drawable/state_pressed"
            app:menu="@menu/activity_main_drawer" />


    </RelativeLayout>

</android.support.constraint.ConstraintLayout>

and create file menu

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:showIn="navigation_view">

    <group android:checkableBehavior="single">
        <item
            android:id="@+id/nav_camera"
            android:icon="@drawable/round_home_24"
            android:title="Inicio"
            android:checked="true"
            android:checkable="true"/>
        <item
            android:id="@+id/nav_gallery"
            android:icon="@drawable/outline_settings_24"
            android:title="Configuración"
            android:checkable="true"/>
        <item
            android:id="@+id/nav_slideshow"
            android:icon="@drawable/round_mail_outline_24"
            android:title="Email"
            android:checkable="true"/>
        <item
            android:id="@+id/nav_manage"
            android:icon="@drawable/outline_search_24"
            android:title="Buscar" />
    </group>

    <item android:title="Communicate">
        <menu>
            <item
                android:id="@+id/nav_share"
                android:icon="@drawable/outline_lock_24"
                android:title="Share" />
            <item
                android:id="@+id/nav_send"
                android:icon="@drawable/outline_account_circle_24"
                android:title="Send" />
        </menu>
    </item>

</menu>

and create the file for borderRadius

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true">
        <shape xmlns:android="http://schemas.android.com/apk/res/android">
            <solid android:color="#ff0000" />
            <corners android:bottomRightRadius="90dp" android:bottomLeftRadius="90dp" />
            <padding android:left="30dp"/>
        </shape>
    </item>
    <item android:state_checked="false">
        <shape xmlns:android="http://schemas.android.com/apk/res/android">
            <solid android:color="@android:color/transparent" />
            <corners android:bottomRightRadius="30dp" android:topRightRadius="30dp" />
        </shape>
    </item>

</selector>

and create state_pressed backgroud color

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true" android:drawable="@drawable/border_radius_tabs">
        <color android:color="@color/colorPrimary"></color>
    </item>
    <item android:state_checked="false">
<color android:color="@color/primaryLightColor"></color>
    </item>

</selector>