Giter Club home page Giter Club logo

recyclerview-binder's Introduction

recyclerview-binder

License Android Arsenal Download

Android Library for RecyclerView to manage order of items and multiple view types.

Features

  • Insert any items to wherever you want without calculating their position.
  • Insert any items in desired order regardless of the order of calling 'add/insert'.
  • Separate implementations for each view type into their own classes.
  • Support RxJava.

Sample

Sample

Gradle

repositories {
    jcenter()
}
dependencies {
    compile 'jp.satorufujiwara:recyclerview-binder:1.3.2'
}

Quick Start

ViewType decides RecyclerView item's layout.

public enum BinderSampleViewType implements ViewType {

    VIEW_TYPE_1,
    VIEW_TYPE_2,
    VIEW_TYPE_3;

    @Override
    public int viewType() {
        return ordinal();
    }
}

Binder binds data to Views in RecyclerView item.

public class DataBinder1 extends RecyclerBinder<BinderSampleViewType> {

    private final String text;

    public DataBinder1(Activity activity, String text) {
        super(activity, BinderSampleViewType.VIEW_TYPE_1);
        this.text = text;
    }

    @Override
    public int layoutResId() {
        return R.layout.binder_data_1;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(View v) {
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
        ViewHolder holder = (ViewHolder) viewHolder;
        holder.textSection.setText(text);
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView textSection;
        public ViewHolder(View view) {
            super(view);
            textSection = (TextView) view.findViewById(R.id.text_section);
        }
    }
}

Binder added to Adapter is layouted in order of Section.

    RecyclerBinderAdapter<BinderSampleSection, BinderSampleViewType> adapter
            = new RecyclerBinderAdapter<>();

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.setAdapter(adapter);

        adapter.add(BinderSampleSection.SECTION_1, new DataBinder1(getActivity(), "in Section1"));
        adapter.add(BinderSampleSection.SECTION_1, new DataBinder2(getActivity(), "in Section1"));
        adapter.add(BinderSampleSection.SECTION_1, new DataBinder3(getActivity(), "in Section1"));

        adapter.add(BinderSampleSection.SECTION_3, new DataBinder2(getActivity(), "in Section3"));
        adapter.add(BinderSampleSection.SECTION_3, new DataBinder1(getActivity(), "in Section3"));

        adapter.add(BinderSampleSection.SECTION_2, new DataBinder3(getActivity(), "in Section2"));
        adapter.add(BinderSampleSection.SECTION_2, new DataBinder2(getActivity(), "in Section2"));
        adapter.add(BinderSampleSection.SECTION_2, new DataBinder1(getActivity(), "in Section2"));
    }

    enum BinderSampleSection implements Section {

        SECTION_1,
        SECTION_2,
        SECTION_3;
        
        @Override
        public int position() {
            return ordinal();
        }
    }

Usage

1. Create class implemented ViewType

Enum is useful for this class.

2. Create classes extends RecyclerBinder

The number of these classes is the same to number of ViewType in Step 1.

3. Create class implemented Section

Enum is useful for this class.

RecyclerBinderAdapter<Section, ViewType> adapter = new RecyclerBinderAdapter<>();

5. Control RecyclerView

adapter.add(Section.SECTION_1, binder);
adapter.addIfEmpty(Section.SECTION_1, binder);
adapter.addAll(Section.SECTION_1, List<RecyclerBinder> binders);
adapter.insert(Section.SECTION_1, binder, index);
adapter.remove(Section.SECTION_1, binder);
adapter.removeAll(Section.SECTION_1);
adapter.replaceAll(Section.SECTION_1, List<RecyclerBinder> binders);
adapter.clear();

Use with RxJava

If subscibe observable in RecyclerBinder classes, use RxRecyclerBinder and bindToLifecycle().

myObservable
    .compose(bindToLifecycle())
    .subscribe();

When item removed from adapter, observable unsubscribe. Note : Due to the unsubscription works, please call RecyclerBinderAdapter.clear() in Activity.onDestroy() or Fragment.onDestroyView().

License

Copyright 2015 Satoru Fujiwara

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.

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.