Skip to content

Instantly share code, notes, and snippets.

@meditat
Created March 27, 2018 03:16
Show Gist options
  • Save meditat/8affb538a20cbac39c7c3de85fc6f43d to your computer and use it in GitHub Desktop.
Save meditat/8affb538a20cbac39c7c3de85fc6f43d to your computer and use it in GitHub Desktop.
//using recyclevieew
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class GreenAdapter extends RecyclerView.Adapter<GreenAdapter.NumberViewHolder> {
private int mNumberItems;
public GreenAdapter(int numberItems) {
this.mNumberItems = numberItems;
}
@Override
public NumberViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View view = inflater.inflate(R.layout.number_list_item,parent,false);
NumberViewHolder holder = new NumberViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(NumberViewHolder holder, int position) {
holder.bind(position);
}
@Override
public int getItemCount() {
return mNumberItems;
}
/**
* Cache of the children views for a list item.
*/
class NumberViewHolder extends RecyclerView.ViewHolder {
// Will display the position in the list, ie 0 through getItemCount() - 1
TextView listItemNumberView;
/**
* Constructor for our ViewHolder. Within this constructor, we get a reference to our
* TextViews and set an onClickListener to listen for clicks. Those will be handled in the
* onClick method below.
*
* @param itemView The View that you inflated in
* {@link GreenAdapter#onCreateViewHolder(ViewGroup , int)}
*/
public NumberViewHolder(View itemView) {
super(itemView);
listItemNumberView = (TextView) itemView.findViewById(R.id.tv_item_number);
}
/**
* A method we wrote for convenience. This method will take an integer as input and
* use that integer to display the appropriate text within a list item.
*
* @param listIndex Position of the item in the list
*/
void bind(int listIndex) {
listItemNumberView.setText(String.valueOf(listIndex));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment