/SugarAdapter

Make RecyclerView.Adapter Great Again!

Primary LanguageJavaApache License 2.0Apache-2.0

SugarAdapter

Make RecyclerView.Adapter Great Again!

Support API v14+

中文介绍

Introduction

When we use RecyclerView, we need to extends Adapter and override some methods, which are so boring:

  • getItemCount()
  • getItemViewType()
  • onCreateViewHolder()
  • onBindViewHolder()
  • onViewAttachedToWindow()
  • onViewDetachedFromWindow()
  • onViewRecycled()
  • onAttachedToRecyclerView()
  • onDetachedFromRecyclerView()
  • onFailedToRecycleView()

More over, we usually need View's lifecycle callback,

such as onViewAttachedToWindow() and onViewDetachedFromWindow().

Now, we use annotationProcessor to reduce your boring Adapter code, add some features with your ViewHolder.

An usage sample see this link.

SugarAdapter

You don't need to extends and override any code of Adapter, just write few lines of builder code:

mAdapter = SugarAdapter.Builder.with(mList) // eg. List<Object>
        .add(FooHolder.class) // extends SugarHolder<Foo>
        .add(BarHolder.class, new SugarHolder.OnCreatedCallback<BarHolder>() { // extends SugarHolder<Bar>
            @Override
            public void onCreated(@NonNull BarHolder holder) {
                // holder.SETTER etc.
            }
        })
        .preInflate(true) // preInflate ViewHolders' XML for smooth scrolling
        .build();
mRecyclerView.setAdapter(mAdapter);

// mAdapter.notifyItem* or mAdapter.notifyDataSetChanged()
// mAdapter.setExtraDelegate() with onAttachedToRecyclerView()/onDetachedFromRecyclerView()

That's all!

SugarHolder

Layout - ViewType - Data, trinity, so we must extends SugarHolder as below:

// FooHolder is public and final by default;
// if you want to extends FooHolder, see demo
@Layout(R.layout.foo) 
public final class FooHolder extends SugarHolder<Foo> {
    // If you don't want to write findViewById(), 
    // just annotate view with @Id(), and make it **public**
    @Id(R.id.text)
    public TextView mTextView;

    public FooHolder(@NonNull View view) {
        super(view);
    }

    @Override
    pubilc void onBindData(@NonNull Foo foo) {
        mTextView.setText(foo.getText());
    }
}

SugarHolder also has some methods which you may want to use or override:

  • getAdapter() // get adapter instance directly in holder, perhaps it's not a good design for your code
  • getData() // get data instance directly in holder, so you don't to hold data by yourself
  • getLifecycle() // support lifecycle-aware components
  • getContext() // get context instance directly in holder, cool
  • getColor()
  • getDrawable()
  • getString()
  • getRootView()
  • findViewById()
  • dp2px()
  • sp2px()
  • onViewAttachedToWindow()
  • onViewDetachedFromWindow()
  • onViewRecycled()
  • onFailedToRecycleView()

Now you can use ViewHolder easily.

Gradle

dependencies {
    // migrate to AndroidX, use 1.8.16
    implementation 'com.zhihu.android:sugaradapter:1.7.16'
    annotationProcessor 'com.zhihu.android:sugaradapter-processor:1.7.16'
}

For Android Library

First, add the ButterKnife's plugin to your project.

Second, in your module's build.gradle:

android {
    defaultConfig {
        javaCompileOptions {
            annotationProcessorOptions {
                arguments = [moduleNameOfSugarAdapter: 'YOUR_MODULE_NAME']
            }
        }
    }
}

dependencies {
    // migrate to AndroidX, use 1.8.16
    implementation 'com.zhihu.android:sugaradapter:1.7.16'
    annotationProcessor 'com.zhihu.android:sugaradapter-processor:1.7.16'
}

Third, in your main project's build.config:

android {
    defaultConfig {
        javaCompileOptions {
            annotationProcessorOptions {
                arguments = [subModulesOfSugarAdapter: 'YOUR_MODULE_NAME_1, YOUR_MODULE_NAME_...']
            }
        }
    }
}

dependencies {
    // migrate to AndroidX, use 1.8.16
    implementation 'com.zhihu.android:sugaradapter:1.7.16'
    annotationProcessor 'com.zhihu.android:sugaradapter-processor:1.7.16'
}

The main project must have at least one subclass of SugarHolder with @Layout to toggle AnnotationProcessor.

Finally, use R2.layout.* and R2.id.* to replace R.layout.* and R.id.*, for example, @Layout(R2.layout.foo)

Note: we don't depend the ButterKnife in our library, we just need it's gradle plugin to generate R2.java

Thanks

License

Copyright 2018 Zhihu Inc.

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.