Skip to content

Card Fragment

Prem Ankur edited this page Dec 27, 2018 · 1 revision
  • To create a card fragment:
    • Create a fragment class with BaseCardFragment() as superclass and override the following methods
      class PlainCardFragment : BaseCardFragment() {
      
          override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? =
                  inflater.inflate(R.layout.fragment_plain_card, container, false)
      
          /**
           * Return the background layout (preferably BlurLayout) for effects during transition
           */
          override fun getBackgroundBlurLayout(): ViewGroup? = blurLayout
      
          /**
           * Return the Drag area that should be used for swipe gestures
           */
          override fun getDragView(): View? = dragArea
      
          /**
           * Return the root view (preferably a CardView) of the fragment
           */
          override fun getRootView(): ViewGroup? = rootCard
      
          /**
           * Return the Drag handle resource ID to toggle visibility during transition
           */
          override fun dragHandleId(): Int = R.id.drag_handle_image
      }
      
    • Add XML data to fragment's layout (refer to styles.xml for style templates):
      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/root_layout"
          style="@style/MatchParentMatchParent"
          android:animateLayoutChanges="true">
      
          <io.alterac.blurkit.BlurLayout
              android:id="@+id/blurLayout"
              style="@style/BlurLayoutFullScreen" />
      
          <androidx.cardview.widget.CardView
              android:id="@+id/rootCard"
              style="@style/CardView.WrapWidthHeight"
              android:layout_centerInParent="true">
      
              <TextView
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_gravity="center_horizontal"
                  android:layout_marginTop="20dp"
                  android:gravity="center"
                  android:padding="14dp"
                  android:text="@string/this_is_a_sample_plain_card_fragment"
                  android:textColor="@android:color/white"
                  android:textSize="16sp"
                  android:textStyle="bold" />
      
              <TextView
                  android:layout_width="280dp"
                  android:layout_height="wrap_content"
                  android:layout_gravity="center_horizontal"
                  android:layout_marginTop="100dp"
                  android:layout_marginBottom="100dp"
                  android:gravity="center"
                  android:padding="14dp"
                  android:text="@string/you_can_swipe_down_from_anywhere_inside_to_dismiss_this_card"
                  android:textColor="@android:color/white"
                  android:textSize="14sp" />
      
              <ImageView
                  android:id="@+id/drag_handle_image"
                  style="@style/DragHandleLight"
                  android:contentDescription="@string/drag_handle" />
      
          </androidx.cardview.widget.CardView>
      
          <View
              android:id="@+id/dragArea"
              style="@style/DragAreaFull"
              android:layout_alignStart="@+id/rootCard"
              android:layout_alignTop="@+id/rootCard"
              android:layout_alignEnd="@+id/rootCard"
              android:layout_alignBottom="@+id/rootCard" />
      
      </RelativeLayout>
      

Clone this wiki locally