GithubHelp home page GithubHelp logo

moezbhatti / recycler-adapter Goto Github PK

View Code? Open in Web Editor NEW
6.0 3.0 1.0 81 KB

A convenience implementation of RecyclerView.Adapter<>

Home Page: http://moezbhatti.com

License: Apache License 2.0

Java 100.00%

recycler-adapter's Introduction

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

public class SimpleViewHolder extends MBViewHolder<DataType> {

    // Declare your views
    protected TextView mTextView;
    ...
    
    public class SimpleViewHolder(MBAdapter<DataType> 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

public class SimpleAdapter extends MBAdapter<DataType> {

    public SimpleAdapter(Context context) {
        super(context);
    }
    
    @Override
    public MBViewHolder<DataType> 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

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

setItems(ArrayList<DataType> data);

void addItems(ArrayList<DataType> 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<DataType> 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

repositories {
  maven { url "https://jitpack.io" }
}
dependencies {
  compile 'com.github.moezbhatti:recycler-adapter:0.1.2'
}

Maven

<repositories>
  	<repository>
      <id>jitpack.io</id>
      <url>https://jitpack.io</url>
	</repository>
</repositories>
<dependency>
    <groupId>com.github.moezbhatti</groupId>
    <artifactId>recycler-adapter</artifactId>
    <version>0.1.2</version>
</dependency>

License

Copyright 2016 Moez Bhatti

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.

recycler-adapter's People

Contributors

moezbhatti avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

enterstudio

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.