• Stars
    star
    235
  • Rank 171,079 (Top 4 %)
  • Language
    Java
  • License
    Apache License 2.0
  • Created almost 6 years ago
  • Updated over 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Make RecyclerView.Adapter Great Again!

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.

More Repositories

1

Matisse

๐ŸŽ† A well-designed local image and video selector for Android
Java
12,518
star
2

griffith

A React-based web video player
TypeScript
2,501
star
3

kids

Kids Is Data Stream
C++
1,225
star
4

rucene

Rust port of Lucene
Rust
1,005
star
5

cuBERT

Fast implementation of BERT inference directly on NVIDIA (CUDA, CUBLAS) and Intel MKL
C++
522
star
6

RxLifecycle

Bind observables to the lifecycle of Activity or Fragment in a non-invasive way.
Java
515
star
7

redis-shard

Redis sharding client library
Python
358
star
8

zhihu-rxjava-meetup

็ŸฅไนŽ x RxJava Meetup
358
star
9

mirror

Yet another Sketch Mirror App for Android.
Java
272
star
10

zetta

Zetta Table Store
Go
130
star
11

norm

An orm library support nGQL for Golang
Go
103
star
12

tache

A tag based invalidation caching library
Python
77
star
13

promate

Graphite On VictoriaMetrics
Go
69
star
14

SERank

An efficient and effective learning to rank algorithm by mining information across ranking candidates. This repository contains the tensorflow implementation of SERank model. The code is developed based on TF-Ranking.
Python
50
star
15

cmdb

Programmable CMDB
Go
18
star
16

chaika

Elastic cache solution on Kubernetes
11
star
17

TLLM_QMM

TLLM_QMM strips the implementation of quantized kernels of Nvidia's TensorRT-LLM, removing NVInfer dependency and exposes ease of use Pytorch module. We modified the dequantation and weight preprocessing to align with popular quantization alogirthms such as AWQ and GPTQ, and combine them with new FP8 quantization.
C++
10
star
18

zetta-proto

Protobuf files for Zetta Table Store
Shell
7
star
19

presto-connectors

Presto Connectors project has been moved to TiBigData at PingCAP Incubator
6
star
20

zetta-client-go

Go client for Zetta Table Store
Go
4
star