Coder Social home page Coder Social logo

recyclerviewenhanced's Introduction

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

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
                  }
             }
         });
    

Additional Functionality

  • 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.

recyclerviewenhanced's People

Contributors

nikhilpanju 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.