Code Monkey home page Code Monkey logo

lego's Introduction

Lego

A powerful Android library for building complex RecyclerView. Building a list is as simple as playing Lego game.

List<Model> =>Lego=> List<View>. 像搭积木一样构建你的RecyclerView列表。

中文介绍

What Lego does

When you need to build a RecyclerView which contains different types of ViewHolder, you may always need to maintain a huge Adapter and calculate which type should be displayed on each position.

Now, forget those different types and viewTypeOfPosition() calculation in your terriable Adapter.

When using Lego, you just need to do:

1. Build a empty List<Object>;
2. Put all your data into this List<Object>;
3. Take this List<Object> to RecyclerView. 

It's done! RecyclerView will auto find the ViewHolder based on List and display.

How to use Lego

For example, we have three viewHolders to display in a RecyclerView: AppleViewHolder, OrangeViewHolder, BananaViewHolder. Their own data class are Apple, Orange, Banana.

You have two ways to build with Lego.

1. Use String Id to identify each ViewHolder

@LegoViewHolder(id = "apple_view_holder")
class AppleViewHolder implements ILegoViewHolder {
    @Override
    public View initView(ViewGroup parent) { ... }

    @Override
    public void bindData(Object data, int position, @Nullable Bundle argument) { ... }
}

@LegoViewHolder(id = "orange_view_holder")
class OrangeViewHolder implements ILegoViewHolder {
    @Override
    public View initView(ViewGroup parent) { ... }

    @Override
    public void bindData(Object data, int position, @Nullable Bundle argument) { ... }
}

@LegoViewHolder(id = "banana_view_holder")
class BananaViewHolder implements ILegoViewHolder {
    @Override
    public View initView(ViewGroup parent) { ... }

    @Override
    public void bindData(Object data, int position, @Nullable Bundle argument) { ... }
}

That's it! In your RecyclerView, you can do as below:

RecyclerView recyclerView = findViewById(R.id.recycler_view);

// 1. create a LegoRecyclerAdapter
LegoRecyclerAdapter adapter = new LegoRecyclerAdapter();

// 2. create a data list of Object
List<Object> data = new ArrayList<>();

// 3. add LegoItem into data
data.add(LegoItem.create("apple_view_holder"));
data.add(LegoItem.create("orange_view_holder"));
data.add(LegoItem.create("banana_view_holder"));

// 4. It's done
adapter.swapData(data);
recyclerView.setAdapter(adapter)

Run this code, the RecyclerView will show these three viewHolders, try it or run our sample!

2. Simpler Way: use Data object to identify each ViewHolder

@LegoViewHolder(bean = Apple.class)
class AppleViewHolder implements ILegoViewHolder {
    @Override
    public View initView(ViewGroup parent) { ... }

    @Override
    public void bindData(Object data, int position, @Nullable Bundle argument) { 
      Apple apple = (Apple) data;
      ...
    }
}

@LegoViewHolder(bean = Orange.class)
class OrangeViewHolder implements ILegoViewHolder {
    @Override
    public View initView(ViewGroup parent) { ... }

    @Override
    public void bindData(Object data, int position, @Nullable Bundle argument) { 
      Orange orange = (Orange) data;
      ...
    }
}

@LegoViewHolder(bean = Banana.class)
class BananaViewHolder implements ILegoViewHolder {
    @Override
    public View initView(ViewGroup parent) { ... }

    @Override
    public void bindData(Object data, int position, @Nullable Bundle argument) {   
      Banana orange = (Banana) data;
      ...
    }
}

In your RecyclerView, do as below:

RecyclerView recyclerView = findViewById(R.id.recycler_view);

// 1. create a LegoRecyclerAdapter
LegoRecyclerAdapter adapter = new LegoRecyclerAdapter();

// 2. create a data list of Object
List<Object> data = new ArrayList<>();

// 3. add LegoItem into data
data.add(new Apple());
data.add(new Orange());
data.add(new Banana());

// 4. It's done
adapter.swapData(data);
recyclerView.setAdapter(adapter)

Bingo! This will display same three ViewHolders as above, but this way is easier to use.

Get LegoViewHolder instance

adapter.setOnLegoViewHolderListener(new OnLegoViewHolderListener() {
    @Override
    public void onCreate(@NonNull ILegoViewHolder viewHolder) {
        if (viewHolder instanceof AppleViewHolder) {
            makeToast("Create AppleViewHolder");
        } else if (viewHolder instanceof OrangeViewHolder) {
            makeToast("Create OrangeViewHolder");
        }
    }
});

ViewHolder cache

If you know your page need at least 2 AppleViewHolder and 3 OrangeViewHolder, you can tell Lego to generate ViewHolder instance and inflate. When data(maybe from server) arrives, you can display immediately and no need to do the ViewHolder Inflate.

LegoCache legoCache =
    new LegoCache.Builder(recyclerView)
    .cache(OrangeViewHolder.class, 2)
    .cache(AppleViewHolder.class, 2)
    .build();
adapter.setLegoCache(legoCache);

Sample

Here is our sample code you can try.

Install

implementation 'com.wingjay.lego:library:0.9.0'
annotationProcessor 'com.wingjay.lego:lego-processor:0.9.0''

if you use Kotlin, use as below

implementation 'com.wingjay.lego:library:0.9.0'
kapt 'com.wingjay.lego:lego-processor:0.9.0''

lego's People

Contributors

wingjay avatar flyfire avatar

Watchers

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