Skip to content

Commit

Permalink
添加长按事件,添加注释
Browse files Browse the repository at this point in the history
  • Loading branch information
EvilBT committed Dec 25, 2016
1 parent 86130ef commit 842b8f7
Show file tree
Hide file tree
Showing 31 changed files with 835 additions and 189 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
# SherlockAdapter

TODO 完善README,测试方法,写好实例
130 changes: 117 additions & 13 deletions adapter/src/main/java/xyz/zpayh/adapter/BaseAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,18 @@
import java.util.List;

/**
* Created by 陈志鹏
* on 2016/12/19.
* 一个简单通用的Adapter控件
* 文 件 名: BaseAdapter
* 创 建 人: 陈志鹏
* 创建日期: 2016/12/25 02:38
* 邮 箱: ch_zh_p@qq.com
* 修改时间:
* 修改备注:
*/

public abstract class BaseAdapter<T> extends RecyclerView.Adapter<BaseViewHolder>
implements LoadMore{

protected final List<T> mData;
protected final List<T> mData = new ArrayList<>();

private int mHeadLayouts[] = new int[0];

Expand All @@ -34,13 +37,13 @@ public abstract class BaseAdapter<T> extends RecyclerView.Adapter<BaseViewHolder
* Empty布局,在没有数据的时间显示,默认是R.layout.default_empty
*/
@LayoutRes
private int mEmptyLayout;
private int mEmptyLayout = R.layout.default_empty;

/**
* Error布局,在没有数据的时间显示,默认是R.layout.default_error
*/
@LayoutRes
private int mErrorLayout;
private int mErrorLayout = R.layout.default_error;

/**
* 是否显示Error布局
Expand All @@ -63,7 +66,7 @@ public abstract class BaseAdapter<T> extends RecyclerView.Adapter<BaseViewHolder
* 来控制显示隐藏一些View,已经提供了默认实现
*/
@LayoutRes
private int mLoadMoreLayout;
private int mLoadMoreLayout = R.layout.default_loadmore;

/**
* 加载更多状态,有{@link LoadMore#LOADING},{@link LoadMore#LOAD_COMPLETED}以及
Expand All @@ -75,12 +78,7 @@ public abstract class BaseAdapter<T> extends RecyclerView.Adapter<BaseViewHolder

private OnItemClickListener mOnItemClickListener;

public BaseAdapter(){
mData = new ArrayList<>();
mEmptyLayout = R.layout.default_empty;
mErrorLayout = R.layout.default_error;
mLoadMoreLayout = R.layout.default_loadmore;
}
private OnItemLongClickListener mOnItemLongClickListener;

/**
* 设置新数据,会清除掉原有数据,并有可能重置加载更多状态
Expand All @@ -99,6 +97,25 @@ public void setData(@Nullable List<T> data){
notifyDataSetChanged();
}

/**
* 添加新数据,并有可能重置加载更多状态
* @param data 数据集合
*/
public void addData(T data){
if (data == null){
return;
}
final int startPos = mData.size() + getHeadSize();
final int itemCount = 1 + getFootSize() + (canAutoLoadMore()?1:0);
mData.add(data);

if (mOpenAutoLoadMore){
mLoadState = LOADING;
}
mShowErrorView = false;
notifyItemRangeChanged(startPos,itemCount);
}

/**
* 添加新数据,并有可能重置加载更多状态
* @param data 数据集合
Expand All @@ -118,6 +135,80 @@ public void addData(List<T> data){
notifyItemRangeChanged(startPos,itemCount);
}

public void removeData(T data){
if (data == null || !mData.contains(data)){
return;
}

int index = mData.indexOf(data);
mData.remove(data);
notifyItemRemoved(getHeadSize()+index);
}

public void removeData(int adapterPosition){
int index = adapterPosition - getHeadSize();
if (index < 0 || index >= mData.size()){
return;
}
mData.remove(index);
notifyItemRemoved(adapterPosition);
}

public void removeHead(int adapterPosition){
if (adapterPosition < 0 || adapterPosition >= getHeadSize()){
return;
}

for (int i = adapterPosition; i < mHeadLayouts.length-1; i++) {
mHeadLayouts[i] = mHeadLayouts[i+1];
if (mHeadLayouts[i+1] == 0){
return;
}
}

mHeadLayouts[mHeadLayouts.length-1] = 0;
notifyItemRemoved(adapterPosition);
}

public void removeFoot(int adapterPosition){
int index = adapterPosition - getHeadSize() - mData.size();
if (index < 0 || index >= getFootSize()){
return;
}

for (int i = index; i < mFootLayouts.length-1; i++) {
mFootLayouts[i] = mFootLayouts[i+1];
if (mFootLayouts[i+1] == 0){
return;
}
}

mFootLayouts[mFootLayouts.length-1] = 0;
notifyItemRemoved(adapterPosition);
}

public void removeAllHead(){
int size = getHeadSize();
for (int i = 0; i < mHeadLayouts.length; i++) {
if(mHeadLayouts[i] == 0){
break;
}
mHeadLayouts[i] = 0;
}
notifyItemRangeRemoved(0,size);
}

public void removeAllFoot(){
int size = getFootSize();
for (int i = 0; i < mFootLayouts.length; i++) {
if (mFootLayouts[i] == 0){
break;
}
mFootLayouts[i] = 0;
}
notifyItemRangeRemoved(getHeadSize()+mData.size(),size);
}

/**
* 此position是AdapterPosition
* @param adapterPosition 当前项的AdapterPosition
Expand All @@ -142,6 +233,10 @@ public void setOnItemClickListener(@Nullable OnItemClickListener onItemClickList
this.mOnItemClickListener = onItemClickListener;
}

public void setOnItemLongClickListener(@Nullable OnItemLongClickListener onItemLongClickListener){
this.mOnItemLongClickListener = onItemLongClickListener;
}

/**
* 按调用顺序添加头部布局
* @param headLayout 布局id
Expand Down Expand Up @@ -487,6 +582,15 @@ public void onItemClick(@NonNull View view, int adapterPosition) {
}
}
});
baseViewHolder.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(@NonNull View view, int adapterPosition) {
if (mOnItemLongClickListener != null){
return mOnItemLongClickListener.onItemLongClick(view, adapterPosition);
}
return false;
}
});
bind(baseViewHolder, layoutRes);
}

Expand Down
86 changes: 76 additions & 10 deletions adapter/src/main/java/xyz/zpayh/adapter/BaseExpandableAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,32 @@
import java.util.List;

/**
* Created by Administrator on 2016/12/23.
* 文 件 名: BaseExpandableAdapter
* 创 建 人: 陈志鹏
* 创建日期: 2016/12/23 02:38
* 邮 箱: ch_zh_p@qq.com
* 修改时间:
* 修改备注:
*/

public abstract class BaseExpandableAdapter extends BaseMultiAdapter<IMultiItem> {

/**
* 展开下级菜单
*/
public void expand(int adapterPosition){
final IMultiItem data = getData(adapterPosition);
if (!(data instanceof IExpandable)){
return;
}
final IExpandable expandable = (IExpandable) data;
if (expandable.isExpandable()){
final List<IMultiItem> subItems = expandable.getSubItems();
if (subItems == null || subItems.isEmpty()){
return;
}

final List<IMultiItem> subItems = expandable.getSubItems();
if (subItems == null || subItems.isEmpty()){

if (expandable.isExpandable() && mData.containsAll(subItems)){
return;
}

Expand All @@ -31,24 +40,81 @@ public void expand(int adapterPosition){
notifyItemRangeInserted(adapterPosition+1,subItems.size());
}

public void collapse(int adapterPosition){
/**
* 展开全部下级菜单
*/
public void expandAll(int adapterPosition){
final IMultiItem data = getData(adapterPosition);
if (!(data instanceof IExpandable)){
return;
}

final IExpandable expandable = (IExpandable) data;
if (!expandable.isExpandable()){

final List<IMultiItem> subItems = expandable.getSubItems();
if (subItems == null || subItems.isEmpty()){
return;
}

if (expandable.isExpandable() && mData.containsAll(subItems)){
return;
}


expandable.setExpandable(true);

int size = 0;
int index = adapterPosition-getHeadSize()+1;
for (IMultiItem subItem : subItems) {
size +=addAll(index+size,subItem);
}
notifyItemChanged(adapterPosition);
notifyItemRangeInserted(adapterPosition+1,size);
}

private int addAll(int index, IMultiItem item){
if (item == null){
return 0;
}
int size = 1;
mData.add(index,item);
if (!(item instanceof IExpandable)){
return size;
}
final IExpandable expandable = (IExpandable) item;
final List<IMultiItem> subItems = expandable.getSubItems();
if (subItems == null || subItems.isEmpty()){
return size;
}

if (expandable.isExpandable() && mData.containsAll(subItems)){
return size;
}

expandable.setExpandable(true);
for (IMultiItem subItem : subItems) {
size += addAll(index+size,subItem);
}
return size;
}

public void collapse(int adapterPosition){
final IMultiItem data = getData(adapterPosition);
if (!(data instanceof IExpandable)){
return;
}
final IExpandable expandable = (IExpandable) data;
final List<IMultiItem> subItems = expandable.getSubItems();
if (subItems == null || subItems.isEmpty()){
return;
}

if (!expandable.isExpandable() && !mData.containsAll(subItems)){
return;
}

expandable.setExpandable(false);

// mData.removeAll(subItems);
int removeSize = removeAll(subItems);

notifyItemChanged(adapterPosition);
Expand All @@ -61,13 +127,13 @@ private int removeAll(List<IMultiItem> subItems){
if (subItem == null || !(subItem instanceof IExpandable))
continue;
IExpandable expandable = (IExpandable) subItem;
if (!expandable.isExpandable())
continue;
expandable.setExpandable(false);
final List<IMultiItem> items = expandable.getSubItems();
if (items == null || items.isEmpty()){
continue;
}
if (!expandable.isExpandable() && !mData.containsAll(items))
continue;
expandable.setExpandable(false);
size += removeAll(items);
}
mData.removeAll(subItems);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package xyz.zpayh.adapter;

/**
* Created by Administrator on 2016/12/23.
* 文 件 名: BaseMultiAdapter
* 创 建 人: 陈志鹏
* 创建日期: 2016/12/25 02:38
* 邮 箱: ch_zh_p@qq.com
* 修改时间:
* 修改备注:
*/

public abstract class BaseMultiAdapter<T extends IMultiItem> extends BaseAdapter<T> {
Expand Down
Loading

0 comments on commit 842b8f7

Please sign in to comment.