Skip to content

Instantly share code, notes, and snippets.

@mikepenz
Created December 14, 2016 22:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikepenz/1085b1dcfffb941974a56c829e79a230 to your computer and use it in GitHub Desktop.
Save mikepenz/1085b1dcfffb941974a56c829e79a230 to your computer and use it in GitHub Desktop.
quick and dirty ContainerItem for the FastAdapter
package com.fyusion.fyuse.item;
import android.content.Context;
import android.support.annotation.IdRes;
import android.support.annotation.LayoutRes;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.fyusion.fyuse.LatoTextView;
import com.fyusion.fyuse.R;
import com.mikepenz.fastadapter.items.AbstractItem;
import com.mikepenz.fastadapter.utils.ViewHolderFactory;
import java.util.List;
/**
* Created by mikepenz on 26.09.16.
*/
public class ContainerItem extends AbstractItem<ContainerItem, ContainerItem.ViewHolder> {
//the static ViewHolderFactory which will be used to generate the ViewHolder for this Item
private static final ViewHolderFactory<? extends ContainerItem.ViewHolder> FACTORY = new ContainerItem.ItemFactory();
private @LayoutRes int layout;
private View view;
public ContainerItem() {
super();
}
public @LayoutRes int getLayout() {
return layout;
}
public ContainerItem withLayout(@LayoutRes int layout) {
this.layout = layout;
return this;
}
@Override
public int getType() {
//noinspection ResourceType
return R.layout.empty_item;
}
@Override
public int getLayoutRes() {
return R.layout.empty_item;
}
/**
* binds the data of this item onto the viewHolder
*
* @param viewHolder the viewHolder of this item
*/
@Override
public void bindView(ContainerItem.ViewHolder viewHolder, List payloads) {
super.bindView(viewHolder, payloads);
if(view == null) {
LayoutInflater inflater = (LayoutInflater) view.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(layout, viewHolder.view, true);
} else {
viewHolder.view.removeAllViews();
viewHolder.view.addView(view);
}
}
/**
* our ItemFactory implementation which creates the ViewHolder for our adapter.
* It is highly recommended to implement a ViewHolderFactory as it is 0-1ms faster for ViewHolder creation,
* and it is also many many times more efficient if you define custom listeners on views within your item.
*/
protected static class ItemFactory implements ViewHolderFactory<ContainerItem.ViewHolder> {
public ContainerItem.ViewHolder create(View v) {
return new ContainerItem.ViewHolder(v);
}
}
/**
* return our ViewHolderFactory implementation here
*
* @return
*/
@Override
public ViewHolderFactory<? extends ContainerItem.ViewHolder> getFactory() {
return FACTORY;
}
/**
* our ViewHolder
*/
public static class ViewHolder extends RecyclerView.ViewHolder {
protected ViewGroup view;
public View inflatedView;
public ViewHolder(View view) {
super(view);
this.view = (ViewGroup) view;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment