Giter Site home page Giter Site logo

multitype's Introduction

MultiType

An Android library to create multiple item types list views easily and flexibly.

Build Status License maven-central

English Version | 《Android 复杂的列表视图新写法 · 详解篇》

Previously, when we need to develop a complex RecyclerView/ListView, it is a difficult and troublesome work. We should override the getItemViewType of RecyclerView.Adapter , add some types, and create some ViewHolders relating to the types. The process is cumbersome.

Once we need to add a new item type, we have to go to the original adapter and modify some old codes, and adapter classes will be more bulky.

Today, I create a new intuitive and flexible way to easily create the complex RecyclerView/ListView, with the MultiType library, we could insert a new item type without changing the old adapter codes and make them more readable.

Getting started

In your build.gradle:

dependencies {
    compile 'me.drakeet.multitype:multitype:3.0.0'
}

Usage

Step 1. Create a class, It would be your data model/Java bean, for example:

public class TextItem {

    @NonNull public String text;

    public TextItem(@NonNull final String text) {
        this.text = text;
    }
}

Step 2. Create a class extends ItemViewBinder<T, VH extends ViewHolder>, for example:

public class TextItemViewBinder extends ItemViewBinder<TextItem, TextItemViewBinder.TextHolder> {

    static class TextHolder extends RecyclerView.ViewHolder {
        @NonNull private final TextView text;

        TextHolder(@NonNull View itemView) {
            super(itemView);
            this.text = (TextView) itemView.findViewById(R.id.text);
        }
    }

    @NonNull @Override
    protected TextHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View root = inflater.inflate(R.layout.item_text, parent, false);
        return new TextHolder(root);
    }

    @Override
    protected void onBindViewHolder(@NonNull TextHolder holder, @NonNull TextItem textItem) {
        holder.text.setText("hello: " + textItem.text);
        Log.d("demo", "position: " + getPosition(holder));
        Log.d("demo", "adapter: " + getAdapter());
    }
}

Step 3. Just register your types and setup your RecyclerView and List<Object> in your Activity, for example:

public class SampleActivity extends AppCompatActivity {

    private MultiTypeAdapter adapter;

    @Override 
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.list);

        adapter = new MultiTypeAdapter();
        adapter.register(TextItem.class, new TextItemViewBinder());
        adapter.register(ImageItem.class, new ImageItemViewBinder());
        adapter.register(RichItem.class, new RichItemViewBinder());
        recyclerView.setAdapter(adapter);

        /* Mock the data */
        TextItem textItem = new TextItem("world");
        ImageItem imageItem = new ImageItem(R.mipmap.ic_launcher);
        RichItem richItem = new RichItem("小艾大人赛高", R.mipmap.avatar);

        Items items = new Items();
        for (int i = 0; i < 20; i++) {
            items.add(textItem);
            items.add(imageItem);
            items.add(richItem);
        }
        adapter.setItems(items);
        adapter.notifyDataSetChanged();
    }
}

That's all, you're good to go!

Advanced usage

One to Multiple:

Wiki

Change Log

https://github.com/drakeet/MultiType/releases

Android Studio Plugin

An intellij idea plugin for Android to generate MultiType Item and ItemViewBinder easily.

Sample screenshots

You could check the sample module for more details and after running it will look like:

And it has been used in drakeet/TimeMachine:

License

Copyright 2017 drakeet.

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.

multitype's People

Contributors

drakeet avatar hamberluo avatar feicien avatar

Watchers

James Cloos 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.