Created
October 4, 2018 06:59
-
-
Save xsahil03x/b72ec5e55c33e5b625c66e1e6012aff7 to your computer and use it in GitHub Desktop.
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
public class MyGamesAdapter extends RecyclerView.Adapter<MyGamesAdapter.MyGamesViewHolder> { | |
private static final int TYPE_FIRST_ITEM = 0; | |
private static final int TYPE_ITEM = 1; | |
private List<Game> gameList; | |
private Context context; | |
private MyGameItemClickListener itemClickListener; | |
public MyGamesAdapter(Context context, MyGameItemClickListener myGameItemClickListener) { | |
this.context = context; | |
itemClickListener = myGameItemClickListener; | |
} | |
public void addGameToList(List<Game> gameList) { | |
this.gameList = gameList; | |
notifyDataSetChanged(); | |
} | |
@Override | |
public int getItemViewType(int position) { | |
if (position == 0) | |
return TYPE_FIRST_ITEM; | |
else | |
return TYPE_ITEM; | |
} | |
@NonNull | |
@Override | |
public MyGamesViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { | |
View view = LayoutInflater.from(parent.getContext()) | |
.inflate(R.layout.layout_my_games_item, parent, false); | |
return new MyGamesViewHolder(view); | |
} | |
@Override | |
public void onBindViewHolder(@NonNull final MyGamesViewHolder holder, int position) { | |
bind(holder, position); | |
} | |
private void bind(MyGamesViewHolder holder, int position) { | |
switch (holder.getItemViewType()) { | |
case TYPE_FIRST_ITEM: | |
holder.tvGameName.setText("Add Game"); | |
GlideApp.with(context) | |
.load(context.getDrawable(R.drawable.add_icon)) | |
.into(holder.ivGameLogo); | |
break; | |
case TYPE_ITEM: | |
Game game = gameList.get(position); | |
holder.tvGameName.setText(game.getTitle()); | |
GlideApp.with(context) | |
.load(game.getIcon()) | |
.into(holder.ivGameLogo); | |
break; | |
} | |
} | |
@Override | |
public int getItemCount() { | |
if (gameList == null) { | |
return 0; | |
} else { | |
return gameList.size(); | |
} | |
} | |
class MyGamesViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { | |
@BindView(R.id.ivGameLogo) | |
ImageView ivGameLogo; | |
@BindView(R.id.tvGameName) | |
TextView tvGameName; | |
MyGamesViewHolder(View itemView) { | |
super(itemView); | |
ButterKnife.bind(this, itemView); | |
itemView.setOnClickListener(this); | |
} | |
@Override | |
public void onClick(View v) { | |
itemClickListener.onItemClicked(gameList.get(getAdapterPosition()), getAdapterPosition()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment