Skip to content

Commit

Permalink
更新README,更新Demo
Browse files Browse the repository at this point in the history
  • Loading branch information
EvilBT committed Dec 28, 2016
1 parent cfe9554 commit 838c93a
Show file tree
Hide file tree
Showing 13 changed files with 92 additions and 62 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public class HeadAndFootAdapter extends BaseAdapter<String> {
}
}
```
![添加头部](http://o9qzkbu2x.bkt.clouddn.com/8.jpg?imageMogr2/auto-orient/thumbnail/300x)
### 添加尾部FootLayout
``` java
adapter.addFootLayout(R.layout.item_foot);
Expand All @@ -89,6 +90,7 @@ public class HeadAndFootAdapter extends BaseAdapter<String> {
}
}
```
![添加尾部](http://o9qzkbu2x.bkt.clouddn.com/7.jpg?imageMogr2/auto-orient/thumbnail/300x)
具体参考[`HeadAndFootActivity`](https://github.com/EvilBT/SherlockAdapter/blob/master/app/src/main/java/xyz/zpayh/myadapter/HeadAndFootActivity.java)
### 设置点击事件
`bind(BaseViewHolder holder,int layoutRes)`里调用`holder.setClickable(ID,true);`启用item的子view的点击事件,并设置一下`BaseAdapter.setOnItemClickListener()`就可以了,详情参考[`MainActivity`](https://github.com/EvilBT/SherlockAdapter/blob/master/app/src/main/java/xyz/zpayh/myadapter/MainActivity.java)里的Adapter。如果只设置了点击事件,没有启用子view的点击,则是itemView响应消息。
Expand All @@ -106,16 +108,20 @@ public class HeadAndFootAdapter extends BaseAdapter<String> {
}
});
```

