Skip to content

Commit

Permalink
add walle
Browse files Browse the repository at this point in the history
  • Loading branch information
donniesky committed May 5, 2017
1 parent 89c6fac commit 6b1e94e
Show file tree
Hide file tree
Showing 16 changed files with 644 additions and 84 deletions.
47 changes: 45 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,45 @@
# SuperAdapter
recyclerview 万能adapter
# Walle <img alt="Walle is an android library" src="https://www.cleveroad.com/public/comercial/label-android.svg" height="19"> [![minSdk for Walle](https://img.shields.io/badge/minSdk-15-green.svg)](#)

## This is Walle - Recyclerview.Adapter's helper


## Setup and usage
### installation

with gradle
```GRADLE
compile 'com.github.donniesky:walle:0.0.1'
```

Assuming you have installed jitpack provider:
```GRADLE
allprojects {
repositories {
jcenter()
maven { url "https://jitpack.io" }
}
}
```

or just import `Walle` module to your project.

## Features
Proxy Pattern.

* No need change anything with your target adapter;
* Not destory target adapter position;
* Support dynamic add & remove;
* No dependencies code build order

### Usage

```JAVA
Walle wrapper = Walle.newBuilder()
.wrapperAdapter(adapter)
.autoLoadMore(false)
.headerViewRes(R.layout.view_header)
.emptyViewRes(R.layout.view_empty)
.footerViewRes(R.layout.view_footer)
.loadMoreViewRes(R.layout.view_loadmore)
.build();
```
4 changes: 3 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ android {
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'
/*compile 'com.github.donniesky:BaseAdapter:0.0.2'*/
compile project(':library')
}
54 changes: 53 additions & 1 deletion app/src/main/java/me/donnie/superadapter/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,65 @@
package me.donnie.superadapter;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import me.donnie.adapter.MultiItemAdapter;
import me.donnie.adapter.Walle;

public class MainActivity extends AppCompatActivity {

private RecyclerView rv;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

rv = (RecyclerView) findViewById(R.id.rv);
rv.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
rv.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));

TestAdapter adapter = new TestAdapter();
adapter.setOnItemClickListener(new MultiItemAdapter.OnItemClickListener() {
@Override
public void onItemClick(MultiItemAdapter adapter, View view, int position) {
Toast.makeText(MainActivity.this, position+"", Toast.LENGTH_SHORT).show();
}
});

Walle wrapper = Walle.newBuilder()
.wrapperAdapter(adapter)
.autoLoadMore(false)
.headerViewRes(R.layout.view_header)
.emptyViewRes(R.layout.view_empty)
.footerViewRes(R.layout.view_footer)
.loadMoreViewRes(R.layout.view_loadmore)
.build();

rv.setAdapter(wrapper.getWrapperAdapter());
adapter.addNewData(getData());

}

private List<Test> getData() {
List<Test> tests = new ArrayList<>();
for (int i = 0; i < 20; i++) {
Test test = new Test();
test.setId(i);
test.setName("name "+i);
test.setDesc("desc "+i);
tests.add(test);
}
return tests;
}


}
17 changes: 4 additions & 13 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,19 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/rv"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="me.donnie.superadapter.MainActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>

</android.support.constraint.ConstraintLayout>
tools:context="me.donnie.superadapter.MainActivity"/>
37 changes: 37 additions & 0 deletions app/src/main/res/layout/list_test_item.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"/>

<LinearLayout
android:layout_marginLeft="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">

<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="123"/>

<TextView
android:id="@+id/desc"
android:layout_marginTop="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="aksjdfkljalksdj"
android:textSize="18sp"/>

</LinearLayout>


</LinearLayout>
14 changes: 14 additions & 0 deletions app/src/main/res/layout/view_empty.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="50sp"
android:text="内容为空"/>

</LinearLayout>
13 changes: 13 additions & 0 deletions app/src/main/res/layout/view_footer.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="50dp">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="这是尾部"/>

</LinearLayout>
13 changes: 13 additions & 0 deletions app/src/main/res/layout/view_header.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="50dp">

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"/>

