Giter Club home page Giter Club logo

recyclerviewhelper's Introduction

Android library that provides most common functions around recycler-view like Swipe to dismiss, Drag and Drop, Divider in the ui, events for when item selected and when not selected, on-click listener for items.

Built with ❤︎ by Nishant Srivastava and contributors


Note: Development for pre-androidx version of this library has stopped. If you are looking for pre-androidx version, then checkout this branch. Library is compatible with AndroidX version only.

Integration

RecyclerViewHelper is available in the Jcenter, so getting it as simple as adding it as a dependency

def recyclerViewVersion="{latest version}"
// Required
implementation "androidx.recyclerview:recyclerview:${recyclerViewVersion}"

// RecyclerViewHelper
implementation "com.github.nisrulz:recyclerviewhelper:x${recyclerViewVersion}"

where {latest version} corresponds to published version in Download without the prepended x. This is done to distinguish between library using andoirdx vs pre-androidx.

Usage Example:

def recyclerViewVersion="1.1.0"
// Required
implementation "androidx.recyclerview:recyclerview:${recyclerViewVersion}"

// RecyclerViewHelper
implementation "com.github.nisrulz:recyclerviewhelper:x${recyclerViewVersion}"

NOTE : The version here corresponds to the version of recyclerview dependency.

Make sure that the google's maven repo is declared in your projects build.gradle file as below
allprojects {
  repositories {
    google()
    jcenter()
  }
}

Usage

  • Implement the RHVAdapter in your recycler view adapter and RHVViewHolder in your ItemViewHolder
    public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ItemViewHolder> implements RVHAdapter {
    
         ...
    
        @Override
        public boolean onItemMove(int fromPosition, int toPosition) {
            swap(fromPosition, toPosition);
            return false;
        }
    
        @Override
        public void onItemDismiss(int position, int direction) {
            remove(position);
        }
    
        public class ItemViewHolder extends RecyclerView.ViewHolder implements RVHViewHolder {
            ...
               
            @Override
            public void onItemSelected(int actionstate) {
                System.out.println("Item is selected");
            }
    
            @Override
            public void onItemClear() {
                System.out.println("Item is unselected");
    
            }
        }
    
        // Helper functions you might want to implement to make changes in the list as an event is fired
        private void remove(int position) {
            dataList.remove(position);
            notifyItemRemoved(position);
        }
    
        private void swap(int firstPosition, int secondPosition) {
            Collections.swap(dataList, firstPosition, secondPosition);
            notifyItemMoved(firstPosition, secondPosition);
        }
    }
  • Then implement your recycler view
   public class MainActivity extends AppCompatActivity {
   
   
       RecyclerView myrecyclerview;
       ArrayList<String> data;
       MyAdapter adapter;
   
       @Override
       protected void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
           setContentView(R.layout.activity_main);
   
           myrecyclerview = (RecyclerView) findViewById(R.id.rv_fruits);
   
           data = new ArrayList<>();
           data.add("Apple");
           ...
           data.add("Fig");
   
           // Setup your adapter
           adapter = new MyAdapter(data);
           // Setup 
           myrecyclerview.hasFixedSize();
           myrecyclerview.setLayoutManager(new LinearLayoutManager(this));
           myrecyclerview.setAdapter(adapter);
   
   
           // Setup onItemTouchHandler to enable drag and drop , swipe left or right
           ItemTouchHelper.Callback callback = new RVHItemTouchHelperCallback(adapter, true, true,
                   true);
           ItemTouchHelper helper = new ItemTouchHelper(callback);
           helper.attachToRecyclerView(myrecyclerview);
   
           // Set the divider in the recyclerview
           myrecyclerview.addItemDecoration(new RVHItemDividerDecoration(this, LinearLayoutManager.VERTICAL));
   
           // Set On Click Listener
           myrecyclerview.addOnItemTouchListener(new RVHItemClickListener(this, new RVHItemClickListener.OnItemClickListener() {
               @Override
               public void onItemClick(View view, int position) {
                   String value = "Clicked Item " + data.get(position) + " at " + position;
   
                   Log.d("TAG", value);
                   Toast.makeText(MainActivity.this, value, Toast.LENGTH_SHORT).show();
               }
           }));
   
       }
   }

Demo

Walkthrough

Pull Requests

I welcome and encourage all pull requests. It usually will take me within 24-48 hours to respond to any issue or request. Here are some basic rules to follow to ensure timely addition of your request:

  1. Match coding style (braces, spacing, etc.) This is best achieved using CMD+Option+L (Reformat code) on Mac (not sure for Windows) with Android Studio defaults. This project uses a modified version of Grandcentrix's code style, so please use the same when editing this project.
  2. If its a feature, bugfix, or anything please only change code to what you specify.
  3. Please keep PR titles easy to read and descriptive of changes, this will make them easier to merge :)
  4. Pull requests must be made against develop branch. Any other branch (unless specified by the maintainers) will get rejected.
  5. Check for existing issues first, before filing an issue.
  6. Have fun!

License

