Skip to content

Commit

Permalink
重写展开Adapter,添加更多的辅助方法,以及更新IMultiItem接口,添加IMultiItem与IExpandable的实现抽象类
Browse files Browse the repository at this point in the history
  • Loading branch information
EvilBT committed Jan 21, 2017
1 parent c94521a commit 2e431a9
Show file tree
Hide file tree
Showing 19 changed files with 1,168 additions and 82 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,11 @@ public class HeadAndFootAdapter extends BaseAdapter<String> {
![加载失败](http://o9qzkbu2x.bkt.clouddn.com/3.jpg?imageMogr2/auto-orient/thumbnail/300x)
![没有数据](http://o9qzkbu2x.bkt.clouddn.com/2.jpg?imageMogr2/auto-orient/thumbnail/300x)
### 支持多布局
继承*BaseMultiAdapter*抽象类,数据类型实现*IMultiItem*接口即可
继承*BaseMultiAdapter*抽象类,数据类型实现*IMultiItem*接口(可以简单继承`DefaultMultiItem`)即可
具体参考Demo中的[`MultiItemActivity`](https://github.com/EvilBT/SherlockAdapter/blob/master/app/src/main/java/xyz/zpayh/myadapter/MultiItemActivity.java)
![多布局](http://o9qzkbu2x.bkt.clouddn.com/5.jpg?imageMogr2/auto-orient/thumbnail/300x)
### 支持伸缩子项
继承BaseExpandableAdapter,如果有可子项需要伸缩,数据类型实现*IExpandable*,子项数据类型实现*IMultiItem*,如果
继承BaseExpandableAdapter,如果有可子项需要伸缩,数据类型实现*IExpandable*(可以简单继承`DefaultExpandable`),子项数据类型实现*IMultiItem*,如果
没有子项可伸缩,则数据类型实现*IMultiItem*即可,如果子项也有它的子项,则子项也需要实现*IExpandable*,子项的子项数据类型
实现*IMultiItem*接口。详情参考Demo中的[`ExpandableActivity`](https://github.com/EvilBT/SherlockAdapter/blob/master/app/src/main/java/xyz/zpayh/myadapter/ExpandableActivity.java)
更多细节请下载Demo查看源代码。
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
* 邮 箱: ch_zh_p@qq.com
* 修改时间:
* 修改备注:
* 用{@link ExpandableAdapter}重新实现逻辑,请用{@link ExpandableAdapter}
*/

public abstract class BaseExpandableAdapter extends BaseMultiAdapter<IMultiItem> {
@Deprecated
abstract class BaseExpandableAdapter extends BaseMultiAdapter {

/**
* 展开下级菜单
Expand Down
9 changes: 7 additions & 2 deletions adapter/src/main/java/xyz/zpayh/adapter/BaseMultiAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,16 @@
* 修改备注:
*/

public abstract class BaseMultiAdapter<T extends IMultiItem> extends BaseAdapter<T> {
public abstract class BaseMultiAdapter extends BaseAdapter<IMultiItem> {

@Override
public int getLayoutRes(int index) {
final T data = mData.get(index);
final IMultiItem data = mData.get(index);
return data.getLayoutRes();
}

@Override
public void convert(BaseViewHolder holder, IMultiItem data, int index) {
data.convert(holder);
}
}
32 changes: 32 additions & 0 deletions adapter/src/main/java/xyz/zpayh/adapter/BaseViewHolder.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package xyz.zpayh.adapter;

import android.graphics.Bitmap;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.support.annotation.CheckResult;
import android.support.annotation.DrawableRes;
Expand Down Expand Up @@ -128,6 +129,14 @@ public BaseViewHolder setText(@IdRes int textId, @StringRes int stringId){
return setText(textId,text);
}

public BaseViewHolder setText(@IdRes int textId, TextCallback callback){
TextView textView = find(textId);
if (textView != null && callback != null){
callback.callback(textView);
}
return this;
}

public BaseViewHolder setImage(@IdRes int imageId, @DrawableRes int resId){
ImageView imageView = find(imageId);
if (imageView != null){
Expand All @@ -152,6 +161,29 @@ public BaseViewHolder setImage(@IdRes int imageId,Bitmap bm){
return this;
}

public BaseViewHolder setImage(@IdRes int imageId,ImageCallback callback){
ImageView imageView = find(imageId);
if (imageView != null && callback != null){
callback.callback(imageView);
}
return this;
}

public BaseViewHolder setView(@IdRes int viewId, ViewCallback callback){
if (find(viewId) != null && callback != null){
callback.callback(find(viewId));
}
return this;
}

public BaseViewHolder setTypeface(@IdRes int textId, Typeface typeface){
TextView textView = find(textId);
if (textView != null){
textView.setTypeface(typeface);
}
return this;
}

/**
* 设置View的visibility状态
* @param id View id
Expand Down
98 changes: 98 additions & 0 deletions adapter/src/main/java/xyz/zpayh/adapter/DefaultExpandable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package xyz.zpayh.adapter;

import android.support.annotation.LayoutRes;

import java.util.ArrayList;
import java.util.List;

/**
* 文 件 名: DefaultExpandable
* 创 建 人: 陈志鹏
* 创建日期: 2017/01/21 14:38
* 邮 箱: ch_zh_p@qq.com
* 修改时间:
* 修改备注:
* 一个默认的实现{@link IExpandable}大部分方法的抽象类,一般继承实现此抽象类即可。
*/

public abstract class DefaultExpandable<T> implements IExpandable {

protected final List<IMultiItem> mSubData;

private int mSpanSize;

private boolean mExpandable;

@LayoutRes
private final int mLayoutRes;

protected T mData;

public DefaultExpandable(@LayoutRes int layoutRes) {
this(layoutRes, null);
}

public DefaultExpandable(@LayoutRes int layoutRes, T data){
this(layoutRes, data, 1);
}

public DefaultExpandable(@LayoutRes int layoutRes,T data, int spanSize){
mLayoutRes = layoutRes;
mData = data;
mSpanSize = spanSize;
mSubData = new ArrayList<>();
mExpandable = false;
}

public T getData() {
return mData;
}

public void setData(T mData) {
this.mData = mData;
}

public void setSubData(List<IMultiItem> subData){
mSubData.clear();
if (subData != null){
mSubData.addAll(subData);
}
}

public void addSubData(List<IMultiItem> subData){
if (subData != null){
mSubData.addAll(subData);
}
}

public void addSubData(IMultiItem subData){
if (subData != null){
mSubData.add(subData);
}
}

@Override
public boolean isExpandable() {
return mExpandable;
}

@Override
public void setExpandable(boolean expandable) {
mExpandable = expandable;
}

@Override
public List<IMultiItem> getSubItems() {
return mSubData;
}

@Override
public int getLayoutRes() {
return mLayoutRes;
}

@Override
public int getSpanSize() {
return mSpanSize;
}
}
56 changes: 56 additions & 0 deletions adapter/src/main/java/xyz/zpayh/adapter/DefaultMultiItem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package xyz.zpayh.adapter;

import android.support.annotation.LayoutRes;

/**
* 文 件 名: DefaultMultiItem
* 创 建 人: 陈志鹏
* 创建日期: 2017/01/21 14:38
* 邮 箱: ch_zh_p@qq.com
* 修改时间:
* 修改备注:
* 一个默认的实现{@link IMultiItem}大部分方法的抽象类,一般继承实现此抽象类即可。
*/


public abstract class DefaultMultiItem<T> implements IMultiItem {

@LayoutRes
private final int mLayoutRes;

private int mSpanSize;

protected T mData;

public DefaultMultiItem(@LayoutRes int layoutRes) {
this(layoutRes,null);
}

public DefaultMultiItem(@LayoutRes int layoutRes,T data) {
this(layoutRes, data, 1);
}

public DefaultMultiItem(@LayoutRes int layoutRes,T data, int spanSize) {
this.mLayoutRes = layoutRes;
this.mData = data;
this.mSpanSize = spanSize;
}

public T getData() {
return mData;
}

public void setData(T mData) {
this.mData = mData;
}

@Override
public int getLayoutRes() {
return mLayoutRes;
}

@Override
public int getSpanSize() {
return mSpanSize;
}
}
Loading

0 comments on commit 2e431a9

Please sign in to comment.