Skip to content

Commit

Permalink
Resturcture
Browse files Browse the repository at this point in the history
  • Loading branch information
nekocode committed Aug 23, 2016
1 parent f102ac3 commit abcc404
Show file tree
Hide file tree
Showing 9 changed files with 202 additions and 124 deletions.
11 changes: 5 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ dependencies {
Create reusable items for recyclerview.

```java
public class TestItem extends ItemPool.Item<String> {
public class TestItem extends Item<String> {
TextView textView;

@NonNull
Expand All @@ -51,13 +51,12 @@ Add itemtypes and data to the ItemPool. It will help the recyclerview automatica
ItemPool items = new ItemPool();
items.addType(TestItem.class);
items.addType(TestItem2.class);
items.onEvent(this);

items.add(new Header());
items.add("A");
items.add("B");
items.add(new TestData2());
items.add(new TestData());
items.add(new TestData2());

recyclerView.setAdapter(items.getAdapter());
items.attachTo(recyclerView);
```

For more detail about handling of the item event, see the [sample here](sample/src/main/java/cn/nekocode/itempool/sample/MainActivity.java).
4 changes: 2 additions & 2 deletions lib-itempool/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ android {
defaultConfig {
minSdkVersion 9
targetSdkVersion 23
versionCode 21
versionName "0.3"
versionCode 40
versionName "0.4"
}
buildTypes {
release {
Expand Down
58 changes: 58 additions & 0 deletions lib-itempool/src/main/java/cn/nekocode/itempool/Item.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright 2016 nekocode
*
* 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.
*/
package cn.nekocode.itempool;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

/**
* Created by nekocode on 16/8/23.
*/
public abstract class Item<Data> {

protected class InternalViewHolder extends RecyclerView.ViewHolder {
protected final Item item;

public InternalViewHolder(View itemView) {
super(itemView);
this.item = Item.this;
}
}
private InternalViewHolder internalViewHolder;

@NonNull
public abstract View onCreateItemView(
@NonNull LayoutInflater inflater,
@NonNull ViewGroup parent);

public abstract void onBindItem(
@NonNull final RecyclerView.ViewHolder holder,
@NonNull final Data data,
ItemEventHandler eventHandler);

public ItemEvent event(int action, Object data) {
return new ItemEvent(action, data, internalViewHolder);
}

protected RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent) {
View itemView = onCreateItemView(LayoutInflater.from(parent.getContext()), parent);
internalViewHolder = new InternalViewHolder(itemView);
return internalViewHolder;
}
}
87 changes: 87 additions & 0 deletions lib-itempool/src/main/java/cn/nekocode/itempool/ItemAdapter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* Copyright 2016 nekocode
*
* 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.
*/
package cn.nekocode.itempool;

import android.support.v4.util.Pair;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

/**
* Created by nekocode on 16/8/23.
*/
class ItemAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private final ItemPool itemPool;

public ItemAdapter(ItemPool itemPool) {
this.itemPool = itemPool;
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Item item = newItem(itemPool.getItemClass(viewType));
return item.onCreateViewHolder(parent);
}

@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
final Object data = itemPool.get(position);
Pair<Class<? extends Item>, ItemEventHandler> pair = itemPool.getItemClass(data.getClass());

final Item item = ((Item.InternalViewHolder) holder).item;
final ItemEventHandler handler = pair.second;
item.onBindItem(holder, data, handler);

holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (handler != null)
handler.onEvent(item.getClass(), new ItemEvent(ItemEvent.ITEM_CLICK, data, holder));
}
});

holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
if (handler != null) {
handler.onEvent(item.getClass(), new ItemEvent(ItemEvent.ITEM_LONGCLICK, data, holder));
return true;
}
return false;
}
});
}

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

@Override
public int getItemViewType(int position) {
return itemPool.getItemType(position);
}

private static <ItemToCreate extends Item> ItemToCreate newItem(Class<ItemToCreate> itemClass) {
Item item;
try {
item = itemClass.newInstance();
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
}
return (ItemToCreate) item;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@
public interface ItemEventHandler {

void onEvent(
@NonNull Class<? extends ItemPool.Item> clazz,
@NonNull Class<? extends Item> clazz,
@NonNull ItemEvent event);
}
Loading

0 comments on commit abcc404

Please sign in to comment.