forked from nekocode/Items
-
Notifications
You must be signed in to change notification settings - Fork 0
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
nekocode
committed
Aug 23, 2016
1 parent
f102ac3
commit abcc404
Showing
9 changed files
with
202 additions
and
124 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
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,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
87
lib-itempool/src/main/java/cn/nekocode/itempool/ItemAdapter.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,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; | ||
} | ||
} |
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
Oops, something went wrong.