项目作者: MaximeJallu

项目描述 :
Generic Adapters for Android
高级语言: Java
项目地址: git://github.com/MaximeJallu/RecyclerAdapter-Java.git
创建时间: 2017-09-13T11:40:53Z
项目社区:https://github.com/MaximeJallu/RecyclerAdapter-Java

开源协议:

下载


Status

alt text API

Maven Central

Android Arsenal

Generic-Adapter:

This tool allows you to no longer worry about adapters. Now you will only create your ViewHolder.
Communication management between your Views and your ViewHolders is possible.
Creating sections is now very easily.
Enjoy.

Download Maven Central

buildtool used is 27
use {exclude group: ‘com.android.support’} only if you have problems

  1. dependencies {
  2. ...
  3. implementation ('com.github.maximejallu:adapters:{version}')
  4. ...
  5. }

Decorators for : ButterKnife - Picasso - Glide

  1. public class SampleApplication extends Application {
  2. @Override
  3. public void onCreate() {
  4. super.onCreate();
  5. RecyclerAdapter.Helper()
  6. .append(new InitViewHolderDecorator() {
  7. @Override
  8. public void initBinding(Object target, View view) {
  9. ButterKnife.bind(target, view);
  10. }
  11. })
  12. .append(new ShowPictureDecorator() {
  13. @Override
  14. public void showPicture(ImageView picture, String url) {
  15. //use Picasso, Glide... Other
  16. }
  17. })
  18. .init();
  19. }
  20. }

RecyclerAdapter (Easy sample method)

CustomerViewHolder.class :

  1. @BindLayoutRes(R.layout.{name_of_your_layout})
  2. public class CustomerViewHolder extends RecyclerViewHolder<Customer> {
  3. TextView mText;
  4. CustomerViewHolder(View view){
  5. super(view);
  6. mText = view.findViewById(R.id.text1);
  7. }
  8. void onBind(Customer item){
  9. //todo implements
  10. }
  11. }

MainFragment.class

  1. public class MainFragment extends Fragment {
  2. ...
  3. private RecyclerAdapter<Customer> mAdapter;
  4. void onCreate(...){
  5. mAdapter = new RecyclerAdapter(customerList, CustomerViewHolder.class);
  6. }
  7. ...
  8. }

RecyclerAdapter (Multi cell method)

  • create your necessary ViewHolder (here only one example)
    ```java
    @BindLayoutRes(R.layout.{name_of_your_layout1})
    public class CustomerViewHolder1 extends RecyclerViewHolder {
    CustomerViewHolder1(View view){

    1. super(view);

    }

    void onBind(Customer item){

    1. //todo implements

    }
    }

//# Solution 1 : the object determines are own item view type
public class Customer implements IViewType {
public static final int TYPE_ON_LINE = 0; /default/
public static final int TYPE_STORE = 1;
public static final int TYPE_OTHER = 2;
private int mType;

@Override
public int getItemViewType() {
return mType;
}
}

public class MyFragment extends Fragment {

  1. public void onCreateView(){
  2. private RecyclerAdapter<Customer> mAdapter;
  3. mAdapter = new RecyclerAdapter(customerList, CustomerViewHolder1.class/*type par default*/);
  4. mAdapter.putViewType(Customer.TYPE_STORE, CustomerViewHolder2.class, null /*callback*/);
  5. mAdapter.putViewType(Customer.TYPE_OTHER, CustomerViewHolder3.class, true /*add default callback*/);
  6. //# Solution 2 : We give the strategy of the IViewType to our adapt it
  7. mAdapter.setItemViewType(item -> {
  8. //todo stategy type of item
  9. return 0;
  10. });
  11. mRecyclerView.setAdapter(adapter);
  12. }

}

  1. # SectionDecorator (Recycler with LinearLayout or GridLayout)
  2. precondition : create your RecyclerViewHolder
  3. Sample :
  4. ```java
  5. mRecylerView.setLayoutManager(...);
  6. /*create Adapter*/
  7. RecyclerAdapter<Customer> baseAdapter = new RecyclerAdapter<>(...);
  8. /*create sectioned adapter. the Adapter type can be RecyclerView.Adapter*/
  9. SectionedAdapter<String, RecyclerAdapter> adapter = new SectionedAdapter<>(SectionViewHolder.class, baseAdapter);
  10. /*add your sections*/
  11. sectionAdapter.addSection(0/*position*/, "Title Section 1");
  12. /*attach Adapter to RecyclerView*/
  13. mRecylerView.setAdapter(sectionAdapter);

ItemDecoration

  1. mRecyclerView.addItemDecoration(new FooterDecoration(getContext(), this, R.layout.item_space_80));