Giter Club home page Giter Club logo

commonadapter's Introduction

CommonAdapter

Android Arsenal

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

功能

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

零、重要接口

adapter的item必须实现此接口,接口源码如下:

public interface AdapterItem<T> {

    /**
     * @return item布局文件的layoutId
     */
    @LayoutRes
    int getLayoutResId();

    /**
     * 初始化views
     */
    void bindViews(final View root);

    /**
     * 设置view的参数
     */
    void setViews();

    /**
     * 根据数据来设置item的内部views
     *
     * @param model    数据list内部的model
     * @param position 当前adapter调用item的位置
     */
    void handleData(T model, int position);

}  

例子:

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 setViews() { }

    @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());

设计思路

1. Adapter

如果用adapter常规写法,你会发现代码量很大,可读性低。如果adapter中有多个类型的Item,我们还得在getView()中写很多if-else语句,很乱。 而现在我让adapter的代码量减少到一个8行的内部类,如果你需要更换item只需要动一行代码,真正实现了可插拔化。最关键的是item现在作为了一个独立的对象,可以方便的进行复用。

2. AdapterItem

和原来方式最为不同的一点就是我把adapter的item作为了一个实体,这种方式借鉴了RecyclerView中ViewHolder的设计。把item作为实体的好处有很多,比如复用啊,封装啊,其余的就不细说了。

3. 分层

在使用过程中,我发现如果adapter放在view层,那就会影响到view层的独立性。此外adapter中经常有很多数据处理的操作,比如通过type选择item,数据的拆包、转换等操作。于是我还是推荐把adapter放在mvp的p层,或者是mvvm的m层。通过在实际的项目中使用来看,放在m或p层的效果较好,view的复用也比较好做。

开发者

Jack Tony: [email protected]  

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.

commonadapter's People

Contributors

kaleai avatar thinwonton avatar valuesfeng avatar xxxifan avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.