</LinearLayout>
13 changes: 13 additions & 0 deletions app/src/main/res/layout/view_loadmore.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="50dp">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="加载更多..."/>

</LinearLayout>
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ buildscript {
allprojects {
repositories {
jcenter()
maven { url "https://jitpack.io" }
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@
* @description
*/

public abstract class SuperAdapter<T> extends MultiItemAdapter<T, BaseViewHolder> {
public abstract class BaseAdapter<T> extends MultiItemAdapter<T, ViewHolder> {

protected int mLayoutResId;

public SuperAdapter(final int layoutResId, List<T> datas) {
public BaseAdapter(final int layoutResId, List<T> datas) {
super(datas);
this.mLayoutResId = layoutResId;

Expand All @@ -31,11 +29,11 @@ public boolean isForViewType(T item, int position) {
}

@Override
public void convert(BaseViewHolder holder, T t, int position) {
SuperAdapter.this.convert(holder, t, position);
public void convert(ViewHolder holder, T t, int position) {
BaseAdapter.this.convert(holder, t, position);
}
});
}

protected abstract void convert(BaseViewHolder holder, T t, int position);
protected abstract void convert(ViewHolder holder, T t, int position);
}
30 changes: 5 additions & 25 deletions library/src/main/java/me/donnie/adapter/MultiItemAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
Expand All @@ -35,7 +34,7 @@
* @description
*/

public abstract class MultiItemAdapter<T, K extends BaseViewHolder> extends RecyclerView.Adapter<K> {
public abstract class MultiItemAdapter<T, K extends ViewHolder> extends RecyclerView.Adapter<K> {

private static final String TAG = MultiItemAdapter.class.getSimpleName();

Expand All @@ -47,10 +46,10 @@ public abstract class MultiItemAdapter<T, K extends BaseViewHolder> extends Recy

public static final int SLIDEIN_LEFT = 0x00000004;

public static final int HEADER_VIEW = 0x00000111;
/*public static final int HEADER_VIEW = 0x00000111;
public static final int LOADING_VIEW = 0x00000222;
public static final int FOOTER_VIEW = 0x00000333;
public static final int EMPTY_VIEW = 0x00000555;
public static final int EMPTY_VIEW = 0x00000555;*/

private OnItemClickListener mOnItemClickListener;
private OnItemChildClickListener mOnItemChildClickListener;
Expand Down Expand Up @@ -172,33 +171,14 @@ public K onCreateViewHolder(ViewGroup parent, int viewType) {

@Override
public void onBindViewHolder(K holder, int position) {
convert(holder, mDatas.get(holder.getLayoutPosition()));
convert(holder, mDatas.get(position));
}

@Override
public int getItemCount() {
return mDatas.size();
}

@Override
public void onViewAttachedToWindow(K holder) {
super.onViewAttachedToWindow(holder);
int viewType = holder.getItemViewType();
if (viewType == EMPTY_VIEW || viewType == HEADER_VIEW || viewType == FOOTER_VIEW
|| viewType == LOADING_VIEW) {
setFullSpan(holder);
} else {

}
}

protected void setFullSpan(RecyclerView.ViewHolder holder) {
if (holder.itemView.getLayoutParams() instanceof StaggeredGridLayoutManager.LayoutParams) {
StaggeredGridLayoutManager.LayoutParams params = (StaggeredGridLayoutManager.LayoutParams) holder.itemView.getLayoutParams();
params.setFullSpan(true);
}
}

private void addAnimation(RecyclerView.ViewHolder holder) {
if (mAnimEnable) {
if (holder.getLayoutPosition() > mLastPosition) {
Expand Down Expand Up @@ -239,7 +219,7 @@ public MultiItemAdapter addItemViewDelegate(int viewType, ItemViewDelegate<T> it
return this;
}

private void bindViewClickListener(final BaseViewHolder holder) {
private void bindViewClickListener(final ViewHolder holder) {
if (holder == null) {
return;
}
Expand Down
Loading

0 comments on commit 6b1e94e

Please sign in to comment.