Android Library to provide swipe, click and other functionality to RecyclerView

Related tags

RecyclerViewEnhanced
Overview

RecyclerViewEnhanced

Android Library to provide swipe, click and other functionality to RecyclerView

Usage

Add this to your build.gradle file

dependencies {
  compile 'com.nikhilpanju.recyclerviewenhanced:recyclerviewenhanced:1.1.0'
}

Features

  • Supports API 14+ (Earlier APIs not tested
  • Supports any view for "Swipe Options"
  • Doesn't require any new adapters or new views. Works with any existing RecyclerViews.
  • Requires adding OnItemTouchListener to the RecyclerView
  • Supports clicking and swiping functionalities.
  • Supports disabling clicking and swiping for particular items/rows.
  • Supports independentViews in your items/rows (Read below for more information)
  • Supports fadeViews in your items/rows (Read below for more information)

Demo

Build the sample application to try RecyclerViewEnhanced alt text

Configuring

  • Create an instance of RecyclerTouchListener

    onTouchListener = new RecyclerTouchListener(this, mRecyclerView);

  • Set IndependentViews and FadeViews (If required)

    IndependentViews are views which can be clicked separately from the entire row. Their clicks have different functionality from row clicks. FadeViews are views which fade in and out as the rows are swiped closed and opened respectively.

    onTouchListener.setIndependentViews(R.id.rowButton)
                   .setViewsToFade(R.id.rowButton)               
    
  • Implement OnRowClickListener using setClickable()

    setClickable() will enable clicks for the recycler view items and the IndependentViews

    .setClickable(new RecyclerTouchListener.OnRowClickListener() {
              @Override
              public void onRowClicked(int position) {
                  // Do something
              }
    
              @Override
              public void onIndependentViewClicked(int independentViewID, int position) {
                  // Do something
              }
          })               
    
  • Enable Swipe Functionality

    Set the views for which you require a click listener and enable swiping by using setSwipeable()

    .setSwipeOptionViews(R.id.add, R.id.edit, R.id.change)
    .setSwipeable(R.id.rowFG, R.id.rowBG, new RecyclerTouchListener.OnSwipeOptionsClickListener() {
              @Override
              public void onSwipeOptionClicked(int viewID, int position) {
                  if (viewID == R.id.add) {
                      // Do something
                  } else if (viewID == R.id.edit) {
                      // Do something
                  } else if (viewID == R.id.change) {
                      // Do something
                  }
             }
         });
    
  • Adding the listener to the RecyclerView

    In onResume() add the listener:

    mRecyclerView.addOnItemTouchListener(onTouchListener);
    

    In onPause() remove the listener:

    mRecyclerView.removeOnItemTouchListener(onTouchListener);
    

Additional Functionality

  • Use onRowLongClickListener to receive long click events

    .setLongClickable(true, new RecyclerTouchListener.OnRowLongClickListener() {
                      @Override
                      public void onRowLongClicked(int position) {
                          ToastUtil.makeToast(getApplicationContext(), "Row " + (position + 1) + " long clicked!");
                      }
                  })
    
  • Use setUnSwipeableRows() to disable certain rows from swiping. Using this also displays an "difficult-to-slide" animation when trying to slide an unswipeable row.

  • Use setUnClickableRows() to disable click actions for certain rows. (Note: This also prevents the independentViews from being clicked).

  • openSwipeOptions() opens the swipe options for a specific row.

  • closeVisibleBG() closes any open options.

  • Implement OnSwipeListener to get onSwipeOptionsClosed() and onSwipeOptionsOpened() events.

Closing swipe options when clicked anywhere outside of the recyclerView:

  • Make your Activity implement RecyclerTouchListener.RecyclerTouchListenerHelper and store the touchListener
private OnActivityTouchListener touchListener;

@Override
public void setOnActivityTouchListener(OnActivityTouchListener listener) {
    this.touchListener = listener;
}
  • Override dispatchTouchEvent() of your Activity and pass the MotionEvent variable to the touchListener
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    if (touchListener != null) touchListener.getTouchCoordinates(ev);
        return super.dispatchTouchEvent(ev);
}

