How can we create a transparent Fragment Layout with Recycler view inside the Layout

808 views Asked by At

enter image description hereI have two fragment for my current application, In FragmentA I have a search view in Toolbar and on clicking on the search view , I have to show another fragment,FragmentB on top of fragmentA using my framelayout,with transparent view so that the contents in FragmentA can be seen as blurred. The newly opened fragment with transparent background has to show some content in the recycler view . How can we achieve the transparency and the recycler view inside the layout.

Layout code:

<androidx.drawerlayout.widget.DrawerLayout 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:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <LinearLayout
        android:id="@+id/dot_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/drawer_layout"
        android:elevation="50dp"
        android:gravity="center"
        android:orientation="vertical">

  
<androidx.appcompat.widget.Toolbar 
   xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/tools"
    xmlns:local="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@color/palate_survey_bg"
    app:ignore="NamespaceTypo"
    local:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    local:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">


    <androidx.appcompat.widget.SearchView
        android:id="@+id/Advance_search"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:background="@drawable/filter_search_bar_bg_lyt"
        app:iconifiedByDefault="false"
        app:queryHint="Search">

    </androidx.appcompat.widget.SearchView>
    </androidx.appcompat.widget.Toolbar >
        <FrameLayout
            android:id="@+id/search_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/toolbar">

        </FrameLayout>

    </LinearLayout>
    
    </androidx.drawerlayout.widget.DrawerLayout
1

There are 1 answers

2
Noah On

Based on the detailed image you provided. Fragment B is a child fragment of Fragment A. So if you want to show Fragment B when a click event is emitted from the SearchView, you should use Fragment A's FragmentManager. Normally, every fragment has a FragmentManager that can house other fragment views and layouts. To retrieve Fragment A's FragmentManager use this code:

Inside Fragment A:

  Advance_search.setOnClicklistener( v -> {

            getChildFragmentManager()
                  .beginTransaction()
                  .replace(R.id.search_container, new FragmentB())
                  .commit();

}

And then inside Fragment B's onViewCreated() method, you can now inflate the RecyclerView you want into Fragment B's view hierachy.

If you want Fragment B to be transparent, set

<item name="android:windowBackground">@android:color/transparent</item>

Or you can just remove windowBackground entirely.

And set android:backgroud="@android:color/transparent" on Fragment B or just remove it if you have set windowBackround to transparent.