Adapter

General

Category
Free
Tag
View Adapters
License
N/A
Min SDK
7 (Android 2.1 Eclair)
Registered
Apr 16, 2016
Favorites
7
Link
https://github.com/ThePacific/adapter
See also
Groupie
DeclarativeAdapter
EasyAdapter
SuperAdapter
Gencycler

Additional

Language
Kotlin
Version
2.2.0 (Mar 29, 2020)
Created
Mar 9, 2016
Updated
Apr 16, 2020
Owner
thepacific
Contributors
Brendan Kirby (brendankirby)
UsherBaby
blackberry2016
3
Activity
Badge
Generate
Download
Source code
APK file

Advertisement

Adapter

A quick adapter library for RecyclerView, GridView, ListView, ViewPager, Spinner. It abstracts the boilerplate of item view types, item layouts, viewholders, span sizes , and more, in order to simplify building complex screens with multiple view types.

Features

  • Support DataBinding.
  • Multiple view types without any ViewHolder
  • Simple, flexible

Setup

compile 'com.github.thepacific:adapter:2.2.0'

Proguard

-keep public class androidx.databinding.DataBindingUtil {
    public static androidx.databinding.ViewDataBinding bind(android.view.View);
}
-keep public class android.databinding.DataBindingUtil {
    public static android.databinding.ViewDataBinding bind(android.view.View);
}

Item

Extend SimpleRecylcerItem. Optionally, you may implement RecyclerItem

public class YourItem extends SimpleRecyclerItem {
    String name;
    String description;
    String imageUrl;

    @Override
    public int getLayout() {
        return R.layout.item_cartoon;
    }

    @Override
    public void bind(ViewHolder holder) {
        // bind data
        DefaultBinding binding = holder.binding();
        binding.setText(R.id.text_name, name);
        binding.setText(R.id.text_desc, description);

        // attach listeners or load image
        holder.attachImageLoader(R.id.img_header);
        holder.attachOnClickListener(R.id.layout_root);

        // holder.attachOnLongClickListener(viewId);
        // holder.attachOnTouchListener(viewId);
        // holder.attachOnCheckedChangeListener(viewId);
    }

    // You may override any other method
}

DataBinding

public class YourItem extends SimpleRecyclerItem {
    @Override
    public void bind(ViewHolder holder) {
        // Without DataBinding, just use DefaultBinding
        DefaultBinding binding = holder.binding();

        // With DataBinding, use layout generated Binding instead of DefaultBinding
        LayoutGeneratedBinding binding = holder.binding();
    }
}

Listeners

Set OnClickListener, OnLongClickListener, OnTouchListener, OnCheckedChangeListener and ImageLoader:

RecyclerAdapter adapter = new RecyclerAdapter(); // RecyclerView
AbsAdapter adapter = new AbsAdapter(int viewTypeCount);// GridView, ListView, Spinner
PagerAdapter2 adapter2 =new PagerAdapter2();// ViewPager

// set listeners or ImageLoader
adapter.setOnClickListener(OnClickListener listener);
adapter.setOnTouchListener(OnTouchListener listener);
adapter.setOnLongClickListener(OnLongClickListener listener);
adapter.setOnCheckedChangeListener(OnCheckedChangeListener listener);
adapter.setImageLoader(ImageLoader imageLoader);

adapter.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        // get ViewHolder
        ViewHolder holder = AdapterUtils.getHolder(v);

        // find view in ItemView
        TextView textName = AdapterUtils.findView(holder.itemView, R.id.text_name);

        // get Item
        Item item = holder.getItem();

        // get adapter data size
        int size = holder.getSize();

        holder.isFirstItem();
        holder.isLastItem();

        // for ListView,GridView, ViewPager, Spinner and RecyclerView
        int position = holder.getCurrentPosition();
         
        // only for RecyclerView
        int position = holder.getAdapterPosition();
    }
});

adapter.setImageLoader((imageView, holder) -> {
    YourItem item = holder.getItem();
    Glide.with(this)
                .load(item.imageUrl())
                .fitCenter()
                .into(imageView);
});

Others

adapter.setOnDataSetChanged(OnDataSetChanged onDataSetChanged);

//  data set changed callback , it's useful to show or hide empty view
public interface OnDataSetChanged {
    /**
     * called when data source changed
     *
     * @param count current data source size
     */
    void apply(int count);
}

License

The MIT License