From 842b8f713986a6da57f315df953ceabab08038bc Mon Sep 17 00:00:00 2001 From: Sherlock Holmes <745453463@qq.com> Date: Mon, 26 Dec 2016 02:27:54 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E9=95=BF=E6=8C=89=E4=BA=8B?= =?UTF-8?q?=E4=BB=B6=EF=BC=8C=E6=B7=BB=E5=8A=A0=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 + .../java/xyz/zpayh/adapter/BaseAdapter.java | 130 ++++++++++++++-- .../zpayh/adapter/BaseExpandableAdapter.java | 86 +++++++++-- .../xyz/zpayh/adapter/BaseMultiAdapter.java | 7 +- .../xyz/zpayh/adapter/BaseViewHolder.java | 91 +++++++++++ .../java/xyz/zpayh/adapter/IExpandable.java | 16 +- .../java/xyz/zpayh/adapter/IMultiItem.java | 25 ++- .../zpayh/adapter/OnItemClickListener.java | 12 +- .../adapter/OnItemLongClickListener.java | 25 +++ .../xyz/zpayh/adapter/OnLoadMoreListener.java | 12 +- app/src/main/AndroidManifest.xml | 29 +++- .../zpayh/myadapter/ExpandableActivity.java | 19 ++- .../zpayh/myadapter/HeadAndFootActivity.java | 42 +++++ .../xyz/zpayh/myadapter/MainActivity.java | 143 +++--------------- .../zpayh/myadapter/MultiItemActivity.java | 29 +++- .../myadapter/adapter/HeadAndFootAdapter.java | 44 ++++++ .../adapter/MyExpandableAdapter.java | 6 +- .../myadapter/adapter/MyMultiAdapter.java | 5 +- .../xyz/zpayh/myadapter/data/Constant.java | 21 +++ .../xyz/zpayh/myadapter/data/Details.java | 36 +++++ .../java/xyz/zpayh/myadapter/data/Image.java | 36 +++++ .../java/xyz/zpayh/myadapter/data/Text.java | 36 +++++ .../java/xyz/zpayh/myadapter/data/Title.java | 64 ++++++++ .../res/layout/activity_head_and_foot.xml | 17 +++ app/src/main/res/layout/activity_main.xml | 8 - app/src/main/res/layout/content_main.xml | 17 +-- app/src/main/res/layout/item_foot.xml | 15 ++ app/src/main/res/layout/item_head.xml | 15 ++ app/src/main/res/layout/item_list.xml | 9 +- app/src/main/res/layout/item_text.xml | 19 +++ app/src/main/res/values/strings.xml | 8 + 31 files changed, 835 insertions(+), 189 deletions(-) create mode 100644 adapter/src/main/java/xyz/zpayh/adapter/OnItemLongClickListener.java create mode 100644 app/src/main/java/xyz/zpayh/myadapter/HeadAndFootActivity.java create mode 100644 app/src/main/java/xyz/zpayh/myadapter/adapter/HeadAndFootAdapter.java create mode 100644 app/src/main/java/xyz/zpayh/myadapter/data/Constant.java create mode 100644 app/src/main/java/xyz/zpayh/myadapter/data/Details.java create mode 100644 app/src/main/java/xyz/zpayh/myadapter/data/Image.java create mode 100644 app/src/main/java/xyz/zpayh/myadapter/data/Text.java create mode 100644 app/src/main/java/xyz/zpayh/myadapter/data/Title.java create mode 100644 app/src/main/res/layout/activity_head_and_foot.xml create mode 100644 app/src/main/res/layout/item_foot.xml create mode 100644 app/src/main/res/layout/item_head.xml create mode 100644 app/src/main/res/layout/item_text.xml diff --git a/README.md b/README.md index baf6b23..175620a 100644 --- a/README.md +++ b/README.md @@ -1 +1,3 @@ # SherlockAdapter + +TODO 完善README,测试方法,写好实例 diff --git a/adapter/src/main/java/xyz/zpayh/adapter/BaseAdapter.java b/adapter/src/main/java/xyz/zpayh/adapter/BaseAdapter.java index 25940ec..27457f7 100644 --- a/adapter/src/main/java/xyz/zpayh/adapter/BaseAdapter.java +++ b/adapter/src/main/java/xyz/zpayh/adapter/BaseAdapter.java @@ -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 extends RecyclerView.Adapter implements LoadMore{ - protected final List mData; + protected final List mData = new ArrayList<>(); private int mHeadLayouts[] = new int[0]; @@ -34,13 +37,13 @@ public abstract class BaseAdapter extends RecyclerView.Adapter extends RecyclerView.Adapter extends RecyclerView.Adapter(); - mEmptyLayout = R.layout.default_empty; - mErrorLayout = R.layout.default_error; - mLoadMoreLayout = R.layout.default_loadmore; - } + private OnItemLongClickListener mOnItemLongClickListener; /** * 设置新数据,会清除掉原有数据,并有可能重置加载更多状态 @@ -99,6 +97,25 @@ public void setData(@Nullable List 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 数据集合 @@ -118,6 +135,80 @@ public void addData(List 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 @@ -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 @@ -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); } diff --git a/adapter/src/main/java/xyz/zpayh/adapter/BaseExpandableAdapter.java b/adapter/src/main/java/xyz/zpayh/adapter/BaseExpandableAdapter.java index 0ff0e7b..6523ff3 100644 --- a/adapter/src/main/java/xyz/zpayh/adapter/BaseExpandableAdapter.java +++ b/adapter/src/main/java/xyz/zpayh/adapter/BaseExpandableAdapter.java @@ -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 { + /** + * 展开下级菜单 + */ 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 subItems = expandable.getSubItems(); + if (subItems == null || subItems.isEmpty()){ return; } - final List subItems = expandable.getSubItems(); - if (subItems == null || subItems.isEmpty()){ + + if (expandable.isExpandable() && mData.containsAll(subItems)){ return; } @@ -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 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 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 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); @@ -61,13 +127,13 @@ private int removeAll(List subItems){ if (subItem == null || !(subItem instanceof IExpandable)) continue; IExpandable expandable = (IExpandable) subItem; - if (!expandable.isExpandable()) - continue; - expandable.setExpandable(false); final List 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); diff --git a/adapter/src/main/java/xyz/zpayh/adapter/BaseMultiAdapter.java b/adapter/src/main/java/xyz/zpayh/adapter/BaseMultiAdapter.java index bbead4c..632f26b 100644 --- a/adapter/src/main/java/xyz/zpayh/adapter/BaseMultiAdapter.java +++ b/adapter/src/main/java/xyz/zpayh/adapter/BaseMultiAdapter.java @@ -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 extends BaseAdapter { diff --git a/adapter/src/main/java/xyz/zpayh/adapter/BaseViewHolder.java b/adapter/src/main/java/xyz/zpayh/adapter/BaseViewHolder.java index 270c4d6..6612a29 100644 --- a/adapter/src/main/java/xyz/zpayh/adapter/BaseViewHolder.java +++ b/adapter/src/main/java/xyz/zpayh/adapter/BaseViewHolder.java @@ -1,10 +1,15 @@ package xyz.zpayh.adapter; +import android.graphics.Bitmap; +import android.graphics.drawable.Drawable; import android.support.annotation.CheckResult; +import android.support.annotation.DrawableRes; import android.support.annotation.IdRes; +import android.support.annotation.Nullable; import android.support.annotation.StringRes; import android.support.v7.widget.RecyclerView; import android.view.View; +import android.widget.ImageView; import android.widget.TextView; import java.util.Arrays; @@ -36,9 +41,24 @@ public void onClick(View v) { } }; + private OnItemLongClickListener mOnItemLongClickListener; + + private boolean mInitLongClickListener; + + private final View.OnLongClickListener mOnLongClickListener = new View.OnLongClickListener() { + @Override + public boolean onLongClick(View v) { + if (mOnItemLongClickListener != null){ + return mOnItemLongClickListener.onItemLongClick(v,getAdapterPosition()); + } + return false; + } + }; + public BaseViewHolder(View itemView) { super(itemView); mInitClickListener = false; + mInitLongClickListener = false; } void setOnItemClickListener(OnItemClickListener onItemClickListener) { @@ -50,6 +70,15 @@ void setOnItemClickListener(OnItemClickListener onItemClickListener) { } } + void setOnItemLongClickListener(OnItemLongClickListener onItemLongClickListener){ + this.mOnItemLongClickListener = onItemLongClickListener; + + if (onItemLongClickListener != null && !mInitLongClickListener){ + mInitLongClickListener = true; + this.itemView.setOnLongClickListener(mOnLongClickListener); + } + } + /** * 设置响应点击事件,如果设置了clickable为true的话,在{@link BaseAdapter#setOnItemClickListener(OnItemClickListener)} * 中会得到响应事件的回调,详情参考{@link BaseAdapter#setOnItemClickListener(OnItemClickListener)} @@ -68,6 +97,24 @@ public BaseViewHolder setClickable(@IdRes int id, boolean clickable){ return this; } + /** + * 设置响应长按事件,如果设置了clickable为true的话,在{@link BaseAdapter#setOnItemClickListener(OnItemClickListener)} + * 中会得到响应事件的回调,详情参考{@link BaseAdapter#setOnItemClickListener(OnItemClickListener)} + * @param id 响应点击事件的View Id + * @param longClickable true响应长按事件,false不响应长按事件 + */ + public BaseViewHolder setLongClickable(@IdRes int id, boolean longClickable){ + View view = find(id); + if (view != null){ + if (longClickable){ + view.setOnClickListener(mOnClickListener); + }else{ + view.setOnClickListener(null); + } + } + return this; + } + public BaseViewHolder setText(@IdRes int textId, String text){ TextView textView = find(textId); if (textView != null){ @@ -81,6 +128,30 @@ public BaseViewHolder setText(@IdRes int textId, @StringRes int stringId){ return setText(textId,text); } + public BaseViewHolder setImage(@IdRes int imageId, @DrawableRes int resId){ + ImageView imageView = find(imageId); + if (imageView != null){ + imageView.setImageResource(resId); + } + return this; + } + + public BaseViewHolder setImage(@IdRes int imageId,@Nullable Drawable drawable){ + ImageView imageView = find(imageId); + if (imageView != null){ + imageView.setImageDrawable(drawable); + } + return this; + } + + public BaseViewHolder setImage(@IdRes int imageId,Bitmap bm){ + ImageView imageView = find(imageId); + if (imageView != null){ + imageView.setImageBitmap(bm); + } + return this; + } + /** * 设置View的visibility状态 * @param id View id @@ -124,4 +195,24 @@ public T find(@IdRes int viewId){ mIdsAndViews[indexToAdd+1] = itemView.findViewById(viewId); return (T) mIdsAndViews[indexToAdd+1]; } + + /** + * 根据当前id查找对应的ImageView控件 + * @param imageId ImageView id + * @return 返回当前id对应的ImageView控件,如果没有,则返回null + */ + @CheckResult + public ImageView findImage(@IdRes int imageId){ + return find(imageId); + } + + /** + * 根据当前id查找对应的TextView控件 + * @param textId TextView id + * @return 返回当前id对应的TextView控件,如果没有,则返回null + */ + @CheckResult + public TextView findText(@IdRes int textId){ + return find(textId); + } } diff --git a/adapter/src/main/java/xyz/zpayh/adapter/IExpandable.java b/adapter/src/main/java/xyz/zpayh/adapter/IExpandable.java index 08f9397..170684c 100644 --- a/adapter/src/main/java/xyz/zpayh/adapter/IExpandable.java +++ b/adapter/src/main/java/xyz/zpayh/adapter/IExpandable.java @@ -3,14 +3,28 @@ import java.util.List; /** - * Created by Administrator on 2016/12/23. + * 文 件 名: IExpandable + * 创 建 人: 陈志鹏 + * 创建日期: 2016/12/25 02:38 + * 邮 箱: ch_zh_p@qq.com + * 修改时间: + * 修改备注: */ public interface IExpandable extends IMultiItem{ + /** + * 用来判断区分是否展开 + */ boolean isExpandable(); + /** + * 设置展开状态 + */ void setExpandable(boolean expandable); + /** + * 返回可以展开的子列表 + */ List getSubItems(); } diff --git a/adapter/src/main/java/xyz/zpayh/adapter/IMultiItem.java b/adapter/src/main/java/xyz/zpayh/adapter/IMultiItem.java index 95ab8e8..b45d3a1 100644 --- a/adapter/src/main/java/xyz/zpayh/adapter/IMultiItem.java +++ b/adapter/src/main/java/xyz/zpayh/adapter/IMultiItem.java @@ -1,16 +1,39 @@ package xyz.zpayh.adapter; import android.support.annotation.LayoutRes; +import android.support.v7.widget.GridLayoutManager; +import android.support.v7.widget.RecyclerView; /** - * Created by Administrator on 2016/12/23. + * 文 件 名: IMultiItem + * 创 建 人: 陈志鹏 + * 创建日期: 2016/12/25 02:38 + * 邮 箱: ch_zh_p@qq.com + * 修改时间: + * 修改备注: */ public interface IMultiItem { + /** + * 不同类型的item请使用不同的布局文件, + * 即使它们的布局是一样的,也要copy多一份出来。 + * @return 返回item对应的布局id + */ @LayoutRes int getLayoutRes(); + /** + * + * @return 返回item类型,请返回一个唯一识别符 + */ int getViewType(); + /** + * 在布局为{@link android.support.v7.widget.GridLayoutManager}时才有用处, + * 返回当前布局所占用的SpanSize + * @return 如果返回的SpanSize <= 0 或者 > {@link GridLayoutManager#getSpanCount()} + * 则{@link BaseAdapter} 会在{@link BaseAdapter#onAttachedToRecyclerView(RecyclerView)} + * 自适应为1或者{@link GridLayoutManager#getSpanCount()},详情参考{@link BaseAdapter#onAttachedToRecyclerView(RecyclerView)} + */ int getSpanSize(); } diff --git a/adapter/src/main/java/xyz/zpayh/adapter/OnItemClickListener.java b/adapter/src/main/java/xyz/zpayh/adapter/OnItemClickListener.java index 54055fc..446838c 100644 --- a/adapter/src/main/java/xyz/zpayh/adapter/OnItemClickListener.java +++ b/adapter/src/main/java/xyz/zpayh/adapter/OnItemClickListener.java @@ -4,10 +4,20 @@ import android.view.View; /** - * Created by Administrator on 2016/12/19. + * 文 件 名: OnItemClickListener + * 创 建 人: 陈志鹏 + * 创建日期: 2016/12/25 02:38 + * 邮 箱: ch_zh_p@qq.com + * 修改时间: + * 修改备注: */ public interface OnItemClickListener { + /** + * 响应点击事件监听回调 + * @param view 响应事件的View + * @param adapterPosition item所在的位置 + */ void onItemClick(@NonNull View view, int adapterPosition); } diff --git a/adapter/src/main/java/xyz/zpayh/adapter/OnItemLongClickListener.java b/adapter/src/main/java/xyz/zpayh/adapter/OnItemLongClickListener.java new file mode 100644 index 0000000..68312da --- /dev/null +++ b/adapter/src/main/java/xyz/zpayh/adapter/OnItemLongClickListener.java @@ -0,0 +1,25 @@ +package xyz.zpayh.adapter; + +import android.support.annotation.NonNull; +import android.view.View; + +/** + * 文 件 名: OnItemLongClickListener + * 创 建 人: 陈志鹏 + * 创建日期: 2016/12/25 02:38 + * 邮 箱: ch_zh_p@qq.com + * 修改时间: + * 修改备注: + */ + +public interface OnItemLongClickListener { + + /** + * 长按事件监听接口 + * @param view 响应事件的View + * @param adapterPosition item在列表中的位置 + * @return true 响应事件 + * false 不响应事件 + */ + boolean onItemLongClick(@NonNull View view, int adapterPosition); +} diff --git a/adapter/src/main/java/xyz/zpayh/adapter/OnLoadMoreListener.java b/adapter/src/main/java/xyz/zpayh/adapter/OnLoadMoreListener.java index 2fbe332..999919a 100644 --- a/adapter/src/main/java/xyz/zpayh/adapter/OnLoadMoreListener.java +++ b/adapter/src/main/java/xyz/zpayh/adapter/OnLoadMoreListener.java @@ -1,9 +1,19 @@ package xyz.zpayh.adapter; + /** - * Created by Administrator on 2016/12/20. + * 文 件 名: IMultiItem + * 创 建 人: 陈志鹏 + * 创建日期: 2016/12/25 02:38 + * 邮 箱: ch_zh_p@qq.com + * 修改时间: + * 修改备注: */ public interface OnLoadMoreListener { + + /** + * 加载更多回调接口 + */ void onLoadMore(); } diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index ba1742f..f97c945 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -1,6 +1,6 @@ + package="xyz.zpayh.myadapter"> - + - + + android:theme="@style/AppTheme.NoActionBar"> + + + + + + + android:theme="@style/AppTheme.NoActionBar"> + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/java/xyz/zpayh/myadapter/ExpandableActivity.java b/app/src/main/java/xyz/zpayh/myadapter/ExpandableActivity.java index 7f44e20..c89d408 100644 --- a/app/src/main/java/xyz/zpayh/myadapter/ExpandableActivity.java +++ b/app/src/main/java/xyz/zpayh/myadapter/ExpandableActivity.java @@ -62,13 +62,18 @@ public void onClick(View view) { data.add(title); } Title title = new Title("Sherlock"); - Title subTitle = new Title("你开心就好"); - int size = random.nextInt(2)+2; - for (int i = 0; i < size; i++){ - Details details = new Details("我是内容"+i); - subTitle.add(details); + for (int j = 0; j < 3; j++) { + Title subTitle = new Title("你开心就好"+j); + if (j == 1){ + subTitle.setExpandable(true); + } + int size = random.nextInt(2) + 2; + for (int i = 0; i < size; i++) { + Details details = new Details("我是内容" + i+","+j); + subTitle.add(details); + } + title.add(subTitle); } - title.add(subTitle); data.add(3,title); adapter.setOnItemClickListener(new OnItemClickListener() { @@ -81,7 +86,7 @@ public void onItemClick(@NonNull View view, int adapterPosition) { if (expandable.isExpandable()){ adapter.collapse(adapterPosition); }else{ - adapter.expand(adapterPosition); + adapter.expandAll(adapterPosition); } } } diff --git a/app/src/main/java/xyz/zpayh/myadapter/HeadAndFootActivity.java b/app/src/main/java/xyz/zpayh/myadapter/HeadAndFootActivity.java new file mode 100644 index 0000000..afc47c5 --- /dev/null +++ b/app/src/main/java/xyz/zpayh/myadapter/HeadAndFootActivity.java @@ -0,0 +1,42 @@ +package xyz.zpayh.myadapter; + +import android.os.Bundle; +import android.support.v7.app.AppCompatActivity; +import android.support.v7.widget.LinearLayoutManager; +import android.support.v7.widget.RecyclerView; + +import java.util.ArrayList; +import java.util.List; + +import xyz.zpayh.myadapter.adapter.HeadAndFootAdapter; + +public class HeadAndFootActivity extends AppCompatActivity { + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_head_and_foot); + + RecyclerView recyclerView = (RecyclerView) findViewById(R.id.list); + recyclerView.setLayoutManager(new LinearLayoutManager(this)); + + HeadAndFootAdapter adapter = new HeadAndFootAdapter(); + + recyclerView.setAdapter(adapter); + + adapter.addHeadLayout(R.layout.item_head); + adapter.addHeadLayout(R.layout.item_head); + adapter.addHeadLayout(R.layout.item_head); + adapter.addFootLayout(R.layout.item_foot); + adapter.addFootLayout(R.layout.item_foot); + + List data = new ArrayList<>(); + + String[] list = getResources().getStringArray(R.array.list); + for (String s : list) { + data.add(s); + } + + adapter.setData(data); + } +} diff --git a/app/src/main/java/xyz/zpayh/myadapter/MainActivity.java b/app/src/main/java/xyz/zpayh/myadapter/MainActivity.java index 8a884b2..42b74cd 100644 --- a/app/src/main/java/xyz/zpayh/myadapter/MainActivity.java +++ b/app/src/main/java/xyz/zpayh/myadapter/MainActivity.java @@ -3,36 +3,23 @@ import android.content.Intent; import android.os.Bundle; import android.support.annotation.NonNull; -import android.support.design.widget.FloatingActionButton; -import android.support.design.widget.Snackbar; -import android.support.v4.widget.SwipeRefreshLayout; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.Toolbar; -import android.util.Log; -import android.view.Menu; -import android.view.MenuItem; import android.view.View; -import android.widget.Toast; import java.util.ArrayList; import java.util.List; +import xyz.zpayh.adapter.BaseAdapter; +import xyz.zpayh.adapter.BaseViewHolder; import xyz.zpayh.adapter.OnItemClickListener; -import xyz.zpayh.adapter.OnLoadMoreListener; -import xyz.zpayh.myadapter.adapter.MyAdapter; public class MainActivity extends AppCompatActivity { private RecyclerView mRecyclerView; - private SwipeRefreshLayout mRefreshLayout; - - private MyAdapter mMyAdapter; - - private int mLoadState = 0; - @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -44,123 +31,43 @@ private void initView() { Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); - final FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); - fab.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View view) { - Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) - .setAction("Action", null).show(); - mMyAdapter.showErrorView(); - startActivity(new Intent(MainActivity.this,MultiItemActivity.class)); - } - }); - mRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.refresh); mRecyclerView = (RecyclerView) findViewById(R.id.list); mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); - //mRecyclerView.setLayoutManager(new GridLayoutManager(this,2)); - //mRecyclerView.setLayoutManager(new StaggeredGridLayoutManager(4,1)); - mMyAdapter = new MyAdapter(); - mRecyclerView.setAdapter(mMyAdapter); - String[] list = getResources().getStringArray(R.array.list); - final List data = new ArrayList<>(list.length); - for (String s : list) { - data.add(s); - } - - //mMyAdapter.addHeadLayout(android.R.layout.simple_list_item_1); - mMyAdapter.addFootLayout(android.R.layout.simple_list_item_1); - - mMyAdapter.openAutoLoadMore(true); - mMyAdapter.setOnLoadMoreListener(new OnLoadMoreListener() { + final BaseAdapter adapter = new BaseAdapter() { @Override - public void onLoadMore() { - Log.d("MainActivity", "加载更多"); - - mRecyclerView.postDelayed(new Runnable() { - @Override - public void run() { - if (mLoadState == 0){ - mMyAdapter.addData(data); - }else if(mLoadState == 1){ - mMyAdapter.loadCompleted(); - }else if (mLoadState == 2){ - mMyAdapter.loadFailed(); - } - } - },2000); + public int getLayoutRes(int index) { + return R.layout.item_list; } - }); - mMyAdapter.setOnItemClickListener(new OnItemClickListener() { + @Override - public void onItemClick(@NonNull View view, int adapterPosition) { - String data = mMyAdapter.getData(adapterPosition); - Toast.makeText(MainActivity.this, data, Toast.LENGTH_SHORT).show(); - /*if (view instanceof TextView){ - Toast.makeText(view.getContext(), ((TextView)view).getText(), Toast.LENGTH_SHORT).show(); - }*/ + public void convert(BaseViewHolder holder, String data, int index) { + holder.setText(R.id.tv_act_title,data); } - }); - mRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { @Override - public void onRefresh() { - mRecyclerView.postDelayed(new Runnable() { - @Override - public void run() { - mMyAdapter.setData(data); - mRefreshLayout.setRefreshing(false); - } - },2000); + public void bind(BaseViewHolder holder, int layoutRes) { + holder.setClickable(R.id.app_root,true); + } + }; + adapter.setOnItemClickListener(new OnItemClickListener() { + @Override + public void onItemClick(@NonNull View view, int adapterPosition) { + String action = adapter.getData(adapterPosition); + Intent intent = new Intent(); + intent.setAction(action); + startActivity(intent); } }); - mMyAdapter.setData(data); - } - - @Override - public boolean onCreateOptionsMenu(Menu menu) { - // Inflate the menu; this adds items to the action bar if it is present. - getMenuInflater().inflate(R.menu.menu_main, menu); - return true; - } - - @Override - public boolean onOptionsItemSelected(MenuItem item) { - // Handle action bar item clicks here. The action bar will - // automatically handle clicks on the Home/Up button, so long - // as you specify a parent activity in AndroidManifest.xml. - int id = item.getItemId(); - - //noinspection SimplifiableIfStatement - if (id == R.id.action_empty) { - mMyAdapter.setData(null); - return true; - } - - if (id == R.id.action_add){ - mMyAdapter.openAutoLoadMore(true); - mLoadState = 0; - return true; - } + mRecyclerView.setAdapter(adapter); - if (id == R.id.action_completed){ - mMyAdapter.openAutoLoadMore(true); - mLoadState = 1; - return true; - } - - if (id == R.id.action_failed){ - mMyAdapter.openAutoLoadMore(true); - mLoadState = 2; - return true; - } - - if (id == R.id.action_close){ - mMyAdapter.openAutoLoadMore(false); - return true; + String[] list = getResources().getStringArray(R.array.activity_title); + final List data = new ArrayList<>(list.length); + for (String s : list) { + data.add(s); } - return super.onOptionsItemSelected(item); + adapter.setData(data); } } diff --git a/app/src/main/java/xyz/zpayh/myadapter/MultiItemActivity.java b/app/src/main/java/xyz/zpayh/myadapter/MultiItemActivity.java index f0c9791..efd1c96 100644 --- a/app/src/main/java/xyz/zpayh/myadapter/MultiItemActivity.java +++ b/app/src/main/java/xyz/zpayh/myadapter/MultiItemActivity.java @@ -1,9 +1,12 @@ package xyz.zpayh.myadapter; +import android.content.DialogInterface; import android.content.Intent; import android.os.Bundle; +import android.support.annotation.NonNull; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.Snackbar; +import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.GridLayoutManager; import android.support.v7.widget.RecyclerView; @@ -14,6 +17,7 @@ import java.util.List; import xyz.zpayh.adapter.IMultiItem; +import xyz.zpayh.adapter.OnItemLongClickListener; import xyz.zpayh.myadapter.adapter.MyMultiAdapter; import xyz.zpayh.myadapter.data.Image; import xyz.zpayh.myadapter.data.Text; @@ -45,9 +49,32 @@ public void onClick(View view) { RecyclerView recyclerView = (RecyclerView) findViewById(R.id.list); //recyclerView.setLayoutManager(new LinearLayoutManager(this)); recyclerView.setLayoutManager(new GridLayoutManager(this,3)); - MyMultiAdapter adapter = new MyMultiAdapter(); + final MyMultiAdapter adapter = new MyMultiAdapter(); recyclerView.setAdapter(adapter); + adapter.setOnItemLongClickListener(new OnItemLongClickListener() { + @Override + public boolean onItemLongClick(@NonNull View view, final int adapterPosition) { + new AlertDialog.Builder(MultiItemActivity.this) + .setTitle("是否删除"+adapterPosition+"项") + .setNegativeButton("是", new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + adapter.removeData(adapterPosition); + } + }) + .setPositiveButton("否", new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + + } + }) + .create() + .show(); + return true; + } + }); + List data = new ArrayList<>(); String[] list = getResources().getStringArray(R.array.list); diff --git a/app/src/main/java/xyz/zpayh/myadapter/adapter/HeadAndFootAdapter.java b/app/src/main/java/xyz/zpayh/myadapter/adapter/HeadAndFootAdapter.java new file mode 100644 index 0000000..36ce882 --- /dev/null +++ b/app/src/main/java/xyz/zpayh/myadapter/adapter/HeadAndFootAdapter.java @@ -0,0 +1,44 @@ +package xyz.zpayh.myadapter.adapter; + +import android.support.annotation.LayoutRes; + +import xyz.zpayh.adapter.BaseAdapter; +import xyz.zpayh.adapter.BaseViewHolder; +import xyz.zpayh.myadapter.R; + +/** + * 文 件 名: HeadAndFootAdapter + * 创 建 人: 陈志鹏 + * 创建日期: 2016/12/26 02:06 + * 邮 箱: ch_zh_p@qq.com + * 修改时间: + * 修改备注: + */ + +public class HeadAndFootAdapter extends BaseAdapter { + + @Override + public int getLayoutRes(int index) { + return R.layout.item_text; + } + + @Override + public void convert(BaseViewHolder holder, String data, int index) { + holder.setText(R.id.text,data); + } + + @Override + public void bind(BaseViewHolder holder, int layoutRes) { + + } + + @Override + public void convertHead(BaseViewHolder holder, @LayoutRes int headLayout, int index) { + holder.setText(R.id.tv_head,"这是头部"+(index+1)); + } + + @Override + public void convertFoot(BaseViewHolder holder, @LayoutRes int footLayout, int index) { + holder.setText(R.id.tv_foot,"这是尾部"+(index+1)); + } +} diff --git a/app/src/main/java/xyz/zpayh/myadapter/adapter/MyExpandableAdapter.java b/app/src/main/java/xyz/zpayh/myadapter/adapter/MyExpandableAdapter.java index 754bf00..cf0f9f8 100644 --- a/app/src/main/java/xyz/zpayh/myadapter/adapter/MyExpandableAdapter.java +++ b/app/src/main/java/xyz/zpayh/myadapter/adapter/MyExpandableAdapter.java @@ -4,7 +4,7 @@ import xyz.zpayh.adapter.BaseViewHolder; import xyz.zpayh.adapter.IMultiItem; import xyz.zpayh.myadapter.R; -import xyz.zpayh.myadapter.data.Contanst; +import xyz.zpayh.myadapter.data.Constant; import xyz.zpayh.myadapter.data.Details; import xyz.zpayh.myadapter.data.Title; @@ -17,10 +17,10 @@ public class MyExpandableAdapter extends BaseExpandableAdapter { @Override public void convert(BaseViewHolder holder, IMultiItem data, int index) { - if (data.getViewType() == Contanst.DETAILS){ + if (data.getViewType() == Constant.DETAILS){ Details details = (Details) data; holder.setText(R.id.content,details.mContent); - }else if (data.getViewType() == Contanst.TITLE){ + }else if (data.getViewType() == Constant.TITLE){ Title title = (Title) data; holder.setText(R.id.title,title.mTitle); } diff --git a/app/src/main/java/xyz/zpayh/myadapter/adapter/MyMultiAdapter.java b/app/src/main/java/xyz/zpayh/myadapter/adapter/MyMultiAdapter.java index e4e7bde..9017b7d 100644 --- a/app/src/main/java/xyz/zpayh/myadapter/adapter/MyMultiAdapter.java +++ b/app/src/main/java/xyz/zpayh/myadapter/adapter/MyMultiAdapter.java @@ -6,6 +6,7 @@ import xyz.zpayh.adapter.BaseViewHolder; import xyz.zpayh.adapter.IMultiItem; import xyz.zpayh.myadapter.R; +import xyz.zpayh.myadapter.data.Constant; import xyz.zpayh.myadapter.data.Image; import xyz.zpayh.myadapter.data.Text; @@ -16,9 +17,9 @@ public class MyMultiAdapter extends BaseMultiAdapter { @Override public void convert(BaseViewHolder holder, IMultiItem data, int index) { - if (data.getViewType() == 0){ + if (data.getViewType() == Constant.TEXT){ holder.setText(R.id.text,((Text)data).mText); - }else if (data.getViewType() == 1){ + }else if (data.getViewType() == Constant.IMAGE){ ((ImageView)holder.find(R.id.image)).setImageResource(((Image)data).mImageId); } } diff --git a/app/src/main/java/xyz/zpayh/myadapter/data/Constant.java b/app/src/main/java/xyz/zpayh/myadapter/data/Constant.java new file mode 100644 index 0000000..1897ee2 --- /dev/null +++ b/app/src/main/java/xyz/zpayh/myadapter/data/Constant.java @@ -0,0 +1,21 @@ +package xyz.zpayh.myadapter.data; + +/** + * 文 件 名: Constant + * 创 建 人: 陈志鹏 + * 创建日期: 2016/12/23 19:40 + * 邮 箱: ch_zh_p@qq.com + * 修改时间: + * 修改备注: + */ + +public interface Constant { + + int TEXT = 0; + + int IMAGE = 1; + + int DETAILS = 2; + + int TITLE = 3; +} diff --git a/app/src/main/java/xyz/zpayh/myadapter/data/Details.java b/app/src/main/java/xyz/zpayh/myadapter/data/Details.java new file mode 100644 index 0000000..c0cc6b1 --- /dev/null +++ b/app/src/main/java/xyz/zpayh/myadapter/data/Details.java @@ -0,0 +1,36 @@ +package xyz.zpayh.myadapter.data; + +import xyz.zpayh.adapter.IMultiItem; +import xyz.zpayh.myadapter.R; + +/** + * 文 件 名: Details + * 创 建 人: 陈志鹏 + * 创建日期: 2016/12/23 19:42 + * 邮 箱: ch_zh_p@qq.com + * 修改时间: + * 修改备注: + */ + +public class Details implements IMultiItem{ + public String mContent; + + public Details(String content) { + mContent = content; + } + + @Override + public int getLayoutRes() { + return R.layout.item_details; + } + + @Override + public int getViewType() { + return Constant.DETAILS; + } + + @Override + public int getSpanSize() { + return 0; + } +} diff --git a/app/src/main/java/xyz/zpayh/myadapter/data/Image.java b/app/src/main/java/xyz/zpayh/myadapter/data/Image.java new file mode 100644 index 0000000..de7e77c --- /dev/null +++ b/app/src/main/java/xyz/zpayh/myadapter/data/Image.java @@ -0,0 +1,36 @@ +package xyz.zpayh.myadapter.data; + +import xyz.zpayh.adapter.IMultiItem; +import xyz.zpayh.myadapter.R; + +/** + * 文 件 名: Image + * 创 建 人: 陈志鹏 + * 创建日期: 2016/12/23 19:42 + * 邮 箱: ch_zh_p@qq.com + * 修改时间: + * 修改备注: + */ + +public class Image implements IMultiItem{ + public int mImageId; + + public Image(int imageId) { + mImageId = imageId; + } + + @Override + public int getLayoutRes() { + return R.layout.item_image; + } + + @Override + public int getViewType() { + return Constant.IMAGE; + } + + @Override + public int getSpanSize() { + return 0; + } +} diff --git a/app/src/main/java/xyz/zpayh/myadapter/data/Text.java b/app/src/main/java/xyz/zpayh/myadapter/data/Text.java new file mode 100644 index 0000000..6b25940 --- /dev/null +++ b/app/src/main/java/xyz/zpayh/myadapter/data/Text.java @@ -0,0 +1,36 @@ +package xyz.zpayh.myadapter.data; + +import xyz.zpayh.adapter.IMultiItem; +import xyz.zpayh.myadapter.R; + +/** + * 文 件 名: Text + * 创 建 人: 陈志鹏 + * 创建日期: 2016/12/23 19:41 + * 邮 箱: ch_zh_p@qq.com + * 修改时间: + * 修改备注: + */ + +public class Text implements IMultiItem{ + public String mText; + + public Text(String text) { + mText = text; + } + + @Override + public int getLayoutRes() { + return R.layout.item_text; + } + + @Override + public int getViewType() { + return Constant.TEXT; + } + + @Override + public int getSpanSize() { + return 0; + } +} diff --git a/app/src/main/java/xyz/zpayh/myadapter/data/Title.java b/app/src/main/java/xyz/zpayh/myadapter/data/Title.java new file mode 100644 index 0000000..e72cc52 --- /dev/null +++ b/app/src/main/java/xyz/zpayh/myadapter/data/Title.java @@ -0,0 +1,64 @@ +package xyz.zpayh.myadapter.data; + +import java.util.ArrayList; +import java.util.List; + +import xyz.zpayh.adapter.IExpandable; +import xyz.zpayh.adapter.IMultiItem; +import xyz.zpayh.myadapter.R; + +/** + * 文 件 名: Title + * 创 建 人: 陈志鹏 + * 创建日期: 2016/12/23 19:42 + * 邮 箱: ch_zh_p@qq.com + * 修改时间: + * 修改备注: + */ + +public class Title implements IExpandable{ + private boolean mExpandable; + + private final List mItems; + + public String mTitle; + + public Title(String title) { + mTitle = title; + mItems = new ArrayList<>(); + } + + public void add(IMultiItem item){ + mItems.add(item); + } + + @Override + public boolean isExpandable() { + return mExpandable; + } + + @Override + public void setExpandable(boolean expandable) { + mExpandable = expandable; + } + + @Override + public List getSubItems() { + return mItems; + } + + @Override + public int getLayoutRes() { + return R.layout.item_title; + } + + @Override + public int getViewType() { + return Constant.TITLE; + } + + @Override + public int getSpanSize() { + return 0; + } +} diff --git a/app/src/main/res/layout/activity_head_and_foot.xml b/app/src/main/res/layout/activity_head_and_foot.xml new file mode 100644 index 0000000..6ddd6c7 --- /dev/null +++ b/app/src/main/res/layout/activity_head_and_foot.xml @@ -0,0 +1,17 @@ + + + + diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index bd1a879..007429d 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -24,12 +24,4 @@ - - diff --git a/app/src/main/res/layout/content_main.xml b/app/src/main/res/layout/content_main.xml index 8e219ab..f1b3380 100644 --- a/app/src/main/res/layout/content_main.xml +++ b/app/src/main/res/layout/content_main.xml @@ -6,21 +6,12 @@ android:id="@+id/content_main" android:layout_width="match_parent" android:layout_height="match_parent" - android:paddingBottom="@dimen/activity_vertical_margin" - android:paddingLeft="@dimen/activity_horizontal_margin" - android:paddingRight="@dimen/activity_horizontal_margin" - android:paddingTop="@dimen/activity_vertical_margin" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context="xyz.zpayh.myadapter.MainActivity" tools:showIn="@layout/activity_main"> - - - + diff --git a/app/src/main/res/layout/item_foot.xml b/app/src/main/res/layout/item_foot.xml new file mode 100644 index 0000000..b6ff0fc --- /dev/null +++ b/app/src/main/res/layout/item_foot.xml @@ -0,0 +1,15 @@ + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/item_head.xml b/app/src/main/res/layout/item_head.xml new file mode 100644 index 0000000..7f63e44 --- /dev/null +++ b/app/src/main/res/layout/item_head.xml @@ -0,0 +1,15 @@ + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/item_list.xml b/app/src/main/res/layout/item_list.xml index 7d7ab81..052512b 100644 --- a/app/src/main/res/layout/item_list.xml +++ b/app/src/main/res/layout/item_list.xml @@ -1,17 +1,18 @@ diff --git a/app/src/main/res/layout/item_text.xml b/app/src/main/res/layout/item_text.xml new file mode 100644 index 0000000..8964e2f --- /dev/null +++ b/app/src/main/res/layout/item_text.xml @@ -0,0 +1,19 @@ + + + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 04dfee2..ece01a0 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -7,6 +7,14 @@ 关闭加载 空数据 + + + HeadAndFootActivity + MultiItemActivity + ExpandableActivity + + + Android iOS