diff --git a/README.md b/README.md index 175620a..0d613bc 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,86 @@ # SherlockAdapter -TODO 完善README,测试方法,写好实例 +一个封装了RecyclerView.Adapter一些常用功能的库。 +## 封装的功能 +- item 的点击事件 +- item 的长按事件 +- item 的子View对应的点击事件 +- item 的子View对应的长按事件 +- 自动加载更多功能,内置一个基础的加载更多界面,如果有需要,可以自定义界面 +- Empty界面,已经内置了一个基础的Empty界面,如果有需要,可以自定义界面 +- Failed界面,已经内置了一个基础的Failed界面,如果有需要,可以自定义界面 +- 支持添加任意数量的HeadLayout +- 支持添加任意数量的FootLayout +- 支持多布局数据界面 +- 支持伸缩子项,理论上无层次限制 + +## 注意事项 +先说注意事项,一般来讲,由于SherlockAdapter采用LayoutRes的值来作为ItemViewType返回,而ItemViewType是用来区分不同的Item的,所以如果不是同种Item,就不要使用同一个Layout文件,例如头部HeadLayout跟ItemLayout的布局是一样的情况下,就复制多一个Layout出来就行,不要共用一个Layout。 + +## 使用方法 + +一般用法,继承BaseAdapter实现三个抽象方法即可: +``` java +public abstract class BaseAdapter extends RecyclerView.Adapter{ + + \\.... + + /** + * 返回布局layout + */ + @LayoutRes + public abstract int getLayoutRes(int index); + + /** + * 在这里设置显示 + */ + public abstract void convert(BaseViewHolder holder, T data, int index); + + /** + * 开启子view的点击事件,或者其他监听 + */ + public abstract void bind(BaseViewHolder holder,int layoutRes); +} +``` +### 设置点击事件 +在`bind(BaseViewHolder holder,int layoutRes)`里调用`holder.setClickable(ID,true);`启用item的子view的点击事件,并设置一下`BaseAdapter.setOnItemClickListener()`就可以了,详情参考MainActivity里的Adapter。如果只设置了点击事件,没有启用子view的点击,则是itemView响应消息。 +### 设置长按事件 +在`bind(BaseViewHolder holder,int layoutRes)`里调用`holder.setLongClickable(ID,true);`启用item的子view的长按事件,并设置一下`BaseAdapter.setOnItemLongClickListener()`就可以了,如果只设置了点击事件,没有启用子view的点击,则是itemView响应消息。基本使用方法与点击事件类似,具体参考Demo中的*MultiItemActivity*. +### 开启自动加载更多功能 +参考 *AutoLoadMoreActivity* 的代码: +``` java + //必须设置事件监听与开启auto + mAdapter.openAutoLoadMore(true); + mAdapter.setOnLoadMoreListener(new OnLoadMoreListener() { + @Override + public void onLoadMore() { + //TODO Do something + } + }); +``` + +### 支持多布局 +继承*BaseMultiAdapter*抽象类,数据类型实现*IMultiItem*接口即可。 +具体参考Demo中的*MultiItemActivity* +### 支持伸缩子项 +继承BaseExpandableAdapter,如果有可子项需要伸缩,数据类型实现*IExpandable*,子项数据类型实现*IMultiItem*,如果 +没有子项可伸缩,则数据类型实现*IMultiItem*即可,如果子项也有它的子项,则子项也需要实现*IExpandable*,子项的子项数据类型 +实现*IMultiItem*接口。详情参考Demo中的*ExpandableActivity*。 +更多细节请下载Demo查看源代码。 + +## License + +> Copyright (C) 2016 zpayh. + http://zpayh.xyz +> +> 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. diff --git a/adapter/src/main/java/xyz/zpayh/adapter/BaseAdapter.java b/adapter/src/main/java/xyz/zpayh/adapter/BaseAdapter.java index 27457f7..860c9fe 100644 --- a/adapter/src/main/java/xyz/zpayh/adapter/BaseAdapter.java +++ b/adapter/src/main/java/xyz/zpayh/adapter/BaseAdapter.java @@ -655,10 +655,19 @@ public boolean canAutoLoadMore() { //======================= LoadMore ========================== + /** + * 返回布局layout + */ @LayoutRes public abstract int getLayoutRes(int index); + /** + * 在这里设置显示 + */ public abstract void convert(BaseViewHolder holder, T data, int index); + /** + * 开启子view的点击事件,或者其他监听 + */ public abstract void bind(BaseViewHolder holder,int layoutRes); } diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index f97c945..724314f 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -1,6 +1,6 @@ + package="xyz.zpayh.myadapter"> - + - + - + - + - + - + - + - + + + + + + + + diff --git a/app/src/main/java/xyz/zpayh/myadapter/AutoLoadMoreActivity.java b/app/src/main/java/xyz/zpayh/myadapter/AutoLoadMoreActivity.java new file mode 100644 index 0000000..52a8d59 --- /dev/null +++ b/app/src/main/java/xyz/zpayh/myadapter/AutoLoadMoreActivity.java @@ -0,0 +1,115 @@ +package xyz.zpayh.myadapter; + +import android.os.Bundle; +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.view.View; + +import java.util.ArrayList; +import java.util.List; + +import xyz.zpayh.adapter.OnLoadMoreListener; +import xyz.zpayh.myadapter.adapter.MyAdapter; + +public class AutoLoadMoreActivity extends AppCompatActivity implements View.OnClickListener{ + + public static final int LOAD_ADD = 0; + public static final int LOAD_FAILED = 1; + public static final int LOAD_COMPLETED = 2; + + private boolean mShowLoadMore; + + private int mState = LOAD_ADD; + + private MyAdapter mAdapter; + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.act_auto_load_more); + + final RecyclerView recyclerView = (RecyclerView) findViewById(R.id.list); + recyclerView.setLayoutManager(new LinearLayoutManager(this)); + mAdapter = new MyAdapter(); + recyclerView.setAdapter(mAdapter); + final SwipeRefreshLayout refreshLayout = (SwipeRefreshLayout) findViewById(R.id.refresh); + + // 模拟数据 + final List data = new ArrayList<>(); + + String[] list = getResources().getStringArray(R.array.list); + for (String s : list) { + data.add(s); + } + + refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { + @Override + public void onRefresh() { + //模拟刷新 + recyclerView.postDelayed(new Runnable() { + @Override + public void run() { + refreshLayout.setRefreshing(false); + mAdapter.setData(data); + } + },1500); + } + }); + + //必须设置事件监听与开启auto + mAdapter.openAutoLoadMore(true); + mAdapter.setOnLoadMoreListener(new OnLoadMoreListener() { + @Override + public void onLoadMore() { + //模拟加载更多 + recyclerView.postDelayed(new Runnable() { + @Override + public void run() { + if (mState == LOAD_ADD){ + mAdapter.addData(data); + }else if (mState == LOAD_COMPLETED){ + mAdapter.loadCompleted(); + }else if (mState == LOAD_FAILED){ + mAdapter.loadFailed(); + } + } + },1500); + } + }); + + findViewById(R.id.action_add).setOnClickListener(this); + findViewById(R.id.action_failed).setOnClickListener(this); + findViewById(R.id.action_completed).setOnClickListener(this); + findViewById(R.id.action_empty).setOnClickListener(this); + findViewById(R.id.action_close).setOnClickListener(this); + } + + @Override + public void onClick(View v) { + switch (v.getId()){ + case R.id.action_add: + setTitle("加载更多"); + mAdapter.openAutoLoadMore(true); + mState = LOAD_ADD; + break; + case R.id.action_failed: + setTitle("加载更多失败"); + mAdapter.openAutoLoadMore(true); + mState = LOAD_FAILED; + break; + case R.id.action_completed: + setTitle("没有更多数据"); + mAdapter.openAutoLoadMore(true); + mState = LOAD_COMPLETED; + break; + case R.id.action_empty: + setTitle("没有数据"); + mAdapter.setData(null); + break; + case R.id.action_close: + mAdapter.openAutoLoadMore(false); + break; + } + } +} diff --git a/app/src/main/java/xyz/zpayh/myadapter/MainActivity.java b/app/src/main/java/xyz/zpayh/myadapter/MainActivity.java index 42b74cd..5cd48be 100644 --- a/app/src/main/java/xyz/zpayh/myadapter/MainActivity.java +++ b/app/src/main/java/xyz/zpayh/myadapter/MainActivity.java @@ -67,7 +67,6 @@ public void onItemClick(@NonNull View view, int adapterPosition) { for (String s : list) { data.add(s); } - adapter.setData(data); } } diff --git a/app/src/main/java/xyz/zpayh/myadapter/adapter/MyAdapter.java b/app/src/main/java/xyz/zpayh/myadapter/adapter/MyAdapter.java index 94cf383..a755a22 100644 --- a/app/src/main/java/xyz/zpayh/myadapter/adapter/MyAdapter.java +++ b/app/src/main/java/xyz/zpayh/myadapter/adapter/MyAdapter.java @@ -25,7 +25,7 @@ public int getLayoutRes(int position) { @Override public void convert(BaseViewHolder holder, String data, int index) { - holder.setText(R.id.text,data); + holder.setText(R.id.tv_act_title,data); } @Override @@ -40,6 +40,7 @@ public void convertFoot(BaseViewHolder holder, @LayoutRes int footLayout, int in @Override public void bind(BaseViewHolder holder, int viewType) { - holder.setClickable(R.id.text,true); + holder.setClickable(R.id.tv_act_title,true) + .setLongClickable(R.id.tv_act_title,true); } } diff --git a/app/src/main/res/layout/act_auto_load_more.xml b/app/src/main/res/layout/act_auto_load_more.xml new file mode 100644 index 0000000..e359d6d --- /dev/null +++ b/app/src/main/res/layout/act_auto_load_more.xml @@ -0,0 +1,82 @@ + + + + + + + + + + + + + + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index ece01a0..3a38d77 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -10,6 +10,7 @@ HeadAndFootActivity + AutoLoadMoreActivity MultiItemActivity ExpandableActivity