SwipeMenuRecyclerView is a lightweight sliding menu library,it can be use in the list or on its own.
- Support multiple styles of menus (Classic, Overlay, Parallax), and easily customize your own style.
- Support long menus, you can also slide on menu button.
- Easily build in layout editor, as simple as build a TextView.
Screenrecord.mp4
1.Add JitPack repository
Add it in your root build.gradle at the end of repositories:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
If your project is created by AndroidStudio-Arctic-Fox, the repositoryies configuration may be in settings.gradle:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
...
maven { url 'https://jitpack.io' }
}
}
2.Add the dependency
dependencies {
implementation 'com.github.aitsuki:SwipeMenuRecyclerView:2.1.5'
}
Depending on your ProGuard config and usage, you may to include the following lines in your proguard configuration file.
-keep class * implements com.aitsuki.swipe.SwipeLayout$Designer
In XML layout file:
<com.aitsuki.swipe.SwipeLayout
app:autoClose="true"
app:designer="@string/classic_designer"
app:preview="none">
<!-- This is the left menu, because it specifies `layout_gravity=start` -->
<TextView
android:id="@+id/left_menu"
android:layout_width="100dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@color/red500"
android:gravity="center"
android:text="LEFT"
android:textSize="18sp" />
<!-- This is the right menu -->
<TextView
android:id="@+id/right_menu"
android:layout_width="100dp"
android:layout_height="match_parent"
android:layout_gravity="end"
android:background="@color/blue500"
android:gravity="center"
android:text="RIGHT"
android:textSize="18sp" />
<!-- This is the content, because it dows not specify `layout_gravity` -->
<TextView
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Classic"
android:background="@color/white"
android:textSize="18sp" />
</com.aitsuki.swipe.SwipeLayout>
If use it as RecyclerView’s item, need to use SwipeMenuRecyclerView
instead of RecyclerView
.
SwipeMenuRecyclerView
is almost the same asRecyclerView
. It only slightly interferes withdispatchTouchEvent()
to allowSwipeLayout
automatically close menu when user scrolled theRecyclerView
, and prevent open multiple menus during multi-touch.But if user manually calls the
RecyclerView.scroollxxx()
method to scoll the list,SwipeMenuRecyclerView
will not automatically close menu, because it doesn’t know whether the user needs this behavior.In this case, you can use the
SwipeMenuRecyclerView.closeMenus()
method.
-
preview="none|left|right"
:defaultnone
.
This attribute is helps build menus when the content is covers it. -
autoClose="true|false"
:defaultfalse
. If menu is opened, whether to automatically close the menu when click it. -
designer="@string/classic_designer|overlay...|parallax..."
: defaultclassic
Setting the style of menu, is similar toBehavior
of CoordinateLayout.
See Designer
swipeLayout.addListener(object : SwipeLayout.Listener {
override fun onSwipe(menuView: View, swipeOffset: Float) {
}
override fun onSwipeStateChanged(menuView: View, newState: Int) {
}
override fun onMenuOpened(menuView: View) {
}
override fun onMenuClosed(menuView: View) {
}
})
-
menuView
: For the currently operating menu, if you use bothleftMenu
andrightMenu
at the same time, you need to determine which one is currently operating. For example, you can judge byid
orgravity
. -
swipeOffset
:The percentage of current menu display, the range is0.0f ~ 1.0f
. -
state
:STATE_IDLE
,STATE_DRAGGING
,STATE_SETTLING
SwipeLayout
has three build-in styles:
-
Classic : Menu scrolls with content.
-
Overlay :Menu does not scroll, and the content covers the menu.
-
Parallax : Menu scrolls with content,and provides parallax styles.
You can implement SwipeLayout.Designer
to customize more styles.
class ClassicDesigner : Designer {
private var leftMenu: View? = null
override fun onInit(parent: SwipeLayout, leftMenu: View?, rightMenu: View?) {
this.leftMenu = leftMenu
leftMenu?.visibility = View.INVISIBLE
rightMenu?.visibility = View.INVISIBLE
}
override fun onLayout(menuView: View, left: Int, top: Int, right: Int, bottom: Int) {
menuView.visibility = if (right - left > 0) VISIBLE else INVISIBLE
if (menuView == leftMenu) {
menuView.layout(right - menuView.width, menuView.top, right, menuView.bottom)
} else {
menuView.layout(left, menuView.top, left + menuView.width, menuView.bottom)
}
}
}
-
onInit()
is called afteronMeasure
ofSwipeLayout
method,and it will only be called once. -
onLayout()
is called afteronLayout
ofSwipeLayout
method,and is called back every time when user sliding.
Among them, the left, top, right, bottom
of the onLayout
method represent the visible area of the current menu, which can be understood as VisibleRect
.
Feel free to dive in! Open an issue or submit PRs.
MIT © Aitsuki