Skip to content

Latest commit

 

History

History
201 lines (147 loc) · 6.45 KB

README.en.md

File metadata and controls

201 lines (147 loc) · 6.45 KB

SwipeMenuRecyclerView

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

Table of Contents

Declaring dependencies

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'
}

ProGuard

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

Usage

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 as RecyclerView. It only slightly interferes with dispatchTouchEvent() to allow SwipeLayout automatically close menu when user scrolled the RecyclerView, 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.

Declared attributes for SwipeLayout

  • preview="none|left|right":default none.
    This attribute is helps build menus when the content is covers it.

  • autoClose="true|false":default false. If menu is opened, whether to automatically close the menu when click it.

  • designer="@string/classic_designer|overlay...|parallax...": default classic
    Setting the style of menu, is similar to Behavior of CoordinateLayout.
    See Designer

Listener for SwipeLayout

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 both leftMenu and rightMenu at the same time, you need to determine which one is currently operating. For example, you can judge by id or gravity.

  • swipeOffset :The percentage of current menu display, the range is 0.0f ~ 1.0f.

  • state : STATE_IDLE , STATE_DRAGGING, STATE_SETTLING

Designer

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 after onMeasure of SwipeLayout method,and it will only be called once.

  • onLayout() is called after onLayout of SwipeLayout 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.

IMG_0074

Contributing

Feel free to dive in! Open an issue or submit PRs.

License

MIT © Aitsuki