Skip to content

Commit

Permalink
初始化1.0版本
Browse files Browse the repository at this point in the history
  • Loading branch information
EvilBT committed Dec 28, 2016
1 parent 842b8f7 commit e198ff8
Show file tree
Hide file tree
Showing 8 changed files with 310 additions and 13 deletions.
85 changes: 84 additions & 1 deletion README.md
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.
9 changes: 9 additions & 0 deletions adapter/src/main/java/xyz/zpayh/adapter/BaseAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
25 changes: 16 additions & 9 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="xyz.zpayh.myadapter">
package="xyz.zpayh.myadapter">

<application
android:allowBackup="true"
Expand All @@ -13,36 +13,43 @@
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER"/>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MultiItemActivity"
android:label="@string/title_activity_multi_item"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="MultiItemActivity"/>
<action android:name="MultiItemActivity" />

<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".ExpandableActivity"
android:label="@string/title_activity_expandable"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="ExpandableActivity"/>
<action android:name="ExpandableActivity" />

<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".HeadAndFootActivity">
<intent-filter>
<action android:name="HeadAndFootActivity"/>
<action android:name="HeadAndFootActivity" />

<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".AutoLoadMoreActivity">
<intent-filter>
<action android:name="AutoLoadMoreActivity" />

<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
Expand Down
115 changes: 115 additions & 0 deletions app/src/main/java/xyz/zpayh/myadapter/AutoLoadMoreActivity.java
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;
}
}
}
1 change: 0 additions & 1 deletion app/src/main/java/xyz/zpayh/myadapter/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ public void onItemClick(@NonNull View view, int adapterPosition) {
for (String s : list) {
data.add(s);
}

adapter.setData(data);
}
}
5 changes: 3 additions & 2 deletions app/src/main/java/xyz/zpayh/myadapter/adapter/MyAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
}
82 changes: 82 additions & 0 deletions app/src/main/res/layout/act_auto_load_more.xml
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>
Loading

0 comments on commit e198ff8

Please sign in to comment.