Giter VIP home page Giter VIP logo

simplerecycleradapter's Introduction

SimpleRecyclerAdapter

Writing a RecyclerView adapter is a lot of boiler-plate code that is doing the same thing in most cases. This library provides you an universal SimpleRecyclerAdapter, which you can use directly. You can bind any list of arbitrary objects. The adapter was implemented with performance in mind. No new objects are created during the binding process (except for the first visible rows and then the objects are reused).

How to implement

Very easy, to create a new RecyclerViewAdapter, you only need to make an instance of our SimpleRecyclerAdapter (you don't need to extend this class):

mRecyclerAdapter = new SimpleRecyclerAdapter<>(mOnClickListener, new SimpleRecyclerAdapter.CreateViewHolder<MyDataObject>() {
        @Override
        public SettableViewHolder<MyDataObject> onCreateViewHolder(final ViewGroup parent, final int viewType) {
            return new MyDataViewHolder(BasicAdapterActivity.this, R.layout.item_mydata, parent);
        }
});

Then create a standard ViewHolder, extending SettableViewHolder. You only need to implement the setData() method.

public class MyDataViewHolder extends SettableViewHolder<MyDataObject> {

    // constructor omitted

    @Override
    public void setData(@NonNull MyDataObject data) {
        mTitle.setText(data.getTitle());
        mDescription.setText(data.getText());
    }
}

There is also support for multiple clickable areas (see AdvancedDataViewHolder example), multiple view-types, long-press.

Advanced use-case

Take a look at the sample AdvancedAdapterActivity.

The code to achieve this:

@Override
public void onItemClick(@NonNull WrappedMyDataObject item, @NonNull SettableViewHolder<WrappedMyDataObject> viewHolder, @NonNull View view) {
    if (item.getType() == WrappedMyDataObject.ITEM_TYPE_NORMAL) {
        MyDataObject dataObject = item.getDataObject();
        int itemPos = viewHolder.getAdapterPosition();

        switch (view.getId()) {
            case R.id.btn_more:
                setTitle("Action clicked on item: " + dataObject.getTitle());
                break;
            case R.id.btn_remove:
                mRecyclerAdapter.removeItem(item, true);
                break;
            case R.id.btn_move_up:
                mRecyclerAdapter.swapItem(itemPos, Math.max(0, itemPos - 1), true);
                break;
            case R.id.btn_move_down:
                int maxIndex = mRecyclerAdapter.getItemCount() - 1;
                mRecyclerAdapter.swapItem(itemPos, Math.min(maxIndex, itemPos + 1), true);
                break;
            default:
                //Actual item click
                setTitle("Last clicked item: " + dataObject.getTitle());
                break;
        }
    }
}

Download

Grab via Gradle:

compile 'eu.inloop:simplerecycleradapter:0.3.1'

simplerecycleradapter's People

Contributors

yuraj11 avatar feromakovi avatar

Watchers

James Cloos avatar dev.with 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.