Giter VIP home page Giter VIP logo

gesture-recycler's Introduction

API Android Arsenal

Gesture Recycler

This library provides swipe & drag and drop support for RecyclerView. Based on great example from Android-ItemTouchHelper-Demo.

Demo

Features

  • item click/long press/double tap listener
  • background views for swipeable items
  • empty view
  • undo
  • swipe
  • long press drag
  • manual mode drag
  • support for different layout managers
  • predefined drag & swipe flags for RecyclerView's layout managers
  • DiffUtil feature
  • header/footer

Dependency

To use this library in your android project, just simply add the following dependency into your build.gradle

repositories {
    mavenCentral()
}

dependencies {
    implementation "com.github.thesurix:gesture-recycler:1.17.0"
}

How to use?

// Define your RecyclerView and adapter as usually
val manager = LinearLayoutManager(context)
recyclerView.setHasFixedSize(true)
recyclerView.layoutManager = manager

// Extend GestureAdapter and write your own
// ViewHolder items must extend GestureViewHolder
val adapter = MonthsAdapter(R.layout.linear_item)
adapter.data = months
recyclerView.adapter = adapter

Swipe and drag & drop support:

val gestureManager = GestureManager.Builder(recyclerView)
                 // Enable swipe
                .setSwipeEnabled(true)
                 // Enable long press drag and drop 
                .setLongPressDragEnabled(true)
                 // Enable manual drag from the beginning, you need to provide View inside your GestureViewHolder
                .setManualDragEnabled(true)
                 // Use custom gesture flags
                 // Do not use those methods if you want predefined flags for RecyclerView layout manager 
                .setSwipeFlags(ItemTouchHelper.LEFT or ItemTouchHelper.RIGHT)
                .setDragFlags(ItemTouchHelper.UP or ItemTouchHelper.DOWN)
                .build()

Background view for swipeable items:

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- Content of the background view (you can use regular layout or ViewStub for better performance)-->
    <ViewStub
            android:id="@+id/background_view_stub"
            android:inflatedId="@+id/background_view"
            android:layout="@layout/background_view_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    
    <LinearLayout
            android:id="@+id/foreground_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">
            <!-- Content of the top view -->
    </LinearLayout>
</FrameLayout>
    // Override foregroundView, backgroundView() variables in ViewHolder to provide top and bottom view
    open val foregroundView: View
            get() = foreground
            
    open val backgroundView: View?
            get() = background

Different background views:

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- Content of the background views -->
    <include
            android:id="@+id/month_background_one"
            layout="@layout/first_background_item"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:visibility="gone"/>

    <include
            android:id="@+id/month_background_two"
            layout="@layout/second_background_item"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:visibility="gone"/>
    
    <LinearLayout
            android:id="@+id/foreground_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">
            <!-- Content of the top view -->
    </LinearLayout>
</FrameLayout>
    // Override foregroundView variable and getBackgroundView(direction: Int) method in ViewHolder to provide top and bottom views
    open val foregroundView: View
            get() = foreground
            
    override fun getBackgroundView(direction: Int): View? {
            //direction can be ItemTouchHelper.LEFT, ItemTouchHelper.RIGHT, ItemTouchHelper.UP, ItemTouchHelper.DOWN
            if (direction == ItemTouchHelper.RIGHT) {
                return firstBackgroundView
            }
            return secondBackgroundView
    }

Data callbacks:

adapter.setDataChangeListener(object : GestureAdapter.OnDataChangeListener<MonthItem> {
                        override fun onItemRemoved(item: MonthItem, position: Int, direction: Int) {
                        }
        
                        override fun onItemReorder(item: MonthItem, fromPos: Int, toPos: Int) {
                        }
                    })

Data animations:

// Support for data animations
adapter.add(month)
adapter.insert(month, 5)
adapter.remove(5)
adapter.swap(2, 5)

// or
adapter.setData(months, diffUtilCallback)

// This will interrupt pending animations
adapter.data = months

Item click events:

// Attach DefaultItemClickListener or implement RecyclerItemTouchListener.ItemClickListener
recyclerView.addOnItemTouchListener(RecyclerItemTouchListener(object : DefaultItemClickListener<MonthItem>() {

            override fun onItemClick(item: MonthItem, position: Int): Boolean {
                // return true if the event is consumed
                return true
            }

            override fun onItemLongPress(item: MonthItem, position: Int) {
            }

            override fun onDoubleTap(item: MonthItem, position: Int): Boolean {
                // return true if the event is consumed
                return true
            }
        }))

Empty view:

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
    
    <!-- Define your empty view in layout -->
    <TextView
        android:id="@+id/empty_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textAppearance="@android:style/TextAppearance.Large"
        android:text="No data"/>
