GithubHelp home page GithubHelp logo

chandruark / gesture-recycler Goto Github PK

View Code? Open in Web Editor NEW

This project forked from thesurix/gesture-recycler

0.0 1.0 0.0 1.78 MB

This library provides swipe & drag and drop support for RecyclerView.

License: Other

Java 39.10% Kotlin 60.90%

gesture-recycler's Introduction

Download 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 view 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

Dependency

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

dependencies {
    compile 'com.thesurix.gesturerecycler:gesture-recycler:1.7.0'
}

How to use?

// Define your RecyclerView and adapter as usually
final LinearLayoutManager manager = new LinearLayoutManager(getContext());
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(manager);

// Extend GestureAdapter and write your own
// ViewHolder items must extend GestureViewHolder
final MonthsAdapter adapter = new MonthsAdapter(getContext(), R.layout.linear_item);
adapter.setData(getMonths());
recyclerView.setAdapter(adapter);

Swipe and drag & drop support:

GestureManager gestureManager = new GestureManager.Builder(mRecyclerView)
                 // 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 | ItemTouchHelper.RIGHT)
                .setDragFlags(ItemTouchHelper.UP | 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 getForegroundView(), getBackgroundView() methods in ViewHolder to provide top and bottom view
    @Override
    public View getForegroundView() {
        return mForegroundView; // This view comes from R.id.foreground_view
    }

    @Override
    public View getBackgroundView() {
        return mBackgroundView; // This view comes from R.id.background_view_stub
    }

Data callbacks:

adapter.setDataChangeListener(new GestureAdapter.OnDataChangeListener<MonthItem>() {
            @Override
            public void onItemRemoved(final MonthItem item, final int position) {
            }

            @Override
            public void onItemReorder(final MonthItem item, final int fromPos, final int toPos) {
            }
        });

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.setData(months)

Item click events:

// Attach DefaultItemClickListener or implement RecyclerItemTouchListener.ItemClickListener
recyclerView.addOnItemTouchListener(new RecyclerItemTouchListener<>(new DefaultItemClickListener<CustomItem>() {
            @Override
            public boolean onItemClick(final CustomItem item, final int position) {
                // return true if the event is consumed
                return false;
            }

            @Override
            public void onItemLongPress(final CustomItem item, final int position) {
            }

            @Override
            public boolean onDoubleTap(final CustomItem item, final int position) {
                // return true if the event is consumed
                return false;
            }
        }));

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
final View emptyView = view.findViewById(R.id.empty_view);
adapter.setEmptyView(emptyView);

// or use callback
adapter.setEmptyViewVisibilityListener(new EmptyViewVisibilityListener() {
            @Override
            public void onVisibilityChanged(final boolean visible) {
                // show/hide emptyView with animation
            }
       });

Undo:

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

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

Help

See examples.

To do

  • bindable ViewHolder
  • examples with data binding
  • tests
  • header/footer
  • convert examples to kotlin

Licence

Copyright 2018 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

thesurix avatar

Watchers

 avatar

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.