![正在自动加载](http://o9qzkbu2x.bkt.clouddn.com/4.jpg?imageMogr2/auto-orient/thumbnail/300x)
![完成加载](http://o9qzkbu2x.bkt.clouddn.com/1.jpg?imageMogr2/auto-orient/thumbnail/300x)
![加载失败](http://o9qzkbu2x.bkt.clouddn.com/3.jpg?imageMogr2/auto-orient/thumbnail/300x)
![没有数据](http://o9qzkbu2x.bkt.clouddn.com/2.jpg?imageMogr2/auto-orient/thumbnail/300x)
### 支持多布局
继承*BaseMultiAdapter*抽象类,数据类型实现*IMultiItem*接口即可。
具体参考Demo中的[`MultiItemActivity`](https://github.com/EvilBT/SherlockAdapter/blob/master/app/src/main/java/xyz/zpayh/myadapter/MultiItemActivity.java)
![多布局](http://o9qzkbu2x.bkt.clouddn.com/5.jpg?imageMogr2/auto-orient/thumbnail/300x)
### 支持伸缩子项
继承BaseExpandableAdapter,如果有可子项需要伸缩,数据类型实现*IExpandable*,子项数据类型实现*IMultiItem*,如果
没有子项可伸缩,则数据类型实现*IMultiItem*即可,如果子项也有它的子项,则子项也需要实现*IExpandable*,子项的子项数据类型
实现*IMultiItem*接口。详情参考Demo中的[`ExpandableActivity`](https://github.com/EvilBT/SherlockAdapter/blob/master/app/src/main/java/xyz/zpayh/myadapter/ExpandableActivity.java)
更多细节请下载Demo查看源代码。

![伸缩子项](http://o9qzkbu2x.bkt.clouddn.com/6.jpg?imageMogr2/auto-orient/thumbnail/300x)
## License

> Copyright (C) 2016 zpayh.
Expand Down
12 changes: 9 additions & 3 deletions adapter/src/main/java/xyz/zpayh/adapter/BaseAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ public abstract class BaseAdapter<T> extends RecyclerView.Adapter<BaseViewHolder
@LoadState
private int mLoadState;

private boolean mIsLoading = false;

private OnItemClickListener mOnItemClickListener;

private OnItemLongClickListener mOnItemLongClickListener;
Expand All @@ -92,6 +94,7 @@ public void setData(@Nullable List<T> data){

if (mOpenAutoLoadMore){
mLoadState = LOADING;
mIsLoading = false;
}
mShowErrorView = false;
notifyDataSetChanged();
Expand Down Expand Up @@ -130,6 +133,7 @@ public void addData(List<T> data){

if (mOpenAutoLoadMore){
mLoadState = LOADING;
mIsLoading = false;
}
mShowErrorView = false;
notifyItemRangeChanged(startPos,itemCount);
Expand Down Expand Up @@ -367,7 +371,8 @@ public final void onBindViewHolder(BaseViewHolder holder, int position) {
return;
}
if (canAutoLoadMore()){
if (mLoadState == LOADING){
if (mLoadState == LOADING && !mIsLoading){
mIsLoading = true;
mOnLoadMoreListener.onLoadMore();
}
//加载更多布局
Expand Down Expand Up @@ -610,7 +615,7 @@ public void showErrorView(){

@Override
public void openAutoLoadMore(boolean open) {
//mAutoLoadMore = true;
mIsLoading = false;
if (canAutoLoadMore()&&!open) {
notifyDataSetChanged();
}
Expand All @@ -624,6 +629,7 @@ public void openAutoLoadMore(boolean open) {
@Override
public void loadCompleted() {
mLoadState = LOAD_COMPLETED;
mIsLoading = false;
if (canAutoLoadMore()) {
notifyItemChanged(getItemCount()-1);
}
Expand All @@ -632,8 +638,8 @@ public void loadCompleted() {
@Override
public void loadFailed() {
mLoadState = LOAD_FAILED;
mIsLoading = false;
if (canAutoLoadMore()) {
//mLoadFailed = true;
notifyItemChanged(getItemCount()-1);
}
}
Expand Down
11 changes: 9 additions & 2 deletions app/src/main/java/xyz/zpayh/myadapter/AutoLoadMoreActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import java.util.ArrayList;
Expand All @@ -24,6 +25,8 @@ public class AutoLoadMoreActivity extends AppCompatActivity implements View.OnCl
private int mState = LOAD_ADD;

private MyAdapter mAdapter;

private List<String> data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -36,7 +39,7 @@ protected void onCreate(Bundle savedInstanceState) {
final SwipeRefreshLayout refreshLayout = (SwipeRefreshLayout) findViewById(R.id.refresh);

// 模拟数据
final List<String> data = new ArrayList<>();
data = new ArrayList<>();

String[] list = getResources().getStringArray(R.array.list);
for (String s : list) {
Expand All @@ -62,6 +65,7 @@ public void run() {
mAdapter.setOnLoadMoreListener(new OnLoadMoreListener() {
@Override
public void onLoadMore() {
Log.d("Sherlock","加载更多");
//模拟加载更多
recyclerView.postDelayed(new Runnable() {
@Override
Expand All @@ -74,7 +78,7 @@ public void run() {
mAdapter.loadFailed();
}
}
},1500);
},6500);
}
});

Expand All @@ -91,16 +95,19 @@ public void onClick(View v) {
case R.id.action_add:
setTitle("加载更多");
mAdapter.openAutoLoadMore(true);
mAdapter.setData(data);
mState = LOAD_ADD;
break;
case R.id.action_failed:
setTitle("加载更多失败");
mAdapter.openAutoLoadMore(true);
mAdapter.setData(data);
mState = LOAD_FAILED;
break;
case R.id.action_completed:
setTitle("没有更多数据");
mAdapter.openAutoLoadMore(true);
mAdapter.setData(data);
mState = LOAD_COMPLETED;
break;
case R.id.action_empty:
Expand Down
17 changes: 3 additions & 14 deletions app/src/main/java/xyz/zpayh/myadapter/ExpandableActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

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.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
Expand Down Expand Up @@ -34,15 +32,6 @@ private void initView() {
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

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();
}
});

RecyclerView recyclerView = (RecyclerView) findViewById(R.id.list);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
final MyExpandableAdapter adapter = new MyExpandableAdapter();
Expand All @@ -53,17 +42,17 @@ public void onClick(View view) {
String[] list = getResources().getStringArray(R.array.list);
Random random = new Random();
for (String s : list) {
Title title = new Title(s);
Title title = new Title(s,R.layout.item_title);
int size = random.nextInt(2)+2;
for (int i = 0; i < size; i++){
Details details = new Details("我是内容"+i);
title.add(details);
}
data.add(title);
}
Title title = new Title("Sherlock");
Title title = new Title("有子标题的标题",R.layout.item_title);
for (int j = 0; j < 3; j++) {
Title subTitle = new Title("你开心就好"+j);
Title subTitle = new Title("子标题"+j,R.layout.item_sub_title);
if (j == 1){
subTitle.setExpandable(true);
}
Expand Down
38 changes: 20 additions & 18 deletions app/src/main/java/xyz/zpayh/myadapter/MultiItemActivity.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
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.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
Expand Down Expand Up @@ -35,16 +33,7 @@ private void initView() {
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

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();

startActivity(new Intent(MultiItemActivity.this,ExpandableActivity.class));
}
});
final SwipeRefreshLayout refreshLayout = (SwipeRefreshLayout) findViewById(R.id.refresh);

RecyclerView recyclerView = (RecyclerView) findViewById(R.id.list);
//recyclerView.setLayoutManager(new LinearLayoutManager(this));
Expand Down Expand Up @@ -75,19 +64,32 @@ public void onClick(DialogInterface dialog, int which) {
}
});

List<IMultiItem> data = new ArrayList<>();
final List<IMultiItem> data = new ArrayList<>();

String[] list = getResources().getStringArray(R.array.list);
for (String s : list) {
data.add(new Text(s));
for (int i = 0; i < list.length; i++) {
data.add(new Text(list[i],i%3+1));
}

data.add(2,new Image(R.drawable.base_load_failed));
data.add(2,new Image(R.drawable.base_load_failed));
data.add(4,new Image(R.drawable.base_load_failed));
data.add(2,new Image(R.drawable.base_empty));
data.add(4,new Image(R.drawable.base_error));
data.add(7,new Image(R.drawable.base_load_failed));

adapter.setData(data);

refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
refreshLayout.postDelayed(new Runnable() {
@Override
public void run() {
adapter.setData(data);
refreshLayout.setRefreshing(false);
}
},2000);
}
});
}

}
2 changes: 1 addition & 1 deletion app/src/main/java/xyz/zpayh/myadapter/data/Image.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ public int getViewType() {

@Override
public int getSpanSize() {
return 0;
return 1;
}
}
7 changes: 5 additions & 2 deletions app/src/main/java/xyz/zpayh/myadapter/data/Text.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@
public class Text implements IMultiItem{
public String mText;

public Text(String text) {
private int mSpanSize;

public Text(String text,int spanSize) {
mText = text;
mSpanSize = spanSize;
}

@Override
Expand All @@ -31,6 +34,6 @@ public int getViewType() {

@Override
public int getSpanSize() {
return 0;
return mSpanSize;
}
}
8 changes: 5 additions & 3 deletions app/src/main/java/xyz/zpayh/myadapter/data/Title.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import xyz.zpayh.adapter.IExpandable;
import xyz.zpayh.adapter.IMultiItem;
import xyz.zpayh.myadapter.R;

/**
* 文 件 名: Title
Expand All @@ -23,9 +22,12 @@ public class Title implements IExpandable{

public String mTitle;

public Title(String title) {
private int mLayoutId;

public Title(String title,int layoutId) {
mTitle = title;
mItems = new ArrayList<>();
mLayoutId = layoutId;
}

public void add(IMultiItem item){
Expand All @@ -49,7 +51,7 @@ public List<IMultiItem> getSubItems() {

@Override
public int getLayoutRes() {
return R.layout.item_title;
return mLayoutId;
}

@Override
Expand Down
9 changes: 0 additions & 9 deletions app/src/main/res/layout/activity_expandable.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,4 @@
</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_expandable" />

<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@android:drawable/ic_dialog_email" />

</android.support.design.widget.CoordinatorLayout>
8 changes: 0 additions & 8 deletions app/src/main/res/layout/activity_multi_item.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,4 @@

<include layout="@layout/content_multi_item" />

<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@android:drawable/ic_dialog_email" />

</android.support.design.widget.CoordinatorLayout>
2 changes: 2 additions & 0 deletions app/src/main/res/layout/item_details.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/content"
android:layout_marginLeft="24dp"
android:layout_marginStart="24dp"
style="@style/Base.TextAppearance.AppCompat.Body2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
1 change: 1 addition & 0 deletions app/src/main/res/layout/item_image.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
app:cardElevation="4dp"
app:cardUseCompatPadding="true">
<ImageView
android:layout_gravity="center"
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
Expand Down
Loading

0 comments on commit 838c93a

Please sign in to comment.