Skip to content

cubesky/AutoRecyclerAdapter

Repository files navigation

Auto Recycler Adapter & Smart Recycler Adapter

GitHub license

This library can help you to control RecyclerView in an easiest way.

Warning: This library using an ugly way and it has some performance issue!
Now you can use ARALib and SmartRecyclerAdapter to improve performance.

This library is under development. You can use this by import CubeSky Repo

From version 2.0, SmartRecyclerAdapter moved to AndroidX insteadof AppCompat.

Smart Recycler Adapter

Smart Recycler Adapter finished in version 1.0, it use Annotation Processor to auto generate source code when you compile, so it doesn't cause any performanse issue.

One more import

After import main library using CubeSky Repo, you need add more library to your build.gradle file. Use main library version code instead of <version> in example.

dependencies {
    compileOnly 'party.liyin:aralib:<version>'
    annotationProcessor 'party.liyin:aralib-processor:<version>'
}

How to use

First, you need a layout. Create a layout file for your data which want to display. Eg. R.layout.datacard
And then, you need add some code to your code to init adapter.

SmartRecyclerAdapter adapter = new SmartRecyclerAdapter(context);
RecyclerView recyclerView = findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);

Then, you need a JavaBean to extends AutoDataBean with ARABind annotation to bind your data to the view.
Instead of old AutoBind system, ARABind use resource id as view_id and use Class as view_type to simplify your code. In ARABind view_param is optional, it only used with multi parameter method.
And then, add ARABindLayout annotation to Class to tell adapter witch layout should it use, it will automatically bind to it.
ARALink give you an access to View component, but if it also bind with ARABind, ARABind will always override your operation on the same method.
Note: Avoid to use ARALink to call set method.

If you need view_param, please use boxed Object such as Integer and Float instead of int and float.
In SmartRecyclerAdapter fields must be public modifier!

@ARABindLayout(R.layout.datacard)
public class DataCardBean extends AutoDataBean {
        @ARABind(view_id = R.id.textView, view_type = TextView.class, view_method = "setText")
        public String data;
        @ARABind(view_id = R.id.textView, view_type = TextView.class, view_method = "setTextSize", view_param = { Integer.class, Float.class })
        public Object[] size;
        @ARALink(view_id = R.id.textView2)
        public TextView textView;
        DataCardBean(String data, int unit, float size) {
            this.data = data;
            this.size = new Object[]{unit, size};
        }
}

Other methods support by Auto Recycler Adapter is also support in Smart Recycler Adapter. You can use them as usual.

SmartRecyclerAdapter unique feature

These feature doesn't work with deprecated Auto Recycler Adapter!

ARALink

ARALink annotation will automatically bind View to its variable. This give you temporary access to the View. . When variable in your bean is ready, boolean variable isInit in AutoDataBean superclass will set to true. If you access ARALink variable before isInit is set to true, you will got a NullPointerException due to variable is unset.

SmartLayoutEnum

SmartLayoutEnum is an enum viewType integer wrapper. Every AutoDataBean is named with its layoudType Integer in this enum class.

getItemWithType

This method will return a AutoBeanWithType class, it contains SmartLayoutEnum with its type and AutoDataBean itself.

SmartRecyclerAdapter.AutoBeanWithType beanWithType = autoRecyclerAdapter.getItemWithType(0);
if (beanWithType.getType() == SmartRecyclerAdapter.SmartLayoutEnum.OneDataBean) {
    ((OneDataBean)beanWithType.getBean()).textView.getText();
}

MeasuredRecyclerView (Experimental)

A RecyclerView which can auto calculate item columns count.

RecyclerHelper

Easy to get LayoutManager, nothing special.

Auto Javadoc

Now annotation processor will auto generate Javadoc. If you doesn't active annotation processor, you will get warning in Javadoc.

Auto Recycler Adapter

Auto Recycler Adapter using Reflection in Runtime to setting value to View. This will cause performance issue. But early then version 1.0 (Not include), you can only use this, I keep it as compatibility.
From version 2.0, AutoRecyclerAdapter mark as Deprecated, you should not use this.
AutoRecyclerAdapter will removed in 2.5.

How to use

First, you need a layout. Create a layout file for your data which want to display. Eg. R.layout.datacard
And then, you need add some code to your code to init adapter.

AutoRecyclerAdapter adapter = new AutoRecyclerAdapter(context);
RecyclerView recyclerView = findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);

Then, you need a JavaBean to extends AutoDataBean with AutoBind annotation to bind your data to the view.

class DataCardBean extends AutoDataBean {
        @AutoBind(view_id = "xxxTextView",view_method = "setText",view_param = CharSequence.class)
        protected String data;
        @AutoBind(view_id = "xxxTextView", view_method = "setTextSize", view_param = {int.class, float.class})
        protected Object[] size;
        DataCardBean(String data, int unit, float size) {
            this.data = data;
            this.size = new Object[]{unit, size};
        }
}

And now, you need bind them together.

autoRecyclerAdapter.bindView(DataCardBean.class, R.layout.datacard);

After Version 0.6, you can use @AutoBindView for AutoDataBean

@AutoBindView(R.layout.datacard)
class DataCardBean extends AutoDataBean {
        @AutoBind(view_id = "xxxTextView",view_method = "setText",view_param = CharSequence.class)
        protected String data;
        @AutoBind(view_id = "xxxTextView", view_method = "setTextSize", view_param = {int.class, float.class})
        protected Object[] size;
        OneDataBean(String data, int unit, float size) {
            this.data = data;
            this.size = new Object[]{unit, size};
        }
}

No need to call adapter.bindView Method!

After, you can use

adapter.addData(new DataCardBean("Hello", TypedValue.COMPLEX_UNIT_PX, 30f), ...)

to add data.
And use

adapter.remove(0); //position

to remove data, or adapter.clear(); to clear all data.

If you want to control the data list on your self. Just run these to get raw data list.

ArrayList<AutoDataBean> list = adapter.getItemList();

Remember, if you control this list in your way, you need to call adapter.notifyDataSetChanged(); to tell adapter to refresh the list on the view.

ShortCut

If you have any layout doesn't need any data. you can use EmptyDataBean.getEmptyLayout(R.layout.layout_xxx) to get a special AutoDataBean. Just add it to the adapter, everything is done.
If you use this EmptyDataBean to add layout with no dynamic data. It will have some performance improve by skip reflection and data inject in the background.

License

MIT

About

A simpler way to control RecyclerView

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages