GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - dxydeveloper/MultiType: An Android library to create multiple item types list views easily and flexibly

BranchesTags
Open more actions menu
 
 

Latest commit

 

History

317 Commits
317 Commits

Folders and files

NameName
Last commit message
Last commit date
.github
.github
 
 
art
art
 
 
gradle/wrapper
gradle/wrapper
 
 
library-ktx
library-ktx
 
 
library
library
 
 
sample
sample
 
 
.gitignore
.gitignore
 
 
.travis.yml
.travis.yml
 
 
LICENSE
LICENSE
 
 
README.md
README.md
 
 
build.gradle
build.gradle
 
 
checkstyle.xml
checkstyle.xml
 
 
findbugs.xml
findbugs.xml
 
 
gradle-mvn-push.gradle
gradle-mvn-push.gradle
 
 
gradle.properties
gradle.properties
 
 
gradlew
gradlew
 
 
gradlew.bat
gradlew.bat
 
 
settings.gradle
settings.gradle
 
 

Repository files navigation

MultiType

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

Build Status License maven-central jetbrains-plugin

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 those types. Once we need to add a new item type, we have to go to the original adapter file and modify some old codes carefully, and these adapter classes will get more complicated.

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

Getting started

In your build.gradle:

MultiType has been rebuilt based on androidx. If you are still using the android support library, please use multitype:3.4.4 and multitype-kotlin:3.4.4.

dependencies {
    implementation 'me.drakeet.multitype:multitype:3.5.0'
}

Optional: Extension for Kotlin:

implementation 'me.drakeet.multitype:multitype-ktx:3.5.0'

Usage

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

public class TextItem {

    public final @NonNull String text;

    public TextItem(@NonNull 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 {
    
        private @NonNull 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;
    private Items items;

    @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);

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

        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 many:

adapter.register(Data.class).to(
    new DataType1ViewBinder(),
    new DataType2ViewBinder()
).withLinker((position, data) ->
    data.type == Data.TYPE_2 ? 1 : 0
);
adapter.register(Data.class).to(
    new DataType1ViewBinder(),
    new DataType2ViewBinder()
).withClassLinker((position, data) -> {
    if (data.type == Data.TYPE_2) {
        return DataType2ViewBinder.class;
    } else {
        return DataType1ViewBinder.class;
    }
});

More methods that you can override from ItemViewBinder:

protected long getItemId(@NonNull T item)
protected void onViewRecycled(@NonNull VH holder)
protected boolean onFailedToRecycleView(@NonNull VH holder)
protected void onViewAttachedToWindow(@NonNull VH holder)
protected void onViewDetachedFromWindow(@NonNull VH holder)

Kotlin:

MultiTypeAdapter

  • Added register(binder: ItemViewBinder<T, *>)
  • Added register(clazz: KClass<out T>, binder: ItemViewBinder<T, *>)
  • Added register(clazz: KClass<out T>): OneToManyFlow<T>

TypePool

  • Added register(clazz: KClass<out T>, binder: ItemViewBinder<T, *>, linker: Linker<T>)
  • Added unregister(clazz: KClass<out T>)
  • Added firstIndexOf(clazz: KClass<out T>)

OneToManyEndpoint

  • Added withKClassLinker(classLinker: KClassLinker<T>)
  • Added withKClassLinker(classLinker: (position: Int, t: T) -> KClass<out ItemViewBinder<T, *>>)

Android Studio Plugin

  • drakeet/MultiTypeTemplates

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

Screenshots

Pages created with MultiType:

Wiki

Change Log

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

License

Copyright 2016-2018 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.

About

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

Resources

Readme
Apache-2.0 license

Contributing

Contributing
Activity

Stars

0 stars

Watchers

0 watching

Forks

0 forks
Report repository

Releases

Packages

Contributors

Languages

You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.