Author

License

Copyright 2016 Nikhil Panju

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.

Issues
  • Ryan/ignored view types

    Ryan/ignored view types

    Many recycler views will have items of varying types. You may not want to have the same foreground/background and swiping behaviors for these views (as I found in my own project using this library).

    opened by ryansgot 6
  •  some problems

    some problems

    I found that when the screen turns off when the swiped becomes ineffective and Viewholder multiplexing effect occurs.

    test environment: Millet 3 system: 6.0.1

    opened by BelongsH 5
  • 如何添加侧滑删除

    如何添加侧滑删除

    很喜欢改控件样式,需要侧滑课以删除和修改的,如何实现,试了下,实现了侧滑删除,但修改那个始终无效了,

    opened by imtianx 3
  • when I refresh the data of recyclerview,app crush;hope to get your help

    when I refresh the data of recyclerview,app crush;hope to get your help

    log belows: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.View.getHeight()' on a null object reference at com.nikhilpanju.recyclerviewenhanced.RecyclerTouchListener.handleTouchEvent(RecyclerTouchListener.java:474) at com.nikhilpanju.recyclerviewenhanced.RecyclerTouchListener.onInterceptTouchEvent(RecyclerTouchListener.java:127) at android.support.v7.widget.RecyclerView.dispatchOnItemTouchIntercept(RecyclerView.java:2231) at android.support.v7.widget.RecyclerView.onInterceptTouchEvent(RecyclerView.java:2277) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1977) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2435) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2066) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2435) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2066) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2435) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2066) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2435) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2066) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2435) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2066) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2435) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2066) at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2485) at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1783) at android.app.Activity.dispatchTouchEvent(Activity.java:2835) at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2444) at android.view.View.dispatchPointerEvent(View.java:8712) at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4236) at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4090) at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3618) at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3684) at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3644) at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3773) at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3652) at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3830) at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3618) at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3684) at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3644) at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3652) at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3618) at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5962) at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5936) at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5896) at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6059) at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:193) at android.os.MessageQueue.nativePollOnce(Native Method) at android.os.MessageQueue.next(MessageQueue.java:143) at android.os.Looper.loop(Looper.java:122) at android.app.ActivityThread.main(ActivityThread.java:5400) 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:1037) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832)

    opened by Reteyery 3
  • How Implement OnSwipeListener?

    How Implement OnSwipeListener?

    Please, how Implement OnSwipeListener? Thanks!

    opened by krawa 3
  • Doesn't it provide left swipe direction?

    Doesn't it provide left swipe direction?

    I want to have swipable edit button on left side instead of standard right side. I dont find any way or method to do that. Please help me.

    opened by ghost 3
  • left to right not work

    left to right not work

    hi how to enable left to right swipe? setLeftToRightSwipeable does not work...

    thanks

    opened by MohsenShafiee 1
  • onTouchListner.openSwipeOptions() not working

    onTouchListner.openSwipeOptions() not working

    when I call the onTouchListner.openSwipeOptions() method, it doesn't openSwipeOptions for the correct row if the recycler view has been scrolled past the first few visible items.

    here is my code:

    onTouchListener.setClickable(new RecyclerTouchListener.OnRowClickListener() {
          @Override
          public void onRowClicked(int position) {
            onTouchListener.openSwipeOptions(position);
          }
    
          @Override
          public void onIndependentViewClicked(int independentViewID, int position) {
    
          }
    });
    
    opened by abdalmoniem 0
  • onIndependentViewClicked callback not working on some devices

    onIndependentViewClicked callback not working on some devices

    For Vivo device it doesn't work, For nexus 5, s6 works fine.

    Can't seem to find any logical reason for this

    opened by hussi1 0
  • Want to Left to Right swipe. How to add?

    Want to Left to Right swipe. How to add?

    Can anyone help me to how to add Left to Right swipe for the recycerview? I need both swipe.. Left to Right and Right to Left..

    opened by Vijayadhas 0
  • Is this library maintainable?

    Is this library maintainable?

    Is this library live? I want to create pull request.

    opened by pavelpoley 0
  • Issue when getting data from Firebase

    Issue when getting data from Firebase

    While loading data from firebase, the view doesn't respond, i.e. I cannot scroll it, and cannot swipe it. On further inspection, I am getting this kind of error:

    E/ViewRootImpl: mView does not exist, so discard the remaining points. java.lang.NullPointerException: Attempt to invoke interface method 'boolean androidx.recyclerview.widget.RecyclerView$OnItemTouchListener.onInterceptTouchEvent(androidx.recyclerview.widget.RecyclerView, android.view.MotionEvent)' on a null object reference
    I/Surface: opservice is null false
    

    I even tried to attach onTouchListener, when the list is loaded. But still not working.

    opened by ssindher11 1
  • Ignores first 3 option item's click

    Ignores first 3 option item's click

    When I swipe and trying to tap an option It doesn't react to click but swiping closes. Just first 3.

    opened by zhukeev 0
  • Target Api 28 level

    Target Api 28 level

    it is not work what you have tall implement either i am not clickable of my view. public class BookingDetailActivity extends AppCompatActivity implements RecyclerTouchListener.RecyclerTouchListenerHelper,AddPackageContract.View {

    onTouchListener = new RecyclerTouchListener(this, recyclerViewAddItem); onTouchListener.setClickable(new RecyclerTouchListener.OnRowClickListener() { @Override public void onRowClicked(int position) { // ShowLogToast.ShowToast(getApplicationContext(), "Row " + (position + 1) + " clicked!", 1); }

                @Override
                public void onIndependentViewClicked(int independentViewID, int position) {
                    // main view is cliecked  (linear forgound view)
                   // ShowLogToast.ShowToast(getApplicationContext(), "Button in row " + (position + 1) + " clicked!", 1);
                }
            }).setSwipeOptionViews(R.id.linearEdit, R.id.linearDelete)
                    .setSwipeable(R.id.linearForeground, R.id.relativeBackground, new RecyclerTouchListener.OnSwipeOptionsClickListener() {
                        @Override
                        public void onSwipeOptionClicked(int viewID, int position) {
                            String message = "";
                            if (viewID == R.id.linearEdit) {
                                message += "Edit";
                                setEditItem(position);
                            } else if (viewID == R.id.linearDelete) {
                                showAlertDeleteItem(position);
                                message += "Delete";
                            }
                            message += " clicked for row " + (position + 1);
                            ShowLogToast.ShowLog(TAG,message);
                            //ShowLogToast.ShowToast(getApplicationContext(), message, 1);
                        }
                    });
    

    @Override protected void onResume() { super.onResume(); if (recyclerViewAddItem != null) recyclerViewAddItem.addOnItemTouchListener(onTouchListener); }

    @Override
    protected void onPause() {
        super.onPause();
        if (recyclerViewAddItem != null)
        recyclerViewAddItem.removeOnItemTouchListener(onTouchListener);
    }
    
    // for hide view of recycler when user touch out side of recyclerView.
    @Override
    public void setOnActivityTouchListener(OnActivityTouchListener listener) {
        this.touchListener = listener;
    }
    
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (touchListener != null) touchListener.getTouchCoordinates(ev);
        return super.dispatchTouchEvent(ev);
    }  }
    
    opened by gauraviqlance 0
  • Fixing crash with different viewtypes

    Fixing crash with different viewtypes

    Now, when marking positions with different viewtypes as unswipeable, it won't crash anymore.

    opened by GoncaloJoaoCorreia 0
  • Target API 26

    Target API 26

    Hello, We are currently using your library in one of our application. Actually we need a favour from your side. According to the Google Play Policy, all apps have to provide support for at least API level 26 which is not satisfied in your case. We are targeting API level 28. So can you please provide us support for API level 28 in your current repository?

    opened by yunuseker 2
  • Update README.md

    Update README.md

    opened by jaydipsensussoft 0
