Home

Awesome

CommonAdapter

Android Arsenal

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

功能

示例

上图是在作者的授权下引用了设计师“流浪汉国宝(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.