The Recycler View in android requires an adapter being configured with view holder pattern being core of its implementation. Ashera provides a generic implementation of the view holder pattern. Hence recycler view can be written by configuring a few attributes.
Also 2 types of adapter has been provided:
Also the following layout managers are supported by RecyclerView
Recyclerview is configured with ListAdapter by default.
The layout for recyclerview is configured using
tools:listitem="@layout/recyclerview_text" attribute.. This example creates a basic RecyclerView of hard coded data using ListAdapter.
ListAdpater also support non draggable header and footer. The layout for recyclerview is configured using layout attribute. The type simple indicates that ListAdapter should be created.
The layout for recyclerview is configured using
layput="@xml/recyclerview_simple" attribute.. This example creates a basic RecyclerView of hard coded data with header and footer using ListAdapter.
<layout adapter="simple">
<header layout="@layout/listheader"></header>
<footer layout="@layout/listfooter"></footer>
<item viewHolderIds="@+id/name" layout="@layout/recyclerview_text"></item>
</layout>
The list adapter can be configured to so that the items can be deleted on swipe and the ordering can be changed using drag and drop. The following example demonstrates the same using the following attributes:
deleteOnSwipe="true"
dragDropMode="swaponhighlight"
dragSwapMode="notifyItemMoved"
dragStartMode="longpress"
<androidx.recyclerview.widget.RecyclerView
android:visibility="gone"
android:id="@+id/recyclerview"
onAndroidTouch=""
modelIdPath="name"
swtAttachEventBubbler="mousedown,mouseup,mousemove"
deleteOnSwipe="true"
dragDropMode="swaponhighlight"
dragSwapMode="notifyItemMoved"
dragStartMode="longpress"
onMoved="getDragAndDropData(model=. from testObj->view)"
modelFor="let item in . from testObj->parentview into loopvar as map"
viewHolderIds="@+id/name"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
layout="@xml/recyclerview_simple"
tools:listitem="@layout/recyclerview_text"/>
The list adapter also supports filtering using the filterItemPath="name" attribute.
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerview"
layout="@xml/recyclerview_simple"
modelFor="let item in . from testObj->parentview into loopvar as map"
modelIdPath="name"
viewHolderIds="@+id/name"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"
filterItemPath="name"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
tools:listitem="@layout/recyclerview_text" />
A fun Puzzle game which can be implemented using RecyclerView drag and drop.
<androidx.recyclerview.widget.RecyclerView
onAndroidTouch=""
swtAttachEventBubbler="mousedown,mouseup,mousemove"
modelIdPath="id"
android:id="@+id/recyclerview"
modelFor="let item in . from testObj->view into loopvar as map"
viewHolderIds="@+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:overScrollMode="never"
app:layoutManager="com.ashera.recycleview.FixedGridViewManager"
dragDirs="up|down|left|right"
swipeDirs="none"
fixedgrid_rowCount="3"
fixedgrid_columnCount="3"
fixedgrid_tileWidth="230"
fixedgrid_tileHeight="170"
dragDropMode="swapwhendropped"
dragSwapMode="notifyDataSetChanged"
dragStartMode="onclick"
onMoved="getDragAndDropData(model=. from testObj->view)"
dragSelectHighlightAttributes="scaleX: 1.2;scaleY: 1.2;alpha: 0.7;elevation:20dp;"
dragResetHighlightAttributes="scaleX: 1;scaleY: 1;alpha: 1;elevation:0dp"
tools:listitem="@layout/recyclerview_dragndrop_item" />
RecyclerViewAdapter can be very complex especially when it comes to dealing with different types of items. Groupie simplifies that for us.
Groupie is a simple, flexible library for complex RecyclerView layouts. It makes it easy to handle asynchronous content updates and insertions and user-driven content changes. At the item level, It abstracts the boilerplate of item view types, item layouts, viewholders, and span sizes.
Groupie helps with more complex implementation of RecyclerView like treating your content as logical groups and handles change notifications for you, Think of sections with headers and footers, expandable groups, blocks of vertical columns, and much more.
This example creates a sectioned list layout implemented using groupie adapter.
<layout xmlns:app="http://schemas.android.com/apk/res-auto"
adapter="groupie">
<section id="test1"
modelFor="let item in . from testObj->parentview into loopvar as map">
<header
layout="@layout/recyclerview_text"
viewHolderIds="@+id/name"/>
<item
layout="@layout/recyclerview_text"
modelFor="let item in subitems from item->loopvar into loopvar as map"
viewHolderIds="@+id/name" />
</section>
</layout>
This example shows how groupie adapter section can be used to create a complex layout. The example also illustrates how drag and drag can be implemented in groupie adpater. The enabling of drag and drop is similar to that of ListAdapter but at header, footer and item level, drag and drop can be enabled and disabled using swipeDirs and dragDirs attribute.
<layout adapter="groupie" xmlns:app="http://schemas.android.com/apk/res-auto">
<section id="test">
<header layout="@layout/listheader" swipeDirs="none" dragDirs="none" />
<footer layout="@layout/listfooter" swipeDirs="none" dragDirs="none"/>
<item layout="@layout/listview_testitem" modelFor="let item in test.items from sectionInfo->view into loopvar as map" viewHolderIds="@+id/name" dragAcrossSections="true"/>
</section>
<section id="test1">
<header layout="@layout/listheader" swipeDirs="none" dragDirs="none"/>
<footer layout="@layout/listfooter" swipeDirs="none" dragDirs="none"/>
<section1 id="test2" modelFor="let item in test1.items from sectionInfo->view into loopvar as map">
<header layout="@layout/listheader_data" viewHolderIds="@+id/textView" swipeDirs="none" dragDirs="none"/>
<item layout="@layout/listview_testitem" modelFor="let item in subitems from item->loopvar into loopvar as map" viewHolderIds="@+id/name"/>
</section1>
</section>
<section id="test3" modelFor="let item in test1.items from sectionInfo->view into loopvar as map">
<header layout="@layout/listheader_data" viewHolderIds="@+id/textView" swipeDirs="none" dragDirs="none"/>
<item layout="@layout/listview_testitem" modelFor="let item in subitems from item->loopvar into loopvar as map" viewHolderIds="@+id/name"/>
</section>
<section id="fullbleeditemParent" modelVar="let item = fullbleeditem from sectionInfo->view into loopvar as map">
<header layout="@layout/item_header" viewHolderIds="title,subtitle,icon" swipeDirs="none" dragDirs="none"/>
<item gridInsets="10dp" layout="@layout/item_card" modelFor="let item in subitems from item->loopvar into loopvar as map" viewHolderIds="FrameLayout,text"/>
</section>
<section id="grid" modelVar="let item = grid from sectionInfo->view into loopvar as map">
<header layout="@layout/item_header" viewHolderIds="title,subtitle,icon" swipeDirs="none" dragDirs="none"/>
<item swipeDirs="none" dragDirs="up|down|left|right|" gridInsets="10dp" layout="@layout/item_card" modelFor="let item in subitems from item->loopvar into loopvar as map" viewHolderIds="FrameLayout,text" numberOfColumns="3"/>
</section>
<section id="carousel" modelVar="let item = carousel from sectionInfo->view into loopvar as map">
<header layout="@layout/item_header" viewHolderIds="title,subtitle,icon" swipeDirs="none" dragDirs="none"/>
<item layout="@layout/item_carousel" swipeDirs="none"/>
</section>
<section id="expandabletest" modelVar="let item = expand from sectionInfo->view into loopvar as map">
<expandable layout="@layout/item_header" viewHolderIds="title,subtitle,icon" expand="true" onclick="icon">
<section1 id="expandable">
<item gridInsets="10dp" layout="@layout/item_card" modelFor="let item in subitems from item->loopvar into loopvar as map" viewHolderIds="FrameLayout,text" numberOfColumns="1"/>
</section1>
</expandable>
</section>
</layout>
The groupie adapter also supports filtering using the filterItemPath="name" attribute.
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerview"
layout="@xml/recyclerview_section"
android:layout_width="match_parent"
android:layout_height="match_parent"
filterItemPath="name"
app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
app:spanCount="12" />