Releases(1.1)
Android Library to provide swipe, click and other functionality to RecyclerView

RecyclerViewEnhanced Android Library to provide swipe, click and other functionality to RecyclerView Usage Add this to your build.gradle file dependen

Nikhil Panju 1k Jul 31, 2021
Android Library to provide swipe, click and other functionality to RecyclerView

RecyclerViewEnhanced Android Library to provide swipe, click and other functionality to RecyclerView Usage Add this to your build.gradle file dependen

Nikhil Panju 1k Jul 17, 2021
Android library providing simple way to control divider items (ItemDecoration) of RecyclerView

RecyclerView-FlexibleDivider Android library providing simple way to control divider items of RecyclerView Release Note [Release Note] (https://github

Yoshihito Ikeda 2.4k Jul 30, 2021
Set of plugable extenstions for Android RecyclerView

DynamicRecyclerView Set of light and non-invasive extensions for Android RecyclerView widget. Does not use custom RecyclerView or LayoutManager. With

Ilja S. 343 Apr 18, 2021
Pumped up RecyclerView

##Description This is an attempt to make RecyclerView easier to use. Features built in: ProgressBar while adapter hasn't been set EmptyView if adapter

Anton Malinskiy 2.7k Jul 23, 2021
A RecyclerView(advanced and flexible version of ListView in Android) with refreshing,loading more,animation and many other features.

UltimateRecyclerView Master branch: Dev branch: Project website:https://github.com/cymcsg/UltimateRecyclerView Description UltimateRecyclerView is a R

MarshalChen 7.2k Jul 31, 2021
Android library for RecyclerView to manage order of items and multiple view types.

recyclerview-binder Android Library for RecyclerView to manage order of items and multiple view types. Features Insert any items to wherever you want

Satoru Fujiwara 187 Jul 24, 2021
Android library defining adapter classes of RecyclerView to manage multiple view types

RecyclerView-MultipleViewTypeAdapter RecyclerView adapter classes for managing multiple view types Release Note [Release Note] (https://github.com/yqr

Yoshihito Ikeda 414 Jul 7, 2021
RecyclerView extension library which provides advanced features. (ex. Google's Inbox app like swiping, Play Music app like drag and drop sorting)

Advanced RecyclerView This RecyclerView extension library provides Google's Inbox app like swiping, Play Music app like drag-and-drop sorting and expa

Haruki Hasegawa 5.1k Jul 27, 2021
[UNMAINTAINED] Sticky Headers decorator for Android's RecyclerView

This project is no longer being maintained sticky-headers-recyclerview This decorator allows you to easily create section headers for RecyclerViews us

timehop 3.7k Jul 22, 2021
A very easy-to-use and non-intrusive implement of Swipe to dismiss for RecyclerView.

RecyclerViewSwipeDismiss A very easy-to-use and non-intrusive implement of Swipe to dismiss for RecyclerView. Preview How to use Add these lines to yo

xcodebuild 431 Apr 17, 2021
A very easy-to-use and non-intrusive implement of Swipe to dismiss for RecyclerView.

RecyclerViewSwipeDismiss A very easy-to-use and non-intrusive implement of Swipe to dismiss for RecyclerView. Preview How to use Add these lines to yo

xcodebuild 431 Apr 17, 2021
Epoxy is an Android library for building complex screens in a RecyclerView

Epoxy Epoxy is an Android library for building complex screens in a RecyclerView. Models are automatically generated from custom views or databinding

Airbnb 7.4k Aug 2, 2021
ANDROID. ChipsLayoutManager (SpanLayoutManager, FlowLayoutManager). A custom layout manager for RecyclerView which mimicric TextView span behaviour, flow layouts behaviour with support of amazing recyclerView features

ChipsLayoutManager This is ChipsLayoutManager - custom Recycler View's LayoutManager which moves item to the next line when no space left on the curre

Oleg Beloy 3.1k Jul 26, 2021