Android适配器终结者?

Android

终结Adapter、ViewHolder,封装通用适配器

项目传送门
1
compile 'com.excellence:basetools:1.2.4'

Android适配器

在Android开发中,经常使用Adapter和ViewHolder,总是写着千篇一律的适配器代码,所以进行通用型、万能型的Adapter封装。

  • MVVM模式Adapter封装
    最近学习MVVM,发现一系列优点,适配器封装也非常简单,并且可以让ViewHolder去死吧,只留下可爱的Adapter;开启DataBinding,进行封装。

  • 普通Adapter封装
    其他环境下:不开启DataBinding,封装Adapter和ViewHolder,使用时需要实现Adapter的抽象接口。

以上两种方式都可以实现单布局类型、多布局类型的适配器。

Adapter示例

单布局类型

多布局类型

MVVM模式Adapter示例

DataBinding,ListVew、GridView适配器

  • 单布局类型
    简单到爆炸有木有!!!

    1
    2
    3
    4
    // 直接创建CommonBindingAdapter
    CommonBindingAdapter<Flower> adapter = new CommonBindingAdapter<>(mFlowers, R.layout.item_flower, BR.flower);
    // 设置适配器,等同于ListView.setAdapter()、GridView.setAdapter()
    mBinding.setAdapter(adapter);
  • 多布局类型
    主要是实现多布局类型的ViewDelegate的接口

    1
    2
    3
    4
    5
    6
    7
    // 直接创建MultiItemTypeBindingAdapter
    MultiItemTypeBindingAdapter<Flower> adapter = new MultiItemTypeBindingAdapter<>(mFlowers);
    // 添加多布局类型
    adapter.addItemViewDelegate(new RoseViewDelegate());
    adapter.addItemViewDelegate(new TulipViewDelegate());
    // 设置适配器
    mBinding.setAdapter(adapter);

    实现ItemViewDelegate的接口

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    public static class RoseViewDelegate implements ItemViewDelegate<Flower>
    {
    @Override
    public int getItemViewLayoutId()
    {
    return R.layout.item_rose;
    }
    @Override
    public int getItemVariable()
    {
    return BR.rose;
    }
    @Override
    public boolean isForViewType(Flower item, int position)
    {
    return item instanceof Rose;
    }
    }

DataBinding,RecyclerView适配器

同上

  • 单布局类型

    1
    2
    3
    4
    5
    6
    // 直接创建BaseRecyclerBindingAdapter
    BaseRecyclerBindingAdapter<Flower> adapter = new BaseRecyclerBindingAdapter<>(mFlowers, R.layout.item_flower, BR.flower);
    // 设置适配器,等同于RecyclerView.setAdapter()
    mBinding.setAdapter(adapter);
    // 注意设置LayoutManager,等同于RecyclerView.setLayoutManager()
    mBinding.setLayoutManager(new LinearLayoutManager(this));
  • 多布局类型

    1
    2
    3
    4
    5
    MultiItemTypeBindingRecyclerAdapter<Flower> adapter = new MultiItemTypeBindingRecyclerAdapter<>(mFlowers);
    adapter.addItemViewDelegate(new RoseViewDelegate());
    adapter.addItemViewDelegate(new TulipViewDelegate());
    mBinding.setAdapter(adapter);
    mBinding.setLayoutManager(new LinearLayoutManager(this));

普通Adapter示例

ListVew、GridView适配器

  • 单布局类型
    实现CommonAdapter的接口

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    // 创建adapter类继承CommonAdapter,然后设置适配器即可
    private class AppGridAdapter extends CommonAdapter<ResolveInfo>
    {
    public AppGridAdapter(Context context, List<ResolveInfo> datas, int layoutId)
    {
    super(context, datas, layoutId);
    }
    @Override
    public void convert(ViewHolder viewHolder, ResolveInfo item, int position)
    {
    // ViewHolder封装了一些辅助方法,方便View的各种设置
    ImageView iconView = viewHolder.getView(android.R.id.icon);
    iconView.setImageDrawable(item.loadIcon(mPackageManager));
    viewHolder.setText(android.R.id.text1, item.loadLabel(mPackageManager).toString());
    }
    }
  • 多布局类型
    实现ItemViewDelegate接口

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    // 多布局适配器
    private class ChatAdapter extends MultiItemTypeAdapter<People>
    {
    public ChatAdapter(Context context, List<People> messages)
    {
    super(context, messages);
    addItemViewDelegate(new ComputerDelegate());
    addItemViewDelegate(new BlueDelegate());
    addItemViewDelegate(new PurpleDelegate());
    }
    }
    // 不同的布局视图
    private class ComputerDelegate implements ItemViewDelegate<People>
    {
    @Override
    public int getItemViewLayoutId()
    {
    return R.layout.item_computer;
    }
    @Override
    public boolean isForViewType(People item, int position)
    {
    return item instanceof ComputerData;
    }
    @Override
    public void convert(ViewHolder viewHolder, People item, int position)
    {
    viewHolder.setText(R.id.computer_text, item.getMsg());
    }
    }

RecyclerView适配器

  • 单布局类型
    实现BaseRecyclerAdapter的接口

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    // 创建adapter类继承BaseRecyclerAdapter
    private class AppRecyclerAdapter extends BaseRecyclerAdapter<ResolveInfo>
    {
    private PackageManager mPackageManager = null;
    public AppRecyclerAdapter(Context context, List<ResolveInfo> datas, int layoutId)
    {
    super(context, datas, layoutId);
    mPackageManager = context.getPackageManager();
    }
    @Override
    public void convert(RecyclerViewHolder viewHolder, ResolveInfo item, int position)
    {
    // ViewHolder封装了一些辅助方法,方便View的各种设置
    viewHolder.setText(android.R.id.text1, item.loadLabel(mPackageManager));
    viewHolder.setImageDrawable(android.R.id.icon, item.loadIcon(mPackageManager));
    }
    }
  • 多布局类型
    实现ItemViewDelegate的接口

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    // 多布局适配器
    private class WarAdapter extends MultiItemTypeRecyclerAdapter<People>
    {
    public WarAdapter(Context context, List<People> datas)
    {
    super(context, datas);
    addItemViewDelegate(new ComputerRecyclerDelegate());
    addItemViewDelegate(new BlueRecyclerDelegate());
    addItemViewDelegate(new PurpleRecyclerDelegate());
    }
    }
    // 不同的布局视图
    private class ComputerRecyclerDelegate implements ItemViewDelegate<People>
    {
    @Override
    public int getItemViewLayoutId()
    {
    return R.layout.item_computer;
    }
    @Override
    public boolean isForViewType(People item, int position)
    {
    return item instanceof ComputerData;
    }
    @Override
    public void convert(RecyclerViewHolder viewHolder, People item, int position)
    {
    viewHolder.setText(R.id.computer_text, item.getMsg());
    }
    }
欢迎star!!!
谢谢老板,请尽情用红包来蹂躏我吧!!!
0%