-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
310 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<T>实现三个抽象方法即可: | ||
``` java | ||
public abstract class BaseAdapter<T> extends RecyclerView.Adapter<BaseViewHolder>{ | ||
|
||
\\.... | ||
|
||
/** | ||
* 返回布局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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
app/src/main/java/xyz/zpayh/myadapter/AutoLoadMoreActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<String> 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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
android:id="@+id/act_auto_load_more" | ||
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" | ||
tools:context="xyz.zpayh.myadapter.AutoLoadMoreActivity"> | ||
<LinearLayout | ||
android:background="@color/base_divider_color" | ||
android:layout_alignParentBottom="true" | ||
android:id="@+id/button_layout" | ||
android:orientation="horizontal" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content"> | ||
<TextView | ||
android:id="@+id/action_add" | ||
android:layout_width="0dp" | ||
android:layout_weight="1" | ||
android:gravity="center" | ||
android:textColor="@android:color/white" | ||
android:paddingTop="@dimen/fab_margin" | ||
android:paddingBottom="@dimen/fab_margin" | ||
android:text="@string/action_add" | ||
android:layout_height="wrap_content" /> | ||
<TextView | ||
android:id="@+id/action_failed" | ||
android:paddingTop="@dimen/fab_margin" | ||
android:paddingBottom="@dimen/fab_margin" | ||
android:layout_width="0dp" | ||
android:layout_weight="1" | ||
android:gravity="center" | ||
android:textColor="@android:color/white" | ||
android:text="@string/action_failed" | ||
android:layout_height="wrap_content" /> | ||
<TextView | ||
android:id="@+id/action_completed" | ||
android:paddingTop="@dimen/fab_margin" | ||
android:paddingBottom="@dimen/fab_margin" | ||
android:layout_width="0dp" | ||
android:layout_weight="1" | ||
android:gravity="center" | ||
android:textColor="@android:color/white" | ||
android:text="@string/action_completed" | ||
android:layout_height="wrap_content" /> | ||
<TextView | ||
android:id="@+id/action_empty" | ||
android:paddingTop="@dimen/fab_margin" | ||
android:paddingBottom="@dimen/fab_margin" | ||
android:layout_width="0dp" | ||
android:layout_weight="1" | ||
android:gravity="center" | ||
android:textColor="@android:color/white" | ||
android:text="@string/action_empty" | ||
android:layout_height="wrap_content" /> | ||
<TextView | ||
android:id="@+id/action_close" | ||
android:paddingTop="@dimen/fab_margin" | ||
android:paddingBottom="@dimen/fab_margin" | ||
android:layout_width="0dp" | ||
android:layout_weight="1" | ||
android:gravity="center" | ||
android:textColor="@android:color/white" | ||
android:text="@string/action_close" | ||
android:layout_height="wrap_content" /> | ||
</LinearLayout> | ||
<android.support.v4.widget.SwipeRefreshLayout | ||
android:id="@+id/refresh" | ||
android:layout_above="@+id/button_layout" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent"> | ||
<android.support.v7.widget.RecyclerView | ||
android:id="@+id/list" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent"/> | ||
</android.support.v4.widget.SwipeRefreshLayout> | ||
|
||
|
||
</RelativeLayout> |
Oops, something went wrong.