https://github.com/moezbhatti/recycler-adapter
A convenience implementation of RecyclerView.Adapter<>
https://github.com/moezbhatti/recycler-adapter
Last synced: 6 months ago
JSON representation
A convenience implementation of RecyclerView.Adapter<>
- Host: GitHub
- URL: https://github.com/moezbhatti/recycler-adapter
- Owner: moezbhatti
- License: apache-2.0
- Created: 2016-08-07T06:06:14.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2016-08-10T02:51:26.000Z (about 9 years ago)
- Last Synced: 2025-04-16T02:11:41.262Z (6 months ago)
- Language: Java
- Homepage: http://moezbhatti.com
- Size: 79.1 KB
- Stars: 6
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# moezbhatti/recycler-adapter
Forget the boilerplate that comes with writing Adapters. `recyclerview-adapter` is very small and lightweight library to set up `RecyclerViews` quickly and easily
Just by setting the data type that you're representing in your list, as a Type argument when creating your adapter, the `MBAdapter` will create and maintain a list to hold your data for you, and automatically update the `RecyclerView` when you add/remove/replace data. It will also keep track of a `selected` state (useful when implementing multi-selection), and re-bind the `MBViewHolder` whenever the state changes
## Usage
Create a class for your ViewHolder
``` java
public class SimpleViewHolder extends MBViewHolder {// Declare your views
protected TextView mTextView;
...
public class SimpleViewHolder(MBAdapter adapter, View view) {
super(adapter, view);// Find your views. I highly recommend using ButterKnife instead of doing it this way
mTextView = (TextView) view.findViewById(R.id.text);
...
}
@Override
public void bind(int position) {
// Get your data object from the adapter
DataType data = mAdapter.getItem(position);
// Now bind data to your views
mTextView.setText(data.getName());
...
}
}```
Now, you just need to create a simple class for your adapter
``` java
public class SimpleAdapter extends MBAdapter {public SimpleAdapter(Context context) {
super(context);
}
@Override
public MBViewHolder onCreateViewHolder(LayoutInflater inflater, ViewGroup parent) {
// Inflate your view
View view = inflater.inflate(R.layout.list_item_simple, parent, false);
// Return a new instance of your ViewHolder
return new SimpleViewHolder(this, view);
}}
```Now you can use it with your `RecyclerView`
``` java
SimpleAdapter adapter = new SimpleAdapter(mContext);mRecyclerView.setAdapter(adapter);
```And here are some of the methods that you can use without having to write all of the usual boilerplate
``` java
setItems(ArrayList data);void addItems(ArrayList data);
void addItem(DataType data);
void addItem(int position, DataType data);
void replaceItem(int position, DataType data);
void removeItem(DataType data);
void removeItem(int position);
void clear();
void setSelected(int position);
boolean isSelected(int position);
DataType getItem(int position);
ArrayList getItems();
```
## To-do
- Support for `onClick` and `onLongClick` at the Adapter level. I personally don't use these methods like this, but it's nice functionality to have for people who do
- Support for multiple view types in a single adapter
- Support for easily adding headers and footers
- If you have any suggestions, create an issue and I'll definitely take them into consideration!## Installation
**Gradle**
``` gradle
repositories {
maven { url "https://jitpack.io" }
}
`````` gradle
dependencies {
compile 'com.github.moezbhatti:recycler-adapter:0.1.2'
}
```**Maven**
``` xml
jitpack.io
https://jitpack.io
```
``` xml
com.github.moezbhatti
recycler-adapter
0.1.2```
## License
```
Copyright 2016 Moez BhattiLicensed 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 athttp://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.
```