All Projects → zhihu → Sugaradapter

zhihu / Sugaradapter

Licence: apache-2.0
Make RecyclerView.Adapter Great Again!

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to Sugaradapter

Easyadapter
This project demonstrates simple approach for implementation complex lists, based on RecyclerView.
Stars: ✭ 187 (-18.34%)
Mutual labels:  recyclerview
Flap
Flap(灵动),一个基于 RecyclerView 的页面组件化框架。
Stars: ✭ 204 (-10.92%)
Mutual labels:  recyclerview
Async Expandable List
Stars: ✭ 221 (-3.49%)
Mutual labels:  recyclerview
Diffadapter
A high-performance , easy-to-use Adapter for RecyclerView ,using diffutil
Stars: ✭ 193 (-15.72%)
Mutual labels:  recyclerview
Tabscrollattacher
Attach TabLayout and RecyclerView. Useful for categorizing RecyclerView items.
Stars: ✭ 196 (-14.41%)
Mutual labels:  recyclerview
Licenseadapter
adapter for RecyclerView to display app's oss dependencies' license
Stars: ✭ 209 (-8.73%)
Mutual labels:  recyclerview
Sherlockadapter
一个万能的封装了RecyclerView.Adapter的功能库。
Stars: ✭ 186 (-18.78%)
Mutual labels:  recyclerview
Gridpagersnaphelper
A powerful tools to impl grid paging layout by RecyclerView
Stars: ✭ 228 (-0.44%)
Mutual labels:  recyclerview
Smilerefresh
微笑下拉刷新。这是在 SwipeRefreshLayout基础上修改的下拉刷新库。
Stars: ✭ 203 (-11.35%)
Mutual labels:  recyclerview
Animatedrecyclerview
RecyclerView with layout animations
Stars: ✭ 220 (-3.93%)
Mutual labels:  recyclerview
Shimmer Recyclerview X
🌀 ShimmerRecyclerViewX for AndroidX
Stars: ✭ 193 (-15.72%)
Mutual labels:  recyclerview
Advancedrecyclerview
An easy, empowering Kotlin library for RecyclerView
Stars: ✭ 197 (-13.97%)
Mutual labels:  recyclerview
Recycler View Margin Decoration
A library for add margin each item in RecyclerView.
Stars: ✭ 217 (-5.24%)
Mutual labels:  recyclerview
Moretype
new method to build data in RecyclerView with Kotlin!
Stars: ✭ 189 (-17.47%)
Mutual labels:  recyclerview
Lrecyclerview
RecyclerView下拉刷新,自动加载更多;仿IOS侧滑Item删除菜单(盼望大家扩展更多功能)
Stars: ✭ 2,466 (+976.86%)
Mutual labels:  recyclerview
Skeletonlayout
Skeleton view pattern for Android
Stars: ✭ 186 (-18.78%)
Mutual labels:  recyclerview
Kohii
Android Video Playback made easy.
Stars: ✭ 204 (-10.92%)
Mutual labels:  recyclerview
Rvparallaximageview
RvParallaxImageView用在recyclerview的item中,它可以随着recyclerview进行视差效果的移动,进而可以在一个小区域的item中展示一个完整的图片。适用于在recyclerview的item中显示大的广告图。
Stars: ✭ 224 (-2.18%)
Mutual labels:  recyclerview
Admobadapter
It wraps your Adapter to display Admob native ads and banners in a ListView/RecyclerView data set. It based on the Yahoo fetchr project https://github.com/yahoo/fetchr
Stars: ✭ 224 (-2.18%)
Mutual labels:  recyclerview
Commonadapter
一个适用于ListView/GridView/RecyclerView的Adapter库,简化大量重复代码,支持多种布局,可自定义图片加载的实现。
Stars: ✭ 219 (-4.37%)
Mutual labels:  recyclerview

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.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].