Licensed under the Apache License, Version 2.0, click here for the full license.

Author & support

This project was created by Nishant Srivastava but hopefully developed and maintained by many others. See the the list of contributors here.

Special Credits to Paul Burke and his article which got me thinking

This library contains a modified version of his implementations of ItemTouchHelper.


If you appreciate my work, consider buying me a cup of ☕ to keep me recharged 🤘 [PayPal]

recyclerviewhelper's People

Contributors

nisrulz 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  avatar  avatar  avatar  avatar  avatar  avatar  avatar

recyclerviewhelper's Issues

De-selecting item

Not an issue but is there an example from this library to de-select an item?

Implement canDropOver to control where an item can be dropped


@Override
    public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder source, RecyclerView.ViewHolder target) {
        if (source.getItemViewType() != target.getItemViewType()) {
            return false;
        }

        // Notify the adapter of the move
        mAdapter.onItemMove(source.getAdapterPosition(), target.getAdapterPosition());
        return true;
    }

If source and target item types are not the same you don't want to move the item.
ItemTouchHelper.Callback docs recommends to override canDropOver(RecyclerView, ViewHolder, ViewHolder) to control where a view can be dropped.

Scroll recycler view toparticular position

Hi,

Can we set the recycler view to particular position like listView

Listview list;
list.setSelection(5);

Please add this selection item index into your libraries..

Can not build project from console

I've got the following error during gradlew build:

Error: Could not find or load main class org.gradle.wrapper.GradleWrapperMain

It looks like gradle wrapper jar file was missed in "gradle/wrapper".

Refreshing gives IndexOutOfBoundsException

BUG

What is the current behavior?
When I refresh my recyclerview and then try to swipe to dismiss an item, I get an IndexOutOfBoundsException.
If the current behavior is a bug, please provide the steps to reproduce.

I just to a refresh and then try to swipe to dismiss an item.

Any logs, error output, bugreport etc?

adapter = RVHAdapter(context, listOfFiles)
recyclerView.swapAdapter(adapter, true)
val callback = RVHItemTouchHelperCallback(adapter, false, true, true)
val helper = ItemTouchHelper(callback)
helper.attachToRecyclerView(recyclerView)

What is the expected behavior?
No IndexOutOfBoundsException
Any other comments?

What versions of software are you using?

  • Device Information:
    Google Pixel 2

  • Android Version:
    Android 9: Pie

  • Configuration Information:

  • Misc:
    Logcat:
    java.lang.IndexOutOfBoundsException: Index: 4, Size: 0
    at java.util.ArrayList.get(ArrayList.java:437)
    at com.example.ViewVideosActivity$VideoAdapter$onItemDismiss$1.invoke(ViewVideosActivity.kt:295)
    at com.example.ViewVideosActivity$VideoAdapter$onItemDismiss$1.invoke(ViewVideosActivity.kt:260)
    at org.jetbrains.anko.AsyncKt.runOnUiThread(Async.kt:34)
    at com.example.ViewVideosActivity$VideoAdapter.onItemDismiss(ViewVideosActivity.kt:291)

Could not get unknown property 'GROUP' for object of type org.gradle.api.internal.plugins.DefaultExtraPropertiesExtension.

Do you want to request a feature or report a bug?
Report a bug
What is the current behavior?

If the current behavior is a bug, please provide the steps to reproduce.

I cloned the project from github, but I am unable to build it. It always gives error "Could not get unknown property 'GROUP' for object of type org.gradle.api.internal.plugins.DefaultExtraPropertiesExtension."

Any logs, error output, bugreport etc?

What is the expected behavior?
The code should build
Any other comments?

What versions of software are you using?

  • Device Information:

  • Android Version:

  • Configuration Information:

  • Misc:

set button in recyclerview

i tried setOnClickListener in onBindViewHolder and ItemViewHolder...
but it happened nothing when i clicked.
should i use different way to set button in a row in recyclerView?
may i see some example?
even i tried in activty not adapter, it shows same.
and how can i change drag clickevent recyclerview to imageview or button?
thank you.

Allow swipe to dismiss to be cancelled from within the adapter

I have a view in the recyclerview that I'd like to be undismissable. While I'd prefer that I have an option to disable swipe to dismiss for particular views, I'd be okay with the swiped-away view bouncing back in if onItemDismiss returns false.

Currently onItemDismiss does not return anything. Maybe it could be made to return a boolean like onItemMove to indicate if the view should be dismissed or not.

📖 Guest Book 🙇🏻‍♂️

This "issue" serves as a guest book. You're welcome to write nice things about this project, share how it's helped you out, or name drop companies using it.

Idea came from this tweet.

Swipe to dismiss doesn't remove the card permanently

I am using your swipe to dismiss feature and it removes the card and even deletes the object from the list but messes up with the animation.

If I delete the card there is just a big grey space where it was and if I scroll down and then back up again the old card is still there where the gray space was but it is deleted from the actual list.

searchList.remove(position);
notifyItemRemoved(position);

Thats my code on item dismiss

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.