</FrameLayout>
// Pass null to disable empty view
val emptyView = view.findViewById(R.id.empty_view)
adapter.setEmptyView(emptyView)

// or use callback
adapter.setEmptyViewVisibilityListener(object : EmptyViewVisibilityListener {
            override fun onVisibilityChanged(visible: Boolean) {
                // show/hide emptyView with animation
            }
        })

Undo:

// Undo last data transaction (add, insert, remove, swipe, reorder)
adapter.undoLast()

// Set undo stack size
adapter.setUndoSize(2)

Header/Footer:

// Enabled, disable header/footer by builder
GestureManager.Builder(recyclerView)
    .setHeaderEnabled(state)
    .setFooterEnabled(state)

// or directly by adapter
adaper.setHeaderEnabled(state)
adaper.setHeaderEnabled(state)

// if header or footer is enabled then library will pass viewType (TYPE_HEADER_ITEM, TYPE_FOOTER_ITEM)
// to onCreateViewHolder(parent: ViewGroup, viewType: Int)
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): GestureViewHolder<T> {
    return when (viewType) {
            TYPE_HEADER_ITEM -> {
                // return header view holder
            }
            TYPE_FOOTER_ITEM -> {
                // return footer view holder
            }
            else -> {
                // return regular view holder
            }
        }
}

// if getItemViewType(viewPosition: Int) is used in your adapter then
// firstly call super.getItemViewType() and check if library wants to handle incoming view type
override fun getItemViewType(viewPosition: Int): Int {
    val handledType = super.getItemViewType(viewPosition)
    if (handledType > 0) {
        // library wants to handle this case, simply return
        return handledType
    }
    return yourTypes
}

Help

See examples.

To do

  • examples with data binding
  • tests
  • different layouts for different swipe directions

Licence

Copyright 2022 thesurix

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

gesture-recycler's People

Contributors

alexandermatveychuk avatar thesurix avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

gesture-recycler's Issues

Half drag like inbox app?

Thank you for this great lib.

Is it possible to do half drag implementation like the Google's Inbox app. From left to right or right to left to show custom layout.

outlook-android-apk

Observer was not Registered

Thanks for the lib,

btw, i have a problem, it's happened when i press back, and the activity destroyed then

    Caused by: java.lang.IllegalStateException: Observer com.thesurix.gesturerecycler.EmptyViewDataObserver@1e8b038a was not registered.
       at android.database.Observable.unregisterObserver(Observable.java:69)
       at android.support.v7.widget.RecyclerView$Adapter.unregisterAdapterDataObserver(RecyclerView.java:5676)
       at com.thesurix.gesturerecycler.GestureAdapter$1.onViewDetachedFromWindow(GestureAdapter.java:82)
       at android.view.View.dispatchDetachedFromWindow(View.java:13633)
       at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:2966)
       at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:2963)
       at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:2963)
       at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:2963)
       at android.view.ViewGroup.removeViewInternal(ViewGroup.java:4296)
       at android.view.ViewGroup.removeViewInternal(ViewGroup.java:4269)
       at android.view.ViewGroup.removeView(ViewGroup.java:4201)
       at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1165)
       at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
       at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1234)
       at android.support.v4.app.FragmentManagerImpl.dispatchDestroy(FragmentManager.java:2083)
       at android.support.v4.app.FragmentController.dispatchDestroy(FragmentController.java:244)
       at android.support.v4.app.FragmentActivity.onDestroy(FragmentActivity.java:369)
       at android.support.v7.app.AppCompatActivity.onDestroy(AppCompatActivity.java:199)
       at android.app.Activity.performDestroy(Activity.java:6156)
       at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1147)
       at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3866)
       at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3897) 
       at android.app.ActivityThread.access$1400(ActivityThread.java:167) 
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1473) 
       at android.os.Handler.dispatchMessage(Handler.java:111) 
       at android.os.Looper.loop(Looper.java:194) 
       at android.app.ActivityThread.main(ActivityThread.java:5546) 
       at java.lang.reflect.Method.invoke(Native Method) 
       at java.lang.reflect.Method.invoke(Method.java:372) 
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:964) 
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:759) ```

Drag & Drop + Header Objects

Hey there, first of all, amazing work with this library.

I may be overlooking something in the examples, but is there any way to get the cards to only move within header objects (like locking the MonthHeaders from being surpassed by any of the dragging cards)?

For example:

FIRST QUARTER

  • Card 1
  • Card 2
  • Card 3
    SECOND QUARTER

Cards 1-3 will only move within First Quarter and Second Quarter, not overtake the position of SECOND QUARTER when scrolled that far?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.