Last active
July 1, 2018 03:21
-
-
Save brandhill/37334a2ac082ad1b727c10fa470014cc to your computer and use it in GitHub Desktop.
Generic Adapter Pseido code
This file contains hidden or 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
public class CategoriesAdapter extends GenericRecycleAdapter<Category, Holders.TextImageHolder> { | |
public CategoriesAdapter(List<Category> list, Context context) { | |
super(list, context); | |
} | |
@Override | |
void onItem(Category category) { | |
} | |
@Override | |
public int getLayout() { | |
return R.layout.categories_row; | |
} | |
@Override | |
public void onSet(Category item, Holders.TextImageHolder holder) { | |
} | |
} |
This file contains hidden or 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
public abstract class GenericRecycleAdapter<T, K extends RecyclerView.ViewHolder> extends RecyclerView.Adapter{ | |
private List<T> mList; | |
//default implementation code | |
public abstract int getLayout(); | |
@Override | |
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { | |
View v = LayoutInflater.from(parent.getContext()) | |
.inflate(getLayout(), parent, false); | |
return getCustomHolder(v); | |
} | |
public Holders.TextImageHolder getCustomHolder(View v) { | |
return new Holders.TextImageHolder(v){ | |
@Override | |
public void onClick(View v) { | |
onItem(mList.get(this.getAdapterPosition())); | |
} | |
}; | |
} | |
abstract void onItem(T t); | |
@Override | |
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { | |
onSet(mList.get(position), (K) holder); | |
} | |
public abstract void onSet(T item, K holder); | |
} | |
This file contains hidden or 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
public class Holders { | |
public static class TextImageHolder extends RecyclerView.ViewHolder implements View.OnClickListener{ | |
public TextView text; | |
public TextImageHolder(View itemView) { | |
super(itemView); | |
text = (TextView) itemView.findViewById(R.id.text); | |
text.setOnClickListener(this); | |
} | |
@Override | |
public void onClick(View v) { | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
source : https://stackoverflow.com/questions/26682277/how-do-i-get-the-position-selected-in-a-recyclerview