CommonAdapter

Introduction: 通过封装 BaseAdapter 和 RecyclerView.Adapter 得到的通用的,简易的 Adapter
More: Author   ReportBugs   
Tags:

通过封装 BaseAdapter 和 RecyclerView.Adapter 得到的通用、简易的 Adapter 对象。

功能

  • [x] 支持多种类型的 item
  • [x] item 会根据 type 来做自动复用
  • [x] ​支持 dataBinding 和其他注入框架
  • [x] 支持 ViewPager 的懒加载模式
  • [x] 提升 item 的独立性,完美支持 item 被多处复用
  • [x] 一个 item 仅会触发一次绑定视图的操作,提升效率
  • [x] 一个 item 仅会调用一次 setViews(),避免重复建立 listener
  • [x] 支持快速将 ListView 的 adapter 切换为 recyclerView 的 adapter
  • [x] 允许用 viewpager 的 notifyDataSetChanged()来更新界面
  • [x] 可以给 recyclerView 的添加空状态(利用RcvAdapterWrapper
  • [x] 可以给 recyclerView 的添加头、底(利用RcvAdapterWrapper
  • [x] 提供了 getCurrentPosition()来支持根据不同的位置选择不同 item 的功能
  • [x] 提供了 getConvertedData(data, type)方法来对 item 传入的数据做转换,拆包
  • [x] 支持在 item 中获得 Activity 对象,用来跳转
  • [x] 支持 RecycleView 层面得到 item 被点击的事件,内外事件会同时触发
  • [x] 允许 Adapter 的数据范形和 item 中的不同,增加灵活性
  • [x] 支持适配器的数据自动绑定,即:数据更改后 adapter 会自动 notify 界面(需配合 databinding 中的ObservableList

示例

上图是在作者的授权下引用了设计师“流浪汉国宝(QQ:515288905)”在 UI 中国上的作品

我觉得这个设计很简洁清爽,未来可能会出这个设计的 android 实现。

添加依赖

1.在项目外层的 build.gradle 中添加 JitPack 仓库

repositories {
    maven {
        url "https://jitpack.io"
    }
}

2.在用到的项目中添加依赖

compile 'com.github.tianzhijiexian:CommonAdapter:Latest release(<-click it)'

举例:

compile 'com.github.tianzhijiexian:CommonAdapter:1.0.0'

零、建立 Item

Adapter 的 item 须实现 AdapterItem 接口也可继承 AbsAdapterItem,例子:

public class TextItem implements AdapterItem<DemoModel> {

    @Override
    public int getLayoutResId() {
        return R.layout.demo_item_text;
    }

    TextView textView;

    @Override
    public void bindViews(View root) {
        textView = (TextView) root.findViewById(R.id.textView);
    }

    @Override
    public void handleData(DemoModel model, int position) {
        textView.setText(model.content);
    }
}

一、ListView+GridView 的通用适配器——CommonAdapter

只需继承CommonAdapter便可实现适配器:

listView.setAdapter(new CommonAdapter<DemoModel>(list, 1) {
    public AdapterItem<DemoModel> createItem(Object type) {
        return new TextItem();
    }
});

二、RecyclerView 的通用适配器——CommonRcvAdapter

通过继承CommonRcvAdapter来实现适配器:

mAdapter = new CommonRcvAdapter<DemoModel>(list) {
 public AdapterItem createItem(Object type) {
        return new TextItem();
  }
};

三、ViewPager 的通用适配器——CommonPagerAdapter

通过继承CommonPagerAdapter来实现适配器:

viewPager.setAdapter(new CommonPagerAdapter<DemoModel>(list) {
    public AdapterItem createItem(Object type) {
        return new TextItem();
    }
});

四、如果需要 RecyclerView 的 pool

需要通过库提供的RecycledViewPool 来设置 pool。

RecycledViewPool pool = new RecycledViewPool();

// ...

recyclerView.setRecycledViewPool(pool);
adapter.setTypePool(pool.getTypePool());

设计思路

在使用过程中,我发现如果 adapter 放在 view 层,那就会影响到 view 层的独立性,adapter 中经常有很多数据处理的操作,不是单纯的展示。我推荐把 adapter 放在 mvp 的 p 层,或者是 mvvm 的 m 层,view 的复用也比较好做。

开发者

Jack Tony: developer-kale@foxmail.com

License

Copyright 2016-2019 Jack Tony

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.
Apps
About Me
GitHub: Trinea
Facebook: Dev Tools