Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/nisrulz/recyclerviewhelper

:page_with_curl: [Android Library] Giving powers to RecyclerView
https://github.com/nisrulz/recyclerviewhelper

android android-library divide drag drop helper recyclerview swipe

Last synced: 7 days ago
JSON representation

:page_with_curl: [Android Library] Giving powers to RecyclerView

Awesome Lists containing this project

README

        







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.























































Also featured in Awesome Android Newsletter #Issue 21


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.](https://github.com/nisrulz/recyclerviewhelper/tree/archive/pre-androidx)
> 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
```gradle
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](https://api.bintray.com/packages/nisrulz/maven/com.github.nisrulz%3Arecyclerviewhelper/images/download.svg) ](https://bintray.com/nisrulz/maven/com.github.nisrulz%3Arecyclerviewhelper/_latestVersion) without the prepended `x`.
This is done to distinguish between library using andoirdx vs pre-androidx.

Usage Example:
```gradle
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

```gradle
allprojects {
repositories {
google()
jcenter()
}
}
```

# Usage
+ Implement the `RHVAdapter` in your recycler view adapter and `RHVViewHolder` in your ItemViewHolder
```java

public class MyAdapter extends RecyclerView.Adapter 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
```java

public class MainActivity extends AppCompatActivity {


RecyclerView myrecyclerview;
ArrayList 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](img/walkthrough1.gif)

# 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](https://github.com/nisrulz/AndroidCodeStyle/tree/nishant-config), 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](https://github.com/nisrulz/recyclerviewhelper/issues) first, before filing an issue.
6. Have fun!

## License
Licensed under the Apache License, Version 2.0, [click here for the full license](/LICENSE.txt).

## Author & support
This project was created by [Nishant Srivastava](https://github.com/nisrulz/nisrulz.github.io#nishant-srivastava) but hopefully developed and maintained by many others. See the [the list of contributors here](https://github.com/nisrulz/recyclerviewhelper/graphs/contributors).

>Special Credits to Paul Burke and his [article](https://medium.com/@ipaulpro/drag-and-swipe-with-recyclerview-b9456d2b1aaf) which got me thinking
>
>This library contains a modified version of his implementations of ItemTouchHelper.

If you appreciate my work, consider [buying me](https://www.paypal.me/nisrulz/5usd) a cup of :coffee: to keep me recharged :metal: [[PayPal](https://www.paypal.me/nisrulz/5usd)]