RecyclerView-Multi-Type

简介:A simple recyclerview multi type for Android
更多:作者   提 Bug   
标签:
recyclerview-multi-recyclerview-multi-recyclerview-multi-recyclerview-multi-

A grace recyclerview multi type for Android

Screenshots

Gradle

Add it in your root build.gradle at the end of repositories:


    allprojects {
        repositories {
            ...
            maven { url 'https://jitpack.io' }
        }
    }

Add the dependency:


    dependencies {
             compile 'com.github.ruzhan123:RecyclerView-Multi-Type:v1.0'
    }

Usage

1, init adapter.


    GridLayoutManager manager = new GridLayoutManager(this, MultiAdapter.SPAN_SIZE_MAX);
    manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
      @Override public int getSpanSize(int position) {
        return mMultiAdapter.getSpanSize(position);
      }
    });

    //1. bean must extends MultiType
    List<OneTestBean> oneList = TestDataHelper.addOneTestBean();

    //2.set list model type and span size, param: list, type, spanSize
    oneList =MultiTypeHelper.setMultiTypeList(oneList, MultiAdapter.TYPE_01, MultiAdapter.SPAN_SIZE_MAX);

    //3. add list<Object>
    mData.addAll(oneList);

    mMultiAdapter.setData(mData);

2, only needs onCreateViewHolder,so you can forget other method.


    public class MultiAdapter extends BaseMultiAdapter {

    public static final int SPAN_SIZE_MAX = 12;

    public static final int TYPE_01 = 10001;
    public static final int TYPE_02 = 10002;
    public static final int TYPE_03_A = 10003;
    public static final int TYPE_03_B = 10004;
    public static final int TYPE_04 = 10005;
    public static final int TYPE_LOAD_MORE = 10006;

    @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    if (viewType == TYPE_01) {
      return new OneMultiHolder(
          LayoutInflater.from(parent.getContext()).inflate(R.layout.holder01, parent, false));

    } else if (viewType == TYPE_02) {
      return new TwoMultiHolder(
          LayoutInflater.from(parent.getContext()).inflate(R.layout.holder02, parent, false));

    } else if (viewType == TYPE_03_A) {
      return new ThreeAMultiHolder(
          LayoutInflater.from(parent.getContext()).inflate(R.layout.holder03a, parent, false));

    } else if (viewType == TYPE_03_B) {
      return new ThreeBMultiHolder(
          LayoutInflater.from(parent.getContext()).inflate(R.layout.holder03b, parent, false));

    } else if (viewType == TYPE_04) {
      return new FourMultiHolder(
          LayoutInflater.from(parent.getContext()).inflate(R.layout.holder04, parent, false));

    } else {
      return new LoadMoreHolder(
          LayoutInflater.from(parent.getContext()).inflate(R.layout.load_more, parent, false));
    }
    }
    }

3,ok,That's all.

Library Class

Developed by

ruzhan - dev19921116@gmail.com

License

Copyright 2017 ruzhan

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.
Android 开发经验分享
Android 